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

Make Sprockets::Utils.module_include thread safe on JRuby #759

Merged
merged 1 commit into from
Sep 20, 2022
Merged
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
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