I recently ran into this frustrating and intermittent error in Ruby on Rails 4 (JRuby, actually):
Circular dependency detected while autoloading constant
Googling turned up several articles advising one to abide by the Rails conventions, but that was not the issue.
The application I’m writing uses background threads. The problem shows up when trying to instantiate a Rails controller in one of them. Rails searches for object definitions dynamically, so when multiple threads are trying to instantiate the same object, there’s a race condition.
The fix is define a mutex for access control:
$thread_mutex = Mutex.new
And then use it in the threads when instantiating the controller, model, or some Ruby class:
mc = nil $thread_mutex.synchronize do mc = MyController.new end
![It was bound to happen eventually. This data theft will enable almost limitless [xkcd.com/792]-style password reuse attacks in the coming weeks. There's only one group that comes out of this looking smart: Everyone who pirated Photoshop. It was bound to happen eventually. This data theft will enable almost limitless [xkcd.com/792]-style password reuse attacks in the coming weeks. There's only one group that comes out of this looking smart: Everyone who pirated Photoshop.](http://i.imgur.com/mgVu9VR.png)



