Skip to content

Fix recovery of expired message reservations - #33

Open
avlyubimov wants to merge 2 commits into
yiisoft:masterfrom
avlyubimov:fix-reservation-expiration
Open

avlyubimov wants to merge 2 commits into
yiisoft:masterfrom
avlyubimov:fix-reservation-expiration

Conversation

@avlyubimov

@avlyubimov avlyubimov commented Nov 9, 2026 •

Copy link
Copy Markdown
Q A
Is bugfix? ✔️
New feature? ✔️ Optional reservation timeout
Breaks BC? ❌

What does this PR do?

When a consumer exits after reserve() and before delete(), the message currently remains stranded: reserve() calls zRem() instead of recording an expiration deadline. The recovery code therefore has no expired reservation to return to the waiting list.

This change:

  • Records a deadline in the reserved sorted set, using a positive, optional reservationTimeout constructor argument (300 seconds by default). Existing constructor calls remain valid.
  • Determines reserved status from the reservation set rather than the attempt counter, so a recovered message can be reported as waiting while its attempt history is retained.
  • Preserves the timeout in withChannelName() and documents the distinction between the reservation timeout and the blocking read timeout.
  • Rechecks each selected entry's deadline and atomically removes and requeues it in Lua. A stale mover cannot requeue a message that another consumer has already recovered and freshly reserved.
  • Uses a unique mover-lock token and atomically compares it before deleting the lock. A consumer that did not acquire the lock, or whose lock has expired, cannot release another mover's lock.

Validation

  • The original reservation regression tests fail on the base implementation (five failures). The added concurrency regression tests fail on the previous PR revision (four failures).
  • All 36 PHPUnit tests pass, with 117 assertions, on PHP 8.1.34 and PHP 8.3.36 with Redis 3.
  • Psalm targeting PHP 8.1, Composer Dependency Analyser, scoped Rector, and PHP CS Fixer using the package configuration pass.
  • Tests cover deadline registration, redelivery after a consumer disconnects without acknowledging, exclusion of unexpired reservations, status after recovery, acknowledgement cleanup, timeout validation, and channel changes. Deterministic interleaving tests also cover stale recovery snapshots for both delayed and reserved entries, lock-acquisition failure, successor lock ownership, and lock cleanup after an exception.

The existing GitHub Docker build fails during Composer resolution of the development dependency set, before PHPUnit runs. Local validation used an isolated PHP 8.1/Redis setup with Infection omitted from the temporary test image, plus a PHP 8.3 image with pcntl for the consumer tests. This PR does not change composer.json or the package test infrastructure; mutation testing was not run.

Scope

The reservation timeout must exceed the expected processing time; this change does not add a heartbeat or change acknowledgement semantics. It does not make the existing pop/deadline-registration sequence atomic, and messages stranded by older versions without a deadline are not repaired automatically.

Summary by Sourcery

Enable reliable redelivery of unacknowledged messages by tracking reservation expiration deadlines and recovering expired reservations safely.

New Features:

  • Add configurable reservation timeouts so unacknowledged messages can be redelivered after expiration.

Bug Fixes:

  • Recover expired message reservations instead of leaving them stranded after a consumer exits before acknowledgement.
  • Preserve message attempt history while correctly reporting recovered messages as waiting or reserved.

Enhancements:

  • Make reservation recovery and mover-lock cleanup safe against concurrent consumers and stale lock owners.

Documentation:

  • Document reservation timeout configuration and its distinction from the blocking read timeout.

Tests:

  • Add coverage for reservation deadlines, redelivery, status transitions, timeout validation, acknowledgement cleanup, channel changes, and concurrent recovery behavior.

@sourcery-ai

sourcery-ai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Use the reserved sorted set to represent active reservations and their expiration deadlines, enabling expired entries to be recovered without requeueing a reservation that has since changed.

  • src/QueueProvider.php L194-207 (+3 small edits)

API

Add a positive, configurable reservation-timeout constructor argument with a 300-second default, preserving existing constructor calls.

  • src/QueueProvider.php L40-45 (+3 small edits)

Implementation

Update reservation processing and channel switching to preserve timeout configuration and safely coordinate expiry recovery with an ownership-checked mover lock.

  • src/QueueProvider.php L95-111 (+3 small edits)

Documentation

Document the default and configurable reservation timeout, how it differs from the blocking read timeout, and the recovery fix.

  • CHANGELOG.md L5
  • README.md L34-54

Tests

Add integration and unit coverage for reservation deadlines, recovery, mover-lock ownership, timeout validation, and timeout preservation when changing channels.

  • tests/Integration/ReservationRecoveryTest.php L1-220
  • tests/Unit/QueueProviderTest.php L7, L18-56, L96

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. After the configured deadline, an unacknowledged message can be delivered again even if its original consumer is still processing it. That can run a handler twice and cause external side effects that reverting the code cannot undo.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@samdark samdark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concurrent expiry recovery can redeliver a message while another consumer holds its fresh reservation. Please requeue only entries the recovery worker has exclusively reclaimed, and ensure only the lock owner can release the mover lock.

@avlyubimov

Copy link
Copy Markdown
Author

Thanks for catching this. Addressed in 4cd31c7:

  • Recovery now rechecks each entry’s deadline and atomically removes and requeues it via Lua, preventing a stale mover from requeuing a fresh reservation.

  • Mover locks use unique tokens and atomic compare-and-delete, so only the current owner can release them.

Added deterministic regression tests for both races. All 36 tests pass locally on PHP 8.1 and 8.3.
GitHub CI is still blocked by the existing Composer dev-dependency conflict, documented in the PR description.

@samdark samdark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please preserve PhpRedis value encoding across expiry recovery and lock cleanup, use a shared clock precise enough to honor the reservation timeout, handle reservation registration and Lua failures, document the Redis commands workers need, and keep only the initial release entry in the changelog.

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.

2 participants