Add stale connection recovery to prevent pool exhaustion by claudear · Pull Request #30 · utopia-php/pools · GitHub
Skip to content

Add stale connection recovery to prevent pool exhaustion - #30

Closed
claudear wants to merge 1 commit into
mainfrom
fix/stale-connection-recovery
Closed

claudear wants to merge 1 commit into
mainfrom
fix/stale-connection-recovery

Conversation

@claudear

Copy link
Copy Markdown
Contributor

Summary

  • Adds maxUseTime configuration to Pool that enables detection of leaked connections (connections checked out longer than the threshold)
  • When the pool is exhausted and stale connections are detected, their slots are recovered allowing new connections to be created
  • Tracks checkout timestamps on Connection objects via markCheckedOut()/getCheckedOutAt()
  • Feature is opt-in (disabled by default with maxUseTime=0) for full backwards compatibility

Problem

The Sentry error "Pool 'console' is empty (size 75)" (CLOUD-3K4N, 3267 events) occurs when all pool connections are checked out and none are returned. This happens when connections are leaked — popped but never reclaimed due to consumer code errors, timeouts, or edge cases. Without recovery, leaked connections permanently reduce pool capacity until full exhaustion.

How it works

  1. Each connection records a timestamp when checked out (markCheckedOut())
  2. When pop() fails to get a connection and maxUseTime > 0, it scans active connections for any exceeding the threshold
  3. Stale connections are removed from tracking and their slots freed (connectionsCreated decremented)
  4. The retry loop then creates a fresh connection in the freed slot

Test plan

  • testPoolRecoversStaleConnections — verifies stale connections are detected and new connections created in their place
  • testPoolDoesNotRecoverNonStaleConnections — verifies non-stale connections are not evicted
  • testPoolStaleRecoveryDisabledByDefault — verifies the feature is off when maxUseTime=0
  • All 90 existing tests pass
  • Lint (pint --preset psr12) passes
  • Static analysis (phpstan) passes

🤖 Generated with Claude Code

…connections

When connections are popped but never returned (leaked), the pool permanently
loses those slots and eventually becomes fully exhausted. This adds a
configurable maxUseTime that, when set, detects connections that have been
checked out longer than the threshold and recovers their slots by allowing
new connections to be created in their place.

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

coderabbitai Bot commented Mar 11, 2026

Copy link
Copy Markdown

@ChiragAgg5k

Copy link
Copy Markdown
Member

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