Skip to content

Commit

Permalink
Merge pull request #759 from ntkme/module-include-thread-safety
Browse files Browse the repository at this point in the history
Make Sprockets::Utils.module_include thread safe on JRuby
  • Loading branch information
rafaelfranca committed Sep 20, 2022
2 parents e03fc2a + 722d587 commit 1276b43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprockets/blob/master/UPGRADING.md

- Add support for Rack 3.0. Headers set by sprockets will now be lower case. [#758](https://github.com/rails/sprockets/pull/758)
- Make `Sprockets::Utils.module_include` thread safe on JRuby. [#759](https://github.com/rails/sprockets/pull/759)

## 4.1.0

Expand Down
39 changes: 22 additions & 17 deletions lib/sprockets/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,38 @@ def concat_javascript_sources(buf, source)
buf
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

mod.instance_methods.each do |sym|
method = mod.instance_method(sym)
if base.method_defined?(sym)
base.send(:alias_method, sym, sym)
mod.instance_methods.each do |sym|
method = mod.instance_method(sym)
if base.method_defined?(sym)
base.send(:alias_method, sym, sym)
end
base.send(:define_method, sym, method)
end
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

0 comments on commit 1276b43

Please sign in to comment.