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

singleton class initialization blocks threads #5901

Closed
kares opened this issue Oct 2, 2019 · 0 comments · Fixed by #5902
Closed

singleton class initialization blocks threads #5901

kares opened this issue Oct 2, 2019 · 0 comments · Fixed by #5902
Milestone

Comments

@kares
Copy link
Member

@kares kares commented Oct 2, 2019

these are sample numbers from a huge Rails app being under load :

jruby-bug_singleton-class-init

from various places Rails likes to do, but mostly instance_exec and clone, multiple threads end up synchronizing over allocating the singleton class, sample stack:

org.jruby.Ruby.addModule(RubyModule)
org.jruby.RubyModule.<init>(Ruby, RubyClass, boolean)
org.jruby.RubyClass.<init>(Ruby, RubyClass, boolean)	
org.jruby.MetaClass.<init>(Ruby, RubyClass, RubyBasicObject)	
org.jruby.RubyBasicObject.getSingletonClassCloneAndAttach(RubyBasicObject)	
org.jruby.RubyBasicObject.rbCloneInternal(ThreadContext, boolean)	
org.jruby.RubyBasicObject.rbClone()	
org.jruby.RubyKernel.rbClone(ThreadContext, IRubyObject)	
org.jruby.RubyKernel$INVOKER$s$rbClone.call(ThreadContext, IRubyObject, RubyModule, String)	
org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(IRubyObject, RubyClass, ThreadContext, IRubyObject)	
org.jruby.runtime.callsite.CachingCallSite.call(ThreadContext, IRubyObject, IRubyObject)	
...bundle.jruby.$2_dot_3_dot_0.gems.activerecord_minus_4_dot_2_dot_11_dot_1.lib.active_record.scoping.named.invokeOther2:clone(ThreadContext, IRubyObject, IRubyObject)
...bundle.jruby.$2_dot_3_dot_0.gems.activerecord_minus_4_dot_2_dot_11_dot_1.lib.active_record.scoping.named.RUBY$method$all$0(ThreadContext, StaticScope, IRubyObject, Block, RubyModule, String)	

the solution should be to use a concurrent collection which does less blocking or handle runtime.addModule(this) from the RubyModule differently.

kares added a commit that referenced this issue Oct 2, 2019
the synchronization of adding a new module (class) to the system,
has a considerable cost e.g. in threaded envs such as Rails

we simply avoid the sync cost by using a concurrent collection

resolves GH-5901
@kares kares added this to the JRuby 9.2.9.0 milestone Oct 8, 2019
@kares kares closed this in #5902 Oct 8, 2019
kares added a commit that referenced this issue Oct 8, 2019
the synchronization of adding a new module (class) to the system,
has a considerable cost e.g. in threaded envs such as Rails

we simply avoid the sync cost by using a concurrent collection

resolves GH-5901
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant