Skip to content

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

@thedarkone

Description

@thedarkone

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 😃.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions