rails: Rails reload! (both in console and using the reloader.reload!) hangs with multi-threaded applications

Steps to reproduce

rails new testme
cd testme
bundle exec rails generate scaffold User
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rails console
# in the console:
require 'benchmark'
Thread.new { User.create!; sleep(100) }
# wait for 1 second and run reload - it will hang for around 10 seconds
puts Benchmark.measure { reload! }
# 0.019528   0.000312   0.019840 ( 10.028938)

Expected behavior

Since the thread is sleeping after user creation and does nothing, I would expect not to hang for such a long time.

Actual behavior

It hangs for 10 seconds. Same happens when reloading from within a process that includes Rails. It hangs for 10 seconds despite the fact that the thread is sleeping more. Even if thread is sleeping shorted period of time (lets say 5s), the reload takes 10.

This is in particular problematic for processes running with Rails in a dev mode, where threads are long living (worker polls for example) and blocked on a Queue#pop.

I confirmed this problem affects sqlite as well as pg.

System configuration

Rails & Ruby version:

About your application's environment
Rails version             7.0.1
Ruby version              ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
RubyGems version          3.2.32
Rack version              2.2.3
Middleware                ActionDispatch::HostAuthorization, Rack::Sendfile, ActionDispatch::Static, ActionDispatch::Executor, ActionDispatch::ServerTiming, ActiveSupport::Cache::Strategy::LocalCache::Middleware, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, ActionDispatch::RemoteIp, Sprockets::Rails::QuietAssets, Rails::Rack::Logger, ActionDispatch::ShowExceptions, WebConsole::Middleware, ActionDispatch::DebugExceptions, ActionDispatch::ActionableExceptions, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ContentSecurityPolicy::Middleware, ActionDispatch::PermissionsPolicy::Middleware, Rack::Head, Rack::ConditionalGet, Rack::ETag, Rack::TempfileReaper
Application root          /home/mencio/Software/testme
Environment               development
Database adapter          sqlite3
Database schema version   20220116201051

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@jonathanhefner great yak shaving in that ticket!

The problem still persists with 7.0. Nothing changed.