Skip to content

feat(consumer): count unfinished deliveries and release claims by owner - #9

Merged
muhammetsafak merged 3 commits into
mainfrom
feat/queue-ownership-and-abandoned-attempts
Nov 28, 2026
Merged

muhammetsafak merged 3 commits into
mainfrom
feat/queue-ownership-and-abandoned-attempts

Conversation

@muhammetsafak

Copy link
Copy Markdown
Contributor

What

  • Attempts for a handler that kills its process. A handler killed by OOM, exit or SIGKILL never reports a failure, so its message came back with the same attempts for ever. Transports now count a delivery that never ended in an ack, release or dead-letter when they hand the message out again; the Worker dead-letters it (reason failed) once attempts reaches maxAttempts, without running the handler again.
    • PDO: taking over a lapsed reservation raises attempts (column and envelope) in the same UPDATE. No schema change.
    • Redis: reserve() records deliveries in a <queue>:started sorted set (SHA-1 of the body) that ack/release/deadLetter clear. A body that returns with its entry still there is counted, in the body and the processing entry. Body format unchanged; a client without the sorted-set commands switches counting off.
    • RabbitMQ: the broker's redelivered flag counts one attempt. A classic queue keeps no delivery count, so repeated crashes are not bounded there (documented).
    • Waiting on an in-flight key is a release and is never counted.
  • Claim ownership. New optional OwnershipAwareIdempotencyStore (claimOwned() / forgetOwned()), used by the Dispatcher when the store implements it, so a handler that outlived its lease cannot delete the claim or completed record of the worker that took over. IdempotencyStore::forget() is unchanged; stores without the interface behave as before. InMemoryIdempotencyStore implements it.

Backward compatible (Queue 2.4.0 candidate): no signature of IdempotencyStore, ConsumerTransport or Handler changed, no body format change. ReceivedMessage and RedisTransport gain a new optional last constructor argument.

Tests

  • (a) two workers, W1 overruns its lease and throws: W2's claim and completed record stay.
  • (b) a handler whose process keeps dying reaches jobs_failed (PDO) / :failed (Redis, in-memory and real-Redis integration); AMQP redelivered flag; MySQL and RabbitMQ integration cases added.
  • (c) a legacy store with only forget() works unchanged.
  • (d) PDO in_flight waiting repeated past maxAttempts consumes no attempt and never dead-letters.

Docs: Retries & Dead-Letters, Idempotent Consumption (claim ownership, upgrading from 2.3), Transport PDO/Redis/RabbitMQ, API Reference, README, CHANGELOG.

A handler that kills its process (OOM, exit, SIGKILL) never reports a failure,
so its message came back with the same attempts for ever. The transports now
count a delivery that never ended in an ack, release or dead-letter when they
hand the message out again, and the worker dead-letters it at maxAttempts.

- PDO: taking over a lapsed reservation raises attempts (column and envelope)
  in the same UPDATE, so one worker counts it; no schema change.
- Redis: reserve() records deliveries in a <queue>:started sorted set that
  ack/release/deadLetter clear; a body that returns with its entry still there
  is counted in the body and the processing entry. Body format is unchanged.
- AMQP: the broker's redelivered flag counts one attempt.
- Waiting on an in-flight key is a release and is never counted.

Add the optional OwnershipAwareIdempotencyStore (claimOwned/forgetOwned) so a
handler that outlived its lease cannot delete the claim or the completed record
of the worker that took over. Stores without it keep claim() and forget().
An abandoned message that reached maxAttempts was dead-lettered before the
idempotency store was read, so a message whose handler had finished (only the
ack was lost) landed in the failed table, and one whose key another handler
still held was failed instead of waiting. The dispatcher now drops it as a
duplicate when the key is recorded and releases it as in_flight when the key is
still claimed; only a message with neither is dead-lettered.

RedisTransport counting no longer fails a reservation: a NOPERM or unknown
command refusal of ZREMRANGEBYSCORE or LINSERT switches the counting off, and
any other failure while counting hands the message out uncounted instead of
leaving it on the processing list.
…a claim

An exhausted message whose key was still claimed was released as in_flight,
which clears the transport's unfinished-delivery marker. When it came back it
was no longer abandoned, so the handler ran again; with a lease longer than the
transport's redelivery time a handler that kills its process every time never
reached the dead-letter queue.

The worker now treats any message that arrives with attempts at or above
maxAttempts and no dead_letter block as exhausted, since only the count can
bring a live message there. A replayed dead letter keeps its block and runs as
before. A store that cannot tell a recorded key from a claimed one now
dead-letters an exhausted message instead of discarding it.
@muhammetsafak
muhammetsafak merged commit 69d97eb into main Nov 28, 2026
10 checks passed
@muhammetsafak
muhammetsafak deleted the feat/queue-ownership-and-abandoned-attempts branch November 28, 2026 09:30
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