Skip to content

fix: keep and cancel delayed NOTIFY tasks in the asyncpg and psqlpy brokers - #94

Merged
danfimov merged 1 commit into
mainfrom
fix/track-delayed-notify-tasks
Nov 25, 2026
Merged

danfimov merged 1 commit into
mainfrom
fix/track-delayed-notify-tasks

Conversation

@danfimov

Copy link
Copy Markdown
Owner

The asyncpg and psqlpy brokers started the task that sends a delayed NOTIFY with asyncio.create_task() and kept no reference to it. That has two effects:

  • asyncio keeps only a weak reference to a task, so the task could be garbage collected while it waits. Its NOTIFY was then never sent, and the message waited for the next poll (up to poll_interval);
  • shutdown() did not cancel it. When a producer shuts down its broker and keeps running (an app reload, tests, the stress harness's producer restart), the psqlpy task woke up after the delay on the closed pool. It logged Task exception was never retrieved … BaseConnectionPoolError: Pool has been closed, once per pending delayed message. asyncpg checks the pool after the delay, so it did not log anything.

Both brokers now keep these tasks and cancel them on shutdown, like the psycopg and aiopg brokers already do. The message itself is not lost either way: workers poll for delayed messages whose delay has passed.

Test: kick a delayed message, shut the broker down, wait past the delay, and check that no unhandled task error was reported. Runs for all four drivers; it fails for psqlpy without the fix.

Found by the stress harness on main (producer restart with 20 pending delayed kicks: 21 such errors per run).

…rokers

The tasks were created without a reference, so they could be garbage collected while waiting,
and shutdown() did not cancel them. A psqlpy producer that shut down its broker and kept running
logged 'Task exception was never retrieved ... Pool has been closed' for every pending delayed message.
Both brokers now track these tasks and cancel them on shutdown, like the psycopg and aiopg brokers.
@danfimov danfimov self-assigned this Nov 25, 2026
@danfimov
danfimov merged commit f7f816d into main Nov 25, 2026
13 checks passed
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