Skip to content

Read connection.unblocked before publishing, and optionally wait while blocked - #31

Merged
TomK merged 2 commits into
masterfrom
blocked-wait
Nov 23, 2026
Merged

TomK merged 2 commits into
masterfrom
blocked-wait

Conversation

@TomK

@TomK TomK commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

When RabbitMQ raises a memory or disk alarm it sends connection.blocked, and php-amqplib then refuses every publish on that connection with AMQPConnectionBlockedException. It only clears the flag when it reads connection.unblocked, but nothing reads from a push connection once publishing throws, so the flag outlives the alarm. In production a Fortifi retrieve worker threw 1,803 times in 90 seconds, nacking each message, for 55 seconds after the cluster cleared the alarm, until its daemon restarted.

  • Push after the broker has unblocked publishers: succeeds; the stale flag is cleared by reading frames already waiting on the push connection.
  • Push while still blocked, blocked_wait unset (default 0): fails with AMQPConnectionBlockedException, as before.
  • Push while blocked, blocked_wait > 0: waits up to blocked_wait seconds for connection.unblocked, then publishes, or fails as before if still blocked.
  • waitWhileBlocked($connectionMode, $timeout): waits up to $timeout seconds and returns whether the connection is unblocked, for callers that publish on their own channel.

Test plan

  • The tests raise a real memory alarm through the CI RabbitMQ container (RABBIT_CONTAINER, now passed to PHPUnit) and skip without it.
  • Pushed alone first, the five new tests failed in CI: a push after the alarm cleared still threw Connection is blocked due to low resources, a push with blocked_wait threw after 0.0001s, and waitWhileBlocked() did not exist. With the fix, all 42 jobs pass with every test run (PHP 7.4–8.4 × RabbitMQ 3.10–4.2).

🤖 Generated with Claude Code

TomK and others added 2 commits November 23, 2026 11:45
The tests raise a real memory alarm with rabbitmqctl through the RabbitMQ
service container, whose id CI now passes as RABBIT_CONTAINER, and skip
when it is not set.

- Without blocked_wait, a push on a blocked connection fails.
- After the broker unblocks publishers, a push succeeds even without
  blocked_wait. Today the client keeps a stale blocked flag, because it
  never reads connection.unblocked, and every push fails.
- With blocked_wait, a push waits for the broker to unblock, and fails
  once blocked_wait passes.
- waitWhileBlocked() returns once unblocked, or false at its timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ocked

php-amqplib refuses to publish on a connection flagged blocked, and only
clears the flag when it reads connection.unblocked. Once a push throws,
nothing reads the push connection, so the flag outlives the alarm.

pushBatch() now calls waitWhileBlocked() before publishing. With the
default blocked_wait of 0 it reads frames already received, which clears
a stale flag. With blocked_wait above 0 it reads for up to that many
seconds until the broker unblocks publishers. If the connection is still
blocked, the publish fails with AMQPConnectionBlockedException as before.

waitWhileBlocked($connectionMode, $timeout) is public for callers that
publish on their own channel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomK
TomK marked this pull request as ready for review November 23, 2026 11:58
@TomK
TomK requested a lite review from Copilot November 23, 2026 12:09
@TomK
TomK merged commit 392f1c9 into master Nov 23, 2026
43 checks passed

Copilot AI 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.

🟡 Changes recommended

The change still contains critical wait() calls on AbstractConnection in both production and test code, causing undefined-method errors instead of processing connection state frames.

2 open issues
What changed in this PR

Updates the RabbitMQ provider to recover from stale blocked-connection state by reading broker frames before publishing and optionally waiting for the broker to unblock.

Changes:

  • Added configurable blocked_wait handling and a public waitWhileBlocked() helper.
  • Added RabbitMQ alarm integration tests covering blocked, recovery, waiting, and timeout behavior.
  • Passed the RabbitMQ service container ID to PHPUnit in CI.
File Description
tests/​Provider/​AmqpTest.php Adds integration tests for RabbitMQ blocked and unblocked publishing behavior.
src/​Provider/​Amqp/​AmqpQueueProvider.php Adds blocked-connection frame handling, optional waiting before publishing, and the public wait helper.
.github/​workflows/​unit-test.yml Supplies the RabbitMQ service container ID to the PHPUnit test process.

Review effort: Lite

Comment thread src/Provider/Amqp/AmqpQueueProvider.php Resolved
Comment thread tests/Provider/AmqpTest.php Resolved
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.

3 participants