Repository navigation
feat: poll for pending messages whose NOTIFY was missed - #88
Merged
Merged
Conversation
|
danfimov
force-pushed
the
feat/poll-pending
branch
3 times, most recently
from
November 25, 2026 22:14
62c63b5 to
97fab98
Compare
danfimov
force-pushed
the
feat/poll-pending
branch
2 times, most recently
from
November 25, 2026 22:30
616a791 to
d86ed7d
Compare
A NOTIFY nobody is listening to is lost, and its message stayed pending forever: messages kicked while no worker was running (e.g. during a deploy) or while a worker was reconnecting, and delayed messages whose producer exited before sending the delayed NOTIFY. Add poll_interval (default 5 s) to the brokers. The asyncpg worker claims the oldest ready pending message (FOR UPDATE SKIP LOCKED, so workers never claim the same row) right after it starts listening, after reconnecting and every poll_interval, draining a backlog without waiting between messages. A delayed message is ready once created_at + delay has passed. NOTIFY stays the fast path. A partial index on pending messages is created on startup.
The notification loop now also yields a poll marker: right after start and after every reconnect, then every poll_interval. The connection is read in slices up to the next poll or keepalive query (notifies(timeout=...)), and listen() drains every ready pending message when the marker arrives. A database error while polling is logged and retried on the next poll instead of ending listen().
Same as for asyncpg: the worker claims ready pending messages right after it starts listening, after the listener is restored and every poll_interval; a partial index on pending messages is created on startup.
Same as for the other brokers: the worker claims ready pending messages right after it starts listening, after the LISTEN connection is restored and every poll_interval; a partial index on pending messages is created on startup.
With polling, a listening broker also claims pending rows left in the shared taskiq_messages table by other tests, so tests that expect a specific message got someone else's.
kick() with a delay label waited for the whole delay before returning, because the delayed NOTIFY was the only way the message could reach a worker. Workers now poll for messages whose delay has elapsed, so the NOTIFY is sent from a background task like in the other brokers, and pending ones are cancelled on shutdown.
Worker processes of one deployment start together, and concurrent CREATE TABLE / CREATE INDEX IF NOT EXISTS on a new table fail with a unique violation in the system catalogs, so some of them failed to start (taskiq restarted them). The race existed for the table before; the pending index made it more likely. Create the table and its indexes in one transaction that first takes an advisory lock derived from the table name, so brokers wait for each other.
Eight asyncpg brokers with the default pool (10 connections each) exhausted the CI server's connection slots.
The poll query cast every pending row's delay label to a number, so one label that could not be cast
("abc") or overflowed make_interval made the query fail on every poll. Polling stopped for all messages
behind that row, and a psycopg worker stopped receiving messages at all.
A label that is not a plain non-negative number now counts as no delay; an absurdly large one is never ready.
kick() inserted the message before int(delay) raised, leaving a row the caller believed was not sent; polling would now execute it.
danfimov
force-pushed
the
feat/poll-pending
branch
from
November 25, 2026 22:30
d86ed7d to
7cbe622
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.
PostgreSQL drops a NOTIFY nobody is listening to, so its message stayed
pendingforever. That happened to:Adds
poll_interval(default 5 s). A listening worker claims the oldest ready pending message right after it starts listening, after reconnecting, and everypoll_interval.UPDATE ... WHERE id = (SELECT ... FOR UPDATE SKIP LOCKED LIMIT 1), so workers never claim the same row, and a backlog is drained without waiting between messages.created_at + delayhas passed.Commits:
kick()no longer waits for the delay of a delayed message. This was deliberately left out of fix(psycopg): commit delayed message before sending its NOTIFY #76 until polling made it safe;CREATE TABLE/INDEX IF NOT EXISTSfail with a unique violation inpg_class/pg_type. The stress harness caught workers failing to start. The table race existed before; the index made it more likely. Setup now runs in one transaction that first takespg_advisory_xact_lock.Behaviour change after upgrading: rows already stuck in
pendingare executed right after the first start.Stress harness:
Note: until #90 is merged, startup needs ownership of the table for
CREATE INDEX IF NOT EXISTS.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:
max_connections.delaylabel is not a plain number no longer breaks the poll query for every row behind it. Such a label counts as no delay, and an absurdly large one is never ready.kick()parses thedelaylabel before inserting, so a malformed label no longer leaves a row that polling would execute.