Repository navigation
feat: optional at-least-once delivery with processing_timeout - #89
Conversation
| WHERE status = 'processing' | ||
| AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1) | ||
| AND attempts < $2 | ||
| """ |
There was a problem hiding this comment.
Stale ack deletes re-claimed message
Stale ack deletes re-claimed message. With processing_timeout set, this query puts a timed-out processing row back to pending, and another worker can then re-claim it. But the ack is still DELETE_MESSAGE_QUERY (DELETE FROM {table} WHERE id = $1), with no check that the acking worker still owns the claim. So a slow worker whose claim expired can delete the row out from under the worker that just re-claimed it. Example: worker A claims message M, its ack is delayed past the timeout (slow task, GC pause, or a timeout shorter than the task); worker B's poll releases M and re-claims it; A finishes and its ack deletes M's row; B is then killed while still processing — M is gone and never redelivered. The at-least-once guarantee this PR adds silently turns back into at-most-once, which is exactly the loss this feature exists to prevent. The same race exists in the psycopg, psqlpy, and aiopg drivers, whose acks also delete by id only. Guarding the delete (for example ... AND status = 'processing' AND attempts = <the claim's attempts>) would stop an expired claim from removing a newer one.
|
c40cc79 to
9496154
Compare
28caa13 to
b148f84
Compare
A claimed message whose worker died before acknowledging it (kill -9, or a claim whose reply was lost in a database restart) stayed in 'processing' forever, and max_retry_attempts was not used at all. Add processing_timeout (default None: at-most-once, as before). When set, a message claimed longer ago than the timeout is returned to the queue on the next poll, up to max_retry_attempts claims; after that it is marked as 'failed' and a warning is logged. Claims record claimed_at and count attempts in two new columns, which startup adds to existing tables; rows stuck before the upgrade are timed from created_at. Delivery can then repeat, so tasks must be idempotent and the timeout longer than the longest task. The psqlpy MessageRow gets the new columns as optional fields, so psqlpy brokers keep reading a table already migrated by another broker.
Same as for asyncpg: claims record claimed_at and attempts, startup adds the columns, and with processing_timeout set each poll returns stale claimed messages to the queue or marks exhausted ones as failed. A database error while doing so is logged and retried on the next poll.
Same as for asyncpg: claims record claimed_at and attempts, startup adds the columns, and with processing_timeout set each poll returns stale claimed messages to the queue or marks exhausted ones as failed.
Same as for asyncpg: claims record claimed_at and attempts, startup adds the columns, and with processing_timeout set each poll returns stale claimed messages to the queue or marks exhausted ones as failed.
With processing_timeout, a message whose claim expired can be claimed again by another worker. The ack deleted the row by id only, so a slow worker finishing late removed the new claim, and the message was lost if the second worker then died. The ack now deletes the row only while it still has the attempts count of its own claim. Claims return the attempts count; psycopg claims return explicit columns instead of RETURNING *.
b148f84 to
92459be
Compare
A claimed message whose worker died before acknowledging it stayed
processingforever. This happens onkill -9, when a claim's reply is lost in a database restart, or when an ack cannot reach the database.max_retry_attemptswas not used anywhere.Adds
processing_timeout. The defaultNonekeeps at-most-once delivery, as before. When it is set:pending, up tomax_retry_attemptsclaims;failed, and a warning is logged.Claims record
claimed_atandattemptsin two new columns, which startup adds to existing tables (instantADD COLUMN). Rows stuck before the upgrade are timed fromcreated_at. A partial index on processing rows is created only when the option is set. Delivery can repeat, so tasks must be idempotent and the timeout must be longer than the longest task.One commit per driver. The psqlpy
MessageRowgets the new columns as optional fields, so a table migrated by another broker keeps working.Rolling upgrade: older
PSQLPyBrokerworkers read claimed rows withRETURNING *into a fixed dataclass. They fail on a migrated table, and those rows stayprocessing. Stop the old psqlpy workers before starting new ones.Stress harness with
processing_timeout=3: worker SIGKILL, PostgreSQL restart and exhausted connection slots → 0 lost and 0 stuck rows for all drivers. Without it, 1–6 rows stayprocessing. There are 1–7 duplicates per scenario: messages whose ack never reached the database.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:
attemptsvalue of its own claim. A slow worker whose claim expired can no longer delete a message that another worker claimed again. psycopg claims now return explicit columns instead ofRETURNING *.