Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Last active December 14, 2015 11:19
Show Gist options
  • Save bensheldon/5077719 to your computer and use it in GitHub Desktop.
Save bensheldon/5077719 to your computer and use it in GitHub Desktop.

Running rails in threadsafe mode

# Enable threaded mode. The line you should uncomment out to enable Rails to run
# in threaded mode. Apparently this isn't actually a big deal, though
# is scary to turn on: http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html
config.threadsafe!
# Lazy/Autoloading is not threadsafe, so you need to tell Rails to eagerly load
# all necessary libraries when it starts up. These settings are NOT inherited
# from your autoload_path settings (seems like they should, huh?) so you need
# to duplicate them (or have a better config management strategy)
# Some confusion: https://github.com/rails/rails/issues/3148#issuecomment-3210973
config.eager_load_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]
# BUT, rake tasks don't eager-load properly (not sure if defect or by design),
# so you need to re-enable lazy/autoloading when running rake tasks...
# which I guess means your rake tasks aren't threadsafe... but I guess that's ok.
config.dependency_loading = true if $rails_rake_task
@eddietejeda
Copy link

I use:

config.threadsafe! unless $rails_rake_task

which seems to work well for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment