This project is archived and is in readonly mode.
Racing Inside ActiveSupport::Dependencies
Reported by skibumsworld | June 27th, 2008 @ 02:11 AM | in 2.1.3
I'm working on a project that uses mongrel handlers to service requests. We use ActiveRecord but not ActionController. We've been seeing weird errors which look like:
#<LoadError: Expected ./my_model.rb to define MyModel>
the file does define the class but an exception is thrown. After some digging we found that the problem is in dependencies.rb in the require_or_load method.
If a thread is interrupted right after:
loaded << expanded
then any other threads that attempt to load the same file will get the error above, because the file hasn't been loaded yet even though it's name already appears in the loaded list.
I've included a test to reproduce this. The test only works against the development version of rails. If you want you can run it against other version but you will need to change the way Dependencies module is declared since it didn't used to be inside the ActiveSupport module.
I'm working on a patch right now, but any suggestions on how to fix this issue are more then welcome.
One last thing. When running the test please don't forget to change the first require to point at the correct location for activerecord.
Comments and changes to this ticket
-
Jeremy Kemper June 27th, 2008 @ 08:53 AM
- State changed from new to open
- Milestone set to 2.1.1
- Assigned user set to Jeremy Kemper
-
DrMark June 27th, 2008 @ 09:07 AM
Jeremy,
While you are working in there, would you fix the typo in will_unload?. It is currently:
def will_unload?(const_desc)
autoloaded?(desc) ||
explicitly_unloadable_constants.include?(to_constant_name(const_desc))
end
but should be:
def will_unload?(const_desc)
autoloaded?(const_desc) ||
explicitly_unloadable_constants.include?(to_constant_name(const_desc))
end
Note the autoloaded line. I will make a separate ticket if necessary.
Thanks!
DrMark
-
Jeremy Kemper June 28th, 2008 @ 01:15 AM
- Tag changed from activesupport, dependencies to activesupport, dependencies, patch
skibumsworld, could you use a Mutex instead? Thread.critical has issues on jruby so other synchronization primitives are preferred.
-
Jeremy Kemper June 28th, 2008 @ 01:16 AM
DrMark, will_unload? appears to be completely unused. Do you use it?
-
DrMark June 28th, 2008 @ 03:52 AM
Hi Jeremy,
Yes. The benchmarkforrails plugin uses it (among others). I thought that it was odd that a bug that obvious could get through :)
If you could fix it or remove it that would be great. That would save me having to monkey patch it all the time :)
Thanks!
-
Jeremy Kemper June 28th, 2008 @ 05:09 AM
Quite so, DrMark. I fixed it in master, 2-1-stable, and 2-0-stable. How odd.
-
Jeremy Kemper July 30th, 2008 @ 10:06 AM
- Assigned user changed from Jeremy Kemper to josh
- State changed from open to stale
- Milestone cleared.
-
Jeremy Kemper October 24th, 2008 @ 03:19 AM
- Milestone set to 2.1.3
- State changed from stale to open
- Assigned user changed from josh to Jeremy Kemper
-
Stephen Bannasch December 27th, 2008 @ 04:23 PM
Here are links to two related bugs in ruby's implementation of require when used with threads:
require is not thread-safe http://redmine.ruby-lang.org/iss...
autoload is not thread-safe http://redmine.ruby-lang.org/iss...
Basically there does not appear to be a safe way for require to work in a multi-threaded application unless the first use of require locks until the require is complete.
-
Pratik March 8th, 2009 @ 02:07 PM
- State changed from open to stale
Putting the ticket in stale. Please reopen if this is still an issue.
Thanks.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>