A reference to a class method in multiple threads causes the steepness of a circular autoload dependency

Code:

threads = [] Thread.abort_on_exception=true begin # throw exceptions in threads so we can see them threads << Thread.new{@a = MyClass.m1} threads << Thread.new{@b = MyClass.m2} threads << Thread.new{@c = MyClass.m3} threads.each { |thr| thr.join } rescue Exception => e puts "EXCEPTION: #{e.inspect}" puts "MESSAGE: #{e.message}" end 

Crash:

.rvm/gems/ ruby-2.1.3@req /gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:in load_missing_constant': Circular dependency detected while autoloading constant MyClass

After a short search, it seems that since each thread references MyClass, it causes an error during manual startup. If I add a single line with a link to MyClass before I call Thread, it seems to prevent the error.

My question is, is there a β€œright” way to prevent this, or is it some kind of ruby ​​error? I understand that startup was thread safe, for http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/

+6
source share
1 answer

Ruby had a problem with Threaded autoload and even require , for that matter. This is fixed in ruby 2.0 , see this bug at ruby-lang.org .

Rails may still have the error [Cyclic dependency error with lib classes] (Cyclic dependency error with lib classes)

In either case, the fix should simply require or reference the character before throwing it into the stream.

 require 'myclass' 

or

 defined? MyClass 
0
source

Source: https://habr.com/ru/post/976159/


All Articles