Ruby on Rails Wednesday, February 4, 2015

Hey Xavier, thanks so much! Really appreciate your answer!

Could you maybe describe to me (or point me in the right direction) to how content autoloading happens?

You know what it? I've kinda solved the issue by just referencing the constant explicitly. But I'm really curious as to how Rails ends up throwing that circular dependency exception.

Like... I get what happens when you have a non-threadsafe variable... because actually setting a variable requires multiple steps in the MRI and so while one thread is reading from a variable before setting it, another thread has already set it to something.

But what's going on here.... like... one thread tries to autoload a constant... and then before it's done another thread does the same and then... why does that result in a circular reference exception? Any ideas?

On Wednesday, 4 February 2015 10:23:29 UTC, Xavier Noria wrote:
On Wed, Feb 4, 2015 at 12:12 AM, Andy Rose <thatan...@gmail.com> wrote:

Hey guys, I posted this on Stackoverflow but it's not getting much love.

Note in the question that I've found a solution to my problem. However I'm curious as to what is going on here. Why does Rails encounter a race condition when auto-loading an ActiveRecord model association...?

http://stackoverflow.com/questions/28267513/why-does-rails-throw-an-autoloading-constant-exception-when-i-use-threads

Feel free to answer on SO if you have an account, but here's cool too.

Hey, I am not on SO.

Constant autoloading is not thread-safe (it can't be). That's one of the reasons by default Rails eager loads the application code in production mode.

There are a few things you can do. The easiest one is to enable eager loading by setting config.eager_load = true (in modern versions of Rails). Another possibility is to load the files with the involved classes using require_dependency in your script before spawning threads.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1cacb44f-5c82-4e78-a834-1c9fb88b7998%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment