After updating a Rails app from RestfulAuthentication and Rails 2.3 to Devise and Rails 5.0, we are getting strange "401 Unauthorized" errors, and the rails application hangs at the first request:
Completed 401 Unauthorized in x ms
where x is a very high number. The error occurs only if the server is started the first time (after a long period of inactivity). The application just hangs and refuses to start, and if we interrupt the server the 401 Unauthorized errors appear. It does not seem to matter if we use Webrick, Puma, Thin or Unicorn. The ApplicationController
looks like
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_http_headers
alias login_required authenticate_user!
...
(the alias was added to guarantee a smooth transition to Devise). The database.yml config file looks like
development:
adapter: mysql2
encoding: utf8
database: db_name
pool: 5
timeout: 5000
username: db_user
passwort:
Once the application is running the errors disappear, but if we restart the computer after a few hours or in the next day, the error appears again.
Occasionally there are also Timeout errors like
ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.003 seconds); all pooled connections were in use
or
No live threads left. Deadlock? (fatal)
The related Devise issues (here and here) are not helpful, and none of the related questions provided a solution, neither ActiveRecord::Base.clear_active_connections!
helps, as proposed here nor Session.delete_all
(the session is stored in ActiveRecord). Help! Any ideas?