Skip to content

Retire rejected pool connections without closing active streams - #1

Merged
Jacob-Met merged 1 commit into
masterfrom
fix/1066-retry-stale-pool-connections
Nov 16, 2026
Merged

Jacob-Met merged 1 commit into
masterfrom
fix/1066-retry-stale-pool-connections

Conversation

@Jacob-Met

Copy link
Copy Markdown
Owner

Summary

When a connection raises ConnectionNotAvailable, the pool currently clears its request but can assign the same stale connection again, or retain an unavailable connection in the last pool slot. This fixes #1066 in both sync and async pools.

The pool now retires connections that reject a request while still reporting availability, or are no longer referenced by an in-flight request. A retired connection is excluded from future assignments and closed only after all responses using it finish. This prevents repeated retries, PoolTimeout due to a dead slot, and premature socket closure of concurrent HTTP/2 responses.

#1084 also addresses encode#1066, but its immediate close() after ConnectionNotAvailable can interrupt another active stream using the same connection. This alternative adds deferred retirement, in-flight stream tests, and real cleanup of released stale sockets rather than closing shared sockets at rejection time.

Verification

  • Regression tests fail on upstream 10a658221deb38a4c5b16db55ab554b0bf731707 (repeated stale assignment and exhausted pool slots); 9 tests pass after the fix across sync, asyncio and Trio, including concurrent in-flight reuse.
  • .venv/bin/python -m pytest -q — 223 passed, six pre-existing XPASS.
  • .venv/bin/python scripts/unasync.py --check — passes.
  • .venv/bin/ruff check httpcore tests/test_pool_stale_connection.py — passes.
  • .venv/bin/mypy --no-warn-unused-ignores httpcore — passes (the unmodified project has an existing unused-ignore warning in trio.py).
  • Existing keepalive concurrency test passes 30 consecutive runs.

Checklist

AI assistance disclosure: implementation, tests and this PR description were prepared with GPT-6, reviewed against failing upstream behavior, the current encode#1084 alternative, and local tests.

A ConnectionNotAvailable retry could reuse a stale connection indefinitely or leave an unavailable connection occupying pool capacity. Track rejected connections until outstanding requests release them, then close and replace safely in sync/async pools.
@Jacob-Met
Jacob-Met merged commit 7deb88d into master Nov 16, 2026
@Jacob-Met
Jacob-Met deleted the fix/1066-retry-stale-pool-connections branch November 16, 2026 06:24
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.

Stale connection stays in pool after ConnectionNotAvailable

2 participants