Skip to content

feat: optional at-least-once delivery with processing_timeout - #89

Merged
danfimov merged 5 commits into
mainfrom
feat/processing-timeout
Nov 25, 2026
Merged

danfimov merged 5 commits into
mainfrom
feat/processing-timeout

Conversation

@danfimov

@danfimov danfimov commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

A claimed message whose worker died before acknowledging it stayed processing forever. This happens on kill -9, when a claim's reply is lost in a database restart, or when an ack cannot reach the database. max_retry_attempts was not used anywhere.

Adds processing_timeout. The default None keeps at-most-once delivery, as before. When it is set:

  • on each poll, a message claimed longer ago than the timeout goes back to pending, up to max_retry_attempts claims;
  • after that it becomes failed, and a warning is logged.

Claims record claimed_at and attempts in two new columns, which startup adds to existing tables (instant ADD COLUMN). Rows stuck before the upgrade are timed from created_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 MessageRow gets the new columns as optional fields, so a table migrated by another broker keeps working.

Rolling upgrade: older PSQLPyBroker workers read claimed rows with RETURNING * into a fixed dataclass. They fail on a migrated table, and those rows stay processing. 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 stay processing. 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:

  • fix (Greptile): an ack deletes the row only while it still has the attempts value 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 of RETURNING *.

@danfimov
danfimov added this pull request to stack #93 November 25, 2026 19:38
WHERE status = 'processing'
AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1)
AND attempts < $2
"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 security 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.

@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 release and fail queries pass only developer-config values (processing_timeout, max_retry_attempts) as bind parameters, so task payloads and labels cannot change the SQL.
  • Redelivery is opt-in: the default processing_timeout=None keeps the old at-most-once behavior for existing users.
  • The startup migration runs inside the existing advisory-locked schema transaction and only adds two columns and an optional index.
  • The exhausted-messages warning logs only ids and task names, not message payloads or results.

Findings

  • P2 Stale ack deletes re-claimed message ▶

@danfimov
danfimov force-pushed the feat/processing-timeout branch 3 times, most recently from c40cc79 to 9496154 Compare November 25, 2026 22:14
@danfimov
danfimov force-pushed the feat/processing-timeout branch 3 times, most recently from 28caa13 to b148f84 Compare November 25, 2026 22:30
Base automatically changed from feat/poll-pending to main November 25, 2026 22:39
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 *.
@danfimov
danfimov force-pushed the feat/processing-timeout branch from b148f84 to 92459be Compare November 25, 2026 22:39
@danfimov danfimov self-assigned this Nov 25, 2026
@danfimov
danfimov merged commit 95f349f 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