Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport/v3.x] Make Sprockets::Utils.module_include thread safe on JRuby #760

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**Master**

* Make `Sprockets::Utils.module_include` thread safe on JRuby. [#760](https://github.com/rails/sprockets/pull/760)

**3.7.2** (June 19, 2018)

* Security release for [CVE-2018-3760](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-3760).
Expand Down
41 changes: 23 additions & 18 deletions lib/sprockets/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,39 @@ def normalize_extension(extension)
false
end

MODULE_INCLUDE_MUTEX = Mutex.new
private_constant :MODULE_INCLUDE_MUTEX

# Internal: Inject into target module for the duration of the block.
#
# mod - Module
#
# Returns result of block.
def module_include(base, mod)
old_methods = {}
MODULE_INCLUDE_MUTEX.synchronize do
old_methods = {}

mod.instance_methods.each do |sym|
old_methods[sym] = base.instance_method(sym) if base.method_defined?(sym)
end
mod.instance_methods.each do |sym|
old_methods[sym] = base.instance_method(sym) if base.method_defined?(sym)
end

unless UNBOUND_METHODS_BIND_TO_ANY_OBJECT
base.send(:include, mod) unless base < mod
end
unless UNBOUND_METHODS_BIND_TO_ANY_OBJECT
base.send(:include, mod) unless base < mod
end

mod.instance_methods.each do |sym|
method = mod.instance_method(sym)
base.send(:define_method, sym, method)
end
mod.instance_methods.each do |sym|
method = mod.instance_method(sym)
base.send(:define_method, sym, method)
end

yield
ensure
mod.instance_methods.each do |sym|
base.send(:undef_method, sym) if base.method_defined?(sym)
end
old_methods.each do |sym, method|
base.send(:define_method, sym, method)
yield
ensure
mod.instance_methods.each do |sym|
base.send(:undef_method, sym) if base.method_defined?(sym)
end
old_methods.each do |sym, method|
base.send(:define_method, sym, method)
end
end
end

Expand Down