Repository navigation
feat: keepalive query on the LISTEN connection - #87
Merged
Merged
Conversation
danfimov
force-pushed
the
feat/listen-keepalive
branch
4 times, most recently
from
November 25, 2026 22:26
22a8737 to
7700c5a
Compare
A LISTEN connection only receives data, so proxies and idle_session_timeout close it as idle, and a connection that hangs without being closed is never noticed: the worker silently stops receiving messages. Add listen_keepalive_interval (default 30 s, None disables) and listen_keepalive_timeout (default 5 s) to the brokers. The asyncpg broker runs a recognizable query on its own LISTEN connection every interval; if the query does not answer in time, the connection is terminated and restored. A connection supplied by the caller is not queried.
Read the broker-owned LISTEN connection in slices of listen_keepalive_interval seconds (notifies(timeout=...)) and run the keepalive query in between. A query that does not answer within listen_keepalive_timeout is treated as a lost connection and the listener reconnects. Notifications that arrive between the slices are not lost.
Run the keepalive query on the listener connection next to the notification loop (run_until_first_done); if it does not answer within listen_keepalive_timeout, the listener is replaced like after a connection loss. psqlpy executes queries on listener.connection while it is iterated, so no notifications are lost.
Run the keepalive query on the broker-owned LISTEN connection next to the notification loop; a query that does not answer within listen_keepalive_timeout is treated as a lost connection. A connection supplied by the caller is not queried.
…celled psycopg waits for the server to acknowledge a cancelled query, so wait_for(keepalive, timeout) on a hung connection only returned when the connection came back: on the stress harness the hang was not detected for its whole 30 seconds. Leave the unanswered query running, treat the connection as lost right after the timeout, and cancel the query once the connection is closed.
The replacement LISTEN connection may not be listening yet when the hung one is gone, so a single NOTIFY could be dropped and the test failed on CI (asyncpg, Python 3.14, PostgreSQL 18).
danfimov
force-pushed
the
feat/listen-keepalive
branch
from
November 25, 2026 22:30
7700c5a to
75e0c43
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A LISTEN connection only receives data. Two things went wrong because of that:
idle_session_timeoutclose it as idle. They do this over and over, because notifications do not reset the idle timer;Adds
listen_keepalive_interval(default 30 s,Nonedisables) andlisten_keepalive_timeout(default 5 s). Every interval, regardless of traffic, the broker runsSELECT 1 /* taskiq-pg LISTEN keepalive */on its own LISTEN connection. If the query does not answer in time, the connection is treated as lost and restored. Connections supplied by the caller are not queried.One commit per driver:
notifies(timeout=...)in slices with the query in between. Notifications between slices are not lost;run_until_first_done).One more commit: psycopg waits for the server to acknowledge a cancelled query, so
wait_for(query, timeout)on a hung connection only returned when the connection came back. The unanswered query is now abandoned and cancelled after the connection is closed.Tests cover a hung keepalive query (all drivers), the server's
idle_session_timeoutwith and without keepalive, a psycopg query that ignores cancellation, and parameter validation.Stress harness, keepalive 1 s:
idle_session_timeout=2sfor 15 sLosses inside the detection window remain until the next PR adds polling.
Part of a stack; review and merge bottom-up. Stress-harness numbers are from a local run: 4 drivers, ~250–550 kicks per scenario, 2 worker processes per driver.
Review follow-ups: