Skip to content

Heartbeat every open AMQP connection, not just the last one used - #30

Open
TomK wants to merge 10 commits into
masterfrom
heartbeat-all-connections
Open

TomK wants to merge 10 commits into
masterfrom
heartbeat-all-connections

Conversation

@TomK

@TomK TomK commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A process with more than one AMQP connection only heartbeats the connection it touched last. _getConnection() re-registers the single PCNTLHeartbeatSender on every call, and SIGALRM has one handler per process, so another provider's sender replaces it too. Every other connection goes quiet when idle, and the broker closes it for missed heartbeats. In production one worker deployment accounted for 548 of ~620 of these closes in three hours. A long-running consumer died with Connection reset by peer after its publisher connection was blocked by a memory alarm.

  • Idle connection on a provider that used another connection mode since: stays open.
  • Idle connection on a provider created before another provider in the same process: stays open.
  • Idle connection while other connections open and close faster than the heartbeat interval: stays open.
  • Disconnecting one connection mode: the provider's other connections keep heartbeating.
  • Push or consume after a background heartbeat failed (e.g. a long process pause): reconnects instead of throwing AMQPConnectionClosedException.
  • Consumer callback paused past the broker heartbeat timeout: its ack is skipped, consume() returns false, and the message is redelivered.
  • Consume connection lost part way through batchConsume(): the batch callback gets no deliveries from the lost connection; they arrive in the next batch.
  • AMQP_WITHOUT_SIGNALS defined true: no SIGALRM handler is installed, as before.
  • Released provider that has consumed: garbage collected; the registry holds connections by weak reference.

Test plan

  • Each behaviour has a test that was pushed alone first and failed in CI: first two with Missed server heartbeat, the six from review in three runs (1, 2, 3), each on the defect it covers.
  • With the fixes, all 42 jobs pass (PHP 7.4–8.4 × RabbitMQ 3.10–4.2).

🤖 Generated with Claude Code

TomK and others added 2 commits November 20, 2026 18:21
A process holding more than one AMQP connection, from one provider's
connection modes or from several providers, should keep every connection
alive while it is idle. These tests fail: only the connection touched last
has a heartbeat sender, so the others miss their heartbeats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AmqpHeartbeat holds every open connection in the process and, on each
alarm, sends a heartbeat on any that has been quiet for half its heartbeat
interval and is not mid-write. A provider registers a connection when it
opens it and unregisters it when it disconnects that connection mode.

This replaces the per-provider PCNTLHeartbeatSender, which was re-registered
on every _getConnection() call and shared the process's single SIGALRM
handler with every other provider, so only the last connection touched was
heartbeated. The protected $_heartbeatSender property is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomK
TomK marked this pull request as ready for review November 20, 2026 18:29
@TomK
TomK requested a balanced review from Copilot November 20, 2026 19:17

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

Connection churn can indefinitely postpone heartbeats, while swallowed heartbeat failures allow dead connections to be reused, causing broker timeouts and publish failures.

4 open issues
What changed in this PR

Updates the AMQP queue provider to use one process-wide heartbeat sender, aiming to keep idle connections alive across connection modes and provider instances.

Changes:

  • Adds a shared heartbeat registry and SIGALRM handler.
  • Registers connections when opened and unregisters them individually on disconnect.
  • Adds regression tests for idle connections across modes and providers.
File Description
tests/​Provider/​Mock/​AmqpMockProvider.php Updates heartbeat controls and exposes existing connections for assertions.
tests/​Provider/​AmqpTest.php Adds multi-connection and multi-provider heartbeat tests.
src/​Provider/​Amqp/​AmqpQueueProvider.php Integrates shared heartbeat registration with connection lifecycle management.
src/​Provider/​Amqp/​AmqpHeartbeat.php Adds the process-wide connection registry and heartbeat scheduler.

Review effort: Balanced

Comment thread src/Provider/Amqp/AmqpHeartbeat.php Resolved
Comment thread src/Provider/Amqp/AmqpHeartbeat.php Resolved
Comment thread src/Provider/Amqp/AmqpHeartbeat.php Resolved
Comment thread src/Provider/Amqp/AmqpHeartbeat.php Resolved
TomK and others added 3 commits November 20, 2026 19:59
- An idle connection stays alive while other connections open and close
  more often than the heartbeat interval.
- A push after a process pause long enough to miss the broker heartbeat
  reconnects instead of writing to the closed socket.
- A provider that has consumed is garbage collected once released.
- AMQP_WITHOUT_SIGNALS leaves the SIGALRM handler untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Hold connections by WeakReference, so a released provider (which a
  consumer channel references through its callback) can be collected.
- Only ever bring the pending alarm forward. Registering or removing a
  connection no longer postpones it, so churn cannot starve idle
  connections of heartbeats.
- When a heartbeat fails inside the signal handler, flag the connection.
  The provider disconnects it on its next push or consume and reconnects.
- Honour AMQP_WITHOUT_SIGNALS, as PCNTLHeartbeatSender did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PHP runs the destructors of a garbage cycle on the first collection and
frees the cycle on the next one.

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

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

A missed-heartbeat socket closure can still cause acknowledgement exceptions to escape to the worker, and regression coverage is missing for paused callbacks and disconnecting one mode while preserving another.

2 open issues
4 resolved since last review

Review effort: Balanced

Comment thread src/Provider/Amqp/AmqpHeartbeat.php Resolved
Comment thread tests/Provider/AmqpTest.php Resolved
TomK and others added 2 commits November 20, 2026 21:02
- A consumer callback paused past the broker heartbeat timeout should
  leave its message for redelivery: its ack must not throw, consume()
  returns false, and the next consume receives the message again.
- Disconnecting one connection mode keeps the provider's other
  connection heartbeating.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A heartbeat that fails in the signal handler during a consumer callback
closes the socket. ack, nack and batchAck now skip settling on a flagged
consume connection, since its delivery tags are void and replaying them on
a new channel would settle unrelated messages. consume() then disconnects
and returns false, and the broker redelivers the messages.

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

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

Heartbeat-failure disconnects leave pending batch deliveries intact, allowing stale tags to be acknowledged on a replacement channel and potentially settling unrelated deliveries.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Handle connection-closed exception after missed heartbeat

src/​Provider/​Amqp/​AmqpQueueProvider.php:309

If SIGALRM detects a missed heartbeat while consume() is waiting for a message, the resumed read can throw AMQPConnectionClosedException (upstream StreamIO, also verified in v3.7.3). Neither existing catch handles it, so execution skips this failure check and the exception escapes to the worker. Catch this exception when the consume connection is flagged so the disconnect-and-return-false path runs, while rethrowing unrelated failures.

2 resolved since last review

Review effort: Balanced

Comment thread src/Provider/Amqp/AmqpQueueProvider.php Resolved
TomK and others added 3 commits November 21, 2026 11:30
A heartbeat failure part way through batchConsume() replaces the consume
connection. The batch should then hold only deliveries from the new
connection, not the lost delivery whose tag is void.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SIGALRM from the shared heartbeat handler interrupts sleep(), and its
whole-second remainder can lose part of a second per interruption, so a
12s idle could fall under the 9s missed-heartbeat threshold. This made
testHeartbeatAfterMissed fail intermittently. Every test now sleeps to a
microtime deadline.

When a heartbeat fails during the first delivery of a batch, consume()
returns false and batchConsume() hands the stale delivery to the callback.
The test now expects no stale delivery in that batch and the redelivery
in the next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deliveries collected by batchConsume() carry tags of the channel they
arrived on. Once that connection is gone the broker requeues them, so the
batch callback must not see them and batchAck() must not settle their tags
on a replacement channel.

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

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.

🔵 Needs a closer look

Human assessment is needed because an unpublished critical finding identifies heartbeat-failure paths that can terminate the worker or leave earlier batch deliveries available after a timeout.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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