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

defined? vs concurrent autoload, JRuby blocks, MRI returns false #2180

Closed
thedarkone opened this issue Nov 12, 2014 · 1 comment
Closed

defined? vs concurrent autoload, JRuby blocks, MRI returns false #2180

thedarkone opened this issue Nov 12, 2014 · 1 comment

Comments

@thedarkone
Copy link
Contributor

From the discussion on rails/rails#17091.

On MRI if other thread (Thread A) is autoloading a Foo constant, defined?(Foo) (in Thread B) returns right away returning false.

On JRuby defined?(Foo) (in Thread B) blocks and waits for Thread A to finish.

Script to repro: https://gist.github.com/thedarkone/d1c26e08f7eca3bc224e

MRI:

~/w/autoload-thread-safety> ruby --version
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.2.0]
~/w/autoload-thread-safety> ruby -I. test.rb 
A accessing const
defining Foo::Bar
B sees Foo::Bar not defined yet
done defining Foo::Bar
A finished with const

on JRuby 1.7.11:

~/w/autoload-thread-safety> jruby --2.0 --version
jruby 1.7.11 (2.0.0p195) 2014-02-24 86339bb on Java HotSpot(TM) 64-Bit Server VM 1.6.0_51-b11-457-11M4509 [darwin-x86_64]
~/w/autoload-thread-safety> jruby --2.0 -I. test.rb 
A accessing const
defining Foo::Bar
done defining Foo::Bar
A finished with const
B accessing const
B finished with const

PS: please note that I think JRuby's behavior makes more sense and should have been the correct one 😃.

@headius
Copy link
Member

headius commented Oct 7, 2021

Not sure why I didn't comment here before 9.3 but this appears to match Ruby 2.6.8 behavior now:

$ jruby -I. test.rb
A accessing const
defining Foo::Bar
B accessing const
done defining Foo::Bar
B finished with constA finished with const

$ rvm ruby-2.6.8 do ruby -I. test.rb
A accessing const
defining Foo::Bar
B accessing const
done defining Foo::Bar
A finished with const
B finished with const

Note that A and B finished at the same time, which may change the order, but this is expected for parallel execution. I think we can call this fixed in 9.3.

@headius headius modified the milestones: JRuby 9.3.1.0, JRuby 9.3.0.0 Oct 7, 2021
@headius headius closed this as completed Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants