When Rails reloads in dev environment Tubesock::Hijacked controller crashes rails
The backtrace that pops when rails reloads in dev environment is:
activesupport-4.0.1/lib/active_support/dependencies.rb:446:in `load_missing_constant': A copy of Api::NotificationController has been removed from the module tree but is still active! (ArgumentError)
from activesupport-4.0.1/lib/active_support/dependencies.rb:184:in `const_missing'
from app/app/controllers/api/notification_controller.rb:28:in `block (2 levels) in flow'
from tubesock-0.2.2/lib/tubesock.rb:71:in `call'
from tubesock-0.2.2/lib/tubesock.rb:71:in `each'
fromtubesock-0.2.2/lib/tubesock.rb:71:in `close'
from tubesock-0.2.2/lib/tubesock.rb:66:in `block in listen'
How can I gracefully stop tubesock when rails reloads? Any pointer?
hmm, probably just reload your browser (and any tabs open)? I don't think I saw this happen to me when developing.
I probably didn't explain myself.
- I start my rails app in dev
- Open my browser and trigger a websocket connection to my "hijacked" controller
- Keep the websocket open
- Change any code in the rails app and save
- Crash
I'm not really sure. I didn't see this while working on my project.
Does it happen if you run the example project?
https://github.com/ngauthier/sock-chat
Nick
I believe this is due to running background threads with rails auto-reload enabled. I was getting similar errors, and learned about the problem from this blog post: http://bibwild.wordpress.com/2011/11/14/multi-threading-in-rails-activerecord-3-0-3-1/ (see the config.cache_classes section)
Basically the problem is rails has to unload all classes to support auto-reload, and background threads that are currently executing don't like having their classes pulled out from under them :). Unfortunately the only fix seems to be to disable auto-reload by setting config.cache_classes = true.