Skip to content

Fix pool cleanup and keepalive reconnection races - #573

Merged
binaryfire merged 2 commits into
0.4from
fix/pool-cleanup-and-reconnect
Sep 9, 2026
Merged

Fix pool cleanup and keepalive reconnection races#573
binaryfire merged 2 commits into
0.4from
fix/pool-cleanup-and-reconnect

Conversation

@binaryfire

Copy link
Copy Markdown
Member

This fixes two coroutine races in the pool lifecycle, reported during review of #572. One can skip an object's cleanup; the other waits for a socket that has already been closed and reports a misleading timeout.

Object pool cleanup

A lifetime sweep or idle trim can pause while destroying an expired object. If another coroutine closes the pool and pauses while destroying a different object, maintenance can resume and remove a healthy object from the closed channel. Returning that object to the channel fails, leaving its destroy callback uncalled and its managed entry behind.

A rejected requeue now destroys the object through the pool's existing cleanup path. The regression tests hold both cleanup operations at the relevant points and verify that every object is destroyed exactly once and the pool finishes empty.

Keepalive reconnection

When two connection attempts overlap, the losing attempt closes its unused socket. The winning socket can close while that cleanup is waiting. Reconnection then returns false, but call() previously ignored the result and waited until wait_timeout before throwing SocketPopException.

call() now checks the result and throws ConnectionException immediately when reconnection completes without an available socket. Reconnection also returns the actual connected state after heartbeat setup, covering closure during timer creation. Existing exceptions and cancellation continue to propagate without an additional reconnect attempt.

The connection contract and keepalive documentation describe these results. Tests cover both closure paths, including callback execution, socket cleanup, and timer cleanup.

Validation

PHPStan, PHP formatting checks, and the object-pool and connection-pool package suites pass. The new regressions were also run against the original implementations and fail for the reported reasons.

Addresses the object cleanup comment and keepalive reconnect comment.

A lifetime sweep or idle trim can resume while close is suspended in another destroy callback. The closed channel still contains objects for draining, so maintenance can remove a healthy object and lose it when the channel rejects its requeue.

Destroy rejected requeues through the existing cleanup path. Cover both maintenance operations with a controlled overlap, asserting that the healthy object is destroyed before close resumes and every object is cleaned up exactly once.
Concurrent reconnect cleanup can finish after the winning socket has closed. Check the false reconnect result before attempting a socket wait, and raise ConnectionException immediately instead of waiting until timeout and reporting socket exhaustion.

Return the actual connected state after heartbeat activation and previous-channel wakeup, including closure during timer creation. Document the reconnect result and keepalive exception behavior, and cover concurrent cleanup and timer-creation closure without adding retries or shared state.
@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: db869055-71f5-4f8a-9056-8b3f43939bbe

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@binaryfire
binaryfire merged commit 26ea9b9 into 0.4 Sep 9, 2026
37 of 38 checks passed
@binaryfire
binaryfire deleted the fix/pool-cleanup-and-reconnect branch September 9, 2026 15:11
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