Skip to content

feat: keepalive query on the LISTEN connection - #87

Merged
danfimov merged 7 commits into
mainfrom
feat/listen-keepalive
Nov 25, 2026
Merged

danfimov merged 7 commits into
mainfrom
feat/listen-keepalive

Conversation

@danfimov

@danfimov danfimov commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

A LISTEN connection only receives data. Two things went wrong because of that:

  • proxies (pgbouncer, HAProxy) and idle_session_timeout close it as idle. They do this over and over, because notifications do not reset the idle timer;
  • a connection that hangs without being closed (network partition) was never noticed. Messages were delayed or lost until it recovered.

Adds listen_keepalive_interval (default 30 s, None disables) and listen_keepalive_timeout (default 5 s). Every interval, regardless of traffic, the broker runs SELECT 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:

  • asyncpg waits on its disconnect event with a timeout;
  • psycopg reads notifies(timeout=...) in slices with the query in between. Notifications between slices are not lost;
  • psqlpy and aiopg run the query next to the notification loop (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_timeout with and without keepalive, a psycopg query that ignores cancellation, and parameter validation.

Stress harness, keepalive 1 s:

fault before after
idle_session_timeout=2s for 15 s 18 reconnects per driver 0 extra reconnects
LISTEN backends frozen (SIGSTOP) for 30 s hang never detected, all messages delayed 20–30 s detected after ~6 s

Losses 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:

  • test: the hung-keepalive test kicks until the restored listener receives a message. A single NOTIFY could arrive before the new LISTEN, which made the test flaky on CI.

@danfimov
danfimov added this pull request to stack #93 November 25, 2026 19:38
@greptile-apps

greptile-apps Bot commented Nov 25, 2026

Copy link
Copy Markdown

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The new keepalive query is a hard-coded SQL string, so no untrusted input reaches a query.
  • All four drivers skip the keepalive when the LISTEN connection is caller-supplied, so the broker never queries a connection it does not own.
  • The new listen_keepalive_interval and listen_keepalive_timeout parameters are developer config and are validated to be positive or None.
  • A keepalive failure always fails closed: every driver treats the connection as lost and reconnects rather than continuing.
  • run_until_first_done and the abandoned psycopg keepalive task cancel and clean up their child tasks, so no orphaned work or data leaks between connections.

@danfimov
danfimov force-pushed the feat/listen-keepalive branch 4 times, most recently from 22a8737 to 7700c5a Compare November 25, 2026 22:26
Base automatically changed from feat/aiopg-broker to main November 25, 2026 22:30
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
danfimov force-pushed the feat/listen-keepalive branch from 7700c5a to 75e0c43 Compare November 25, 2026 22:30
@danfimov danfimov self-assigned this Nov 25, 2026
@danfimov
danfimov merged commit 62a53a4 into main Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant