Skip to content

fix(drivers): close two LISTEN races, steady recovery tests under load - #51

Merged
z22092 merged 1 commit into
mainfrom
fix/listen-recovery-margins
Nov 23, 2026
Merged

z22092 merged 1 commit into
mainfrom
fix/listen-recovery-margins

Conversation

@z22092

@z22092 z22092 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

What and why

On a busy machine, with other test suites running beside them, the LISTEN recovery tests from #47 and some delay tests from #45 failed now and then. Two of the causes were in the drivers, one in asyncpg, one in a test, and the rest were margins.

  • psycopg: an abandoned answer's error went unread. shutdown() cancels the listen reader, and the end of listen() cancels it again. The second cancellation can stop _give_up() before it gathers the task it gave up, so asyncio logs Task exception was never retrieved with that task's OperationalError, and the anyio pytest runner fails the test. _answered_within() now reads the task's outcome in a done-callback. The same path serves health checks, claims and the startup LISTEN. The bug came with fix(broker): reopen LISTEN and bound waits on silent connections #47 and was never released, so it has no CHANGELOG entry.
  • psqlpy: a NOTIFY right after the LISTEN connection opened could be lost. psqlpy 0.13.1 sends LISTEN only on the listener's first __anext__, not in startup(). A NOTIFY sent between the two reached no listener, and its message waited for the next sweep. PsqlpyListenDriver.on_startup() now runs LISTEN on the listener's connection, bounded by query_timeout, before it returns, so listen() logs reopened only once LISTEN is in effect. ListenDriver.on_startup() documents this as the contract for every driver. Reported upstream as psqlpy-python/psqlpy#179.
  • asyncpg: a cancelled connect() leaves an unread error (MagicStack/asyncpg#1397, fix proposed in Do not leak the SSL upgrade future when connect() is cancelled early MagicStack/asyncpg#1398). With the default sslmode=prefer, a cancellation inside create_connection() leaves ConnectionError('unexpected connection_lost() call') on a future nothing awaits. The recovery tests cancel reopens on purpose, so their asyncpg brokers use sslmode=allow through tests/helpers.with_sslmode_allow(), unless the DSN or PGSSLMODE names a mode. allow connects without the SSLRequest exchange and still moves to TLS when the server requires it.
  • A race in test_when_the_pool_goes_silent__then_listen_keeps_its_pace[asyncpg]. The silence can land after a sweep's claim got its answer, while asyncpg resets the connection for the pool. Then the release is what gets no answer, and only Releasing a connection to the pool failed is logged. The test now accepts either line as the sign that the silent connection was given up, and its responsiveness check now runs for QUERY_TIMEOUT + 1 s instead of a fixed 2 s.
  • Margins. QUERY_TIMEOUT in the recovery tests goes from 0.5 to 2.0 s, because a connect or a claim on a healthy server took longer than 0.5 s under load, and SLACK grows with it. NOTIFY_DELIVERS_WITHIN now lives once in tests/helpers.py at 3.0 s. The delay test that checks NOTIFY latency had its own 0.5 s copy and measured 0.607 s under load; its 5 s wait still rules out the sweep, which is a minute away.

How it was tested

  • New tests, red before the fix and green after it:
    • test_when_a_health_check_is_cancelled_twice__then_no_error_is_left_unread[psycopg] cancels the reader twice during a health check. Before the fix: Task exception was never retrieved: OperationalError('consuming input failed: server closed the connection unexpectedly ...').
    • test_when_on_startup_returns__then_a_notify_sent_at_once_is_read sends the NOTIFY from a blocking connection right after on_startup(), so the event loop runs nothing of the listener first. Before the fix it failed for psqlpy only; asyncpg and psycopg passed.
  • psqlpy on its own (0.13.1, PostgreSQL 17.11): right after startup(), pg_stat_activity shows no LISTEN for the listener; after the first __anext__ it shows LISTEN <channel>;. A NOTIFY sent before the first __anext__ was delivered 0 of 5 times, and 5 of 5 with an explicit LISTEN on listener.connection.
  • asyncpg on its own (0.32.0): cancelling connect() after 1 to 5 loop steps against a server that never answers left 2 unread ConnectionErrors with sslmode=prefer, and none with allow or disable.
  • Mutations, each turning the tests red:
    • no done-callback;
    • no early psqlpy LISTEN;
    • shutdown() not cancelling the reader;
    • a reopen without its query_timeout bound;
    • waits reset after a short-lived connection;
    • no reopen (9 failed);
    • psycopg cancelling instead of closing;
    • asyncpg without the acquire timeout.
  • Under load (8 cores, 24 busy loops beside the tests, other suites running), with only the psycopg fix in: 3 of 8 rounds of the silent-pool, silent-LISTEN and health-check tests failed, one for each of the other causes above.
  • After the change, 10 rounds of the recovery, listen-driver and delay tests under load: 7 rounds with 24 busy loops and other suites running, before the review fixes, and 3 rounds with 12 busy loops on the final change. Every recovery and listen-driver test passed in every round. In tests/test_delay.py, a psycopg claim stalled past that test's own wait twice, once at 3 s and once at 6 s, in different rounds; the cause is not known yet, and this PR does not change those two tests. One round measured the NOTIFY latency at 0.607 s, which led to the 3.0 s bound.
  • Full suite on this change before the rebase onto feat: add SQLAlchemy, SQLModel and Alembic integrations #50 (main 8b2c83b): 720 passed, 9 skipped on Python 3.13 with PostgreSQL 18, the same with PostgreSQL 17 (TEST_POSTGRES_MAJOR=17), and the same on Python 3.10 with make test-lowest. Coverage 97.80% (gate 94%). After the rebase, pre-commit run --all-files passes, the four touched test modules pass (203 passed, 9 skipped), and CI runs the full matrix on this head. Snyk Code reports no issue in the files this PR changes.

Checklist

  • pre-commit run --all-files passes
  • Tests cover the change (the driver fixture runs each one against all three drivers)
  • CHANGELOG.md has an entry under ## [Unreleased], or the change is not user-visible
  • README and docstrings are updated if behaviour or public arguments changed

🤖 Generated with Claude Code

Under CPU load the LISTEN recovery tests and two delay tests failed now
and then. Two of the causes were in the drivers:

- psycopg: shutdown() cancels the listen reader and the end of listen()
  cancels it again; the second cancellation can stop _give_up() before
  it gathers the task it gave up, and asyncio then logs "Task exception
  was never retrieved". A done-callback now reads the task's outcome.
- psqlpy: psqlpy sends LISTEN only on the listener's first __anext__,
  so a NOTIFY sent right after on_startup() could be lost (reported as
  psqlpy-python/psqlpy#179). on_startup() now runs LISTEN on the
  listener's connection itself, bounded by query_timeout, and
  ListenDriver.on_startup() documents that contract.

The tests: asyncpg brokers in the recovery tests connect with
sslmode=allow (tests/helpers.with_sslmode_allow), because a connect()
cancelled during the SSLRequest of sslmode=prefer leaves an unread
error (MagicStack/asyncpg#1397); the silent-pool test accepts a
given-up release as well as a given-up claim; QUERY_TIMEOUT goes from
0.5 to 2.0 s, and NOTIFY_DELIVERS_WITHIN lives once in tests/helpers.py
at 3.0 s, the delay test's 0.5 s copy having been too short under load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@z22092
z22092 marked this pull request as ready for review November 23, 2026 16:44
@z22092
z22092 merged commit b1c1128 into main Nov 23, 2026
20 checks passed
@z22092
z22092 deleted the fix/listen-recovery-margins branch November 26, 2026 16:03
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