You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Upon starting the web server, we do the following two things:
db = asyncpg.create_pool to create the connection pool for the application's DB connections.
conn = await db.pool.acquire(); await conn.add_listener('foo', _callback) to register long-lived listeners to catch and handle NOTIFY events from the DB.
Recently I restarted the Postgres server and noticed something interesting. First, connections to the DB using the db pool object saw no interruption and were still able to connect to the DB server despite it having a new PID (since the process had been restarted).
However, the conn the application relied on to receive async notifications from the server was dead/closed and so the application logic dependent upon receiving those notifications was not functioning. (This is how I reproduced this issue and discovered the cause to have been a PG server restart.)
Why is the pool unaffected by restart? It makes sense the connection instance is dead since the socket is closed after restarting the server, but I'm a bit surprised the pool is not affected.
I suppose the best strategy for checking for dead DB connections would be to simply check conn.is_closed periodically - or, is there some "on connection close" callback I can define to retry/reopen a connection (since obtaining new connections from the pool still works after restart)?
The pool is designed to handle connections being closed and to reconnect automatically as long as that is possible to do using the original connection parameters.
The closest thing to being able to handle "on close" on individual connections currently is to subclass Connection and overload the _cleanup() method. An explicit user-specified callback would arguably be nicer, of course, so I won't oppose a PR adding it (it can be one of the things _cleanup() calls).
Activity
elprans commentedon Mar 22, 2019
The pool is designed to handle connections being closed and to reconnect automatically as long as that is possible to do using the original connection parameters.
The closest thing to being able to handle "on close" on individual connections currently is to subclass
Connectionand overload the_cleanup()method. An explicit user-specified callback would arguably be nicer, of course, so I won't oppose a PR adding it (it can be one of the things_cleanup()calls).ioistired commentedon Dec 27, 2019
it would seem that even
_cleanupis not called when the connection is closed externally. I have implemented support for custom callbacks that are run by_cleanup: https://github.com/iomintz/asyncpg/tree/on_closeA demo of it in use:
As you can see, _cleanup was not called until I manually ran
terminate()even though the connection itself was closed.So this does not seem to be a way to handle "on close".