-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveRecord connection sharing: connection is returned to the pool #317
Comments
Activerecord pool may close connections in e.g. Closed connection state is distinct from just disconnected (i.e. by TCP connection reset). Closed connection can't be reconnected by
That's why once this error happens, it persists for the lifetime of |
Note that it only causes problems with |
Seems that just replacing It decides to recover connection back if thread that checked out the connection is no longer alive. In queue_classic default queue's connection is effectively global:
So, when AR pool reaps connections, if thread in which |
Probably better way to fix this would be to leave This will work for non-activerecord connections too, as conn adapter is already memoized in thread-local in (However, for ActiveRecord connections, |
When ActiveRecord integration is enabled,
ConnAdapter
is constructed with connection taken from ActiveRecord pool:queue_classic/lib/queue_classic.rb
Line 57 in 4260d89
However, according to ActiveRecord documentation, it's not a correct way to do it, as the connection will be returned to the pool by Action Pack at the end of request:
As
ConnAdapter
is not going to release its connection,ActiveRecord::Base.connection_pool.checkout
should be used.This might cause
PG::ConnectionBad: connection is closed
problem described here: #302 (comment), which I have regularly too. (Also there were old issues from the time ActiveRecord integration was not supported: #141, #134, #96). Pool may close connections returned to it, and it may close connection that appears to be shared betweenQC::ConnAdapter
and the pool after that accidental connection release.Proposed change is:
However, I don't know yet how to make reproducible test case, as it involves pool state.
The text was updated successfully, but these errors were encountered: