Skip to content

Count opening connections towards pool_size - #529

Merged
Dreamsorcerer merged 5 commits into
aio-libs:masterfrom
RayBB:fix/pool-concurrent-connect
Nov 24, 2026
Merged

Dreamsorcerer merged 5 commits into
aio-libs:masterfrom
RayBB:fix/pool-concurrent-connect

Conversation

@RayBB

@RayBB RayBB commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #524.

A connection that was still being opened didn't count towards the pool size, so every concurrent command on an empty pool, or while a discarded connection was being replaced, opened its own connection, ignoring pool_size. Connections are now counted while they open, so the pool never opens more than pool_size, and still opens up to pool_size in parallel.

Counting them exposed a second bug: with pool_minsize below pool_size, the pool only grew past pool_minsize through that storm of connections. Once pool_minsize connections were open, a command that found them all busy waited for one instead of opening another, so e.g. Client(pool_size=10, pool_minsize=1) stayed at one connection under load. acquire() now opens a new connection when none is idle and the pool has room.

  • size() includes connections being opened; the post-connect check that closed the surplus is gone.
  • When no connection is idle and size() < pool_size, acquire() opens a new one instead of waiting.
  • If opening a connection fails or is cancelled while other commands wait for it, one of them is woken to try itself, so none waits forever. This replaces the except in acquire() that put the slot back unconditionally, which could make size() exceed pool_size.
  • Tests check that concurrent gets open exactly pool_size connections at start-up, after losing every connection and after one connection is discarded, with the opens gated so they must overlap while the other commands wait; that the pool grows past pool_minsize up to pool_size, both one command at a time and under concurrent load; that a failed connect hands its slot to each waiter in turn, but not when another command took the slot first; that a failed reconnect doesn't push size() over pool_size; and a cancelled connect. Each scenario runs under asyncio.timeout(), so a regression fails instead of hanging.
  • The test added in Test that acquire keeps its pool slot when reconnecting fails #528 asserted that a failed reconnect puts a placeholder back (size() == 1). With connections counted while they open, there is no placeholder: a failed reconnect leaves size() == 0, and the next acquire() opens a connection. The test now asserts that.

When the pool is empty, at start-up or after every connection was lost,
each concurrent command opens its own connection, because a connection
being opened does not count towards the pool size. 50 concurrent gets
with pool_size=2 open 50 connections and close 48 of them.

Since connections are discarded unless a command completes, the same
happens each time a connection is replaced: while the replacement opens,
size() drops, so every command that arrives opens a connection too. If
the replacement then fails, its slot is put back next to a connection
another command opened meanwhile, and size() exceeds pool_size.

Also cover a failed connect and a connect cancelled while another
command waits for it, so that a fix can't leave waiters hanging or
leave a cancelled slot counted in size().
A connection that was still being opened did not count towards
size(), so every concurrent acquire() on an empty pool, or while a
discarded connection was being replaced, opened its own connection,
and the extras were closed right after. Count them while they open, so
callers that arrive meanwhile wait for a connection instead. Up to
pool_size connections still open in parallel.

If opening fails or is cancelled while other callers wait, hand one of
them the free slot (None) so it tries to connect itself instead of
waiting forever. This replaces the unconditional put back in acquire(),
which could leave a slot queued next to a connection opened meanwhile.
The hand-off runs on the next loop pass and only if a waiter is still
unserved and the pool has room, so waiters cancelled at the same moment
(for example by a shared deadline) don't leave a stale slot queued.
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.33%. Comparing base (010ee63) to head (d831409).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #529      +/-   ##
==========================================
+ Coverage   97.97%   98.33%   +0.35%     
==========================================
  Files          10       10              
  Lines        1037     1201     +164     
  Branches       57       63       +6     
==========================================
+ Hits         1016     1181     +165     
  Misses         12       12              
+ Partials        9        8       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5 Tier: apex

[Medium impact] The change affects connection use across memcache client commands.

Fix the failed-open marker path before merging; commands can fail despite having a healthy idle connection.

Reviews (3) · Last reviewed commit: "Keep a failed connection's slot instead ..." · Reviewed by Greptile

Comment thread aiomcache/pool.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
With connections counted while they open, a pool with pool_minsize
below pool_size stopped growing at all: the first command's connection
made size() reach pool_minsize, so every other command waited for it.
Before, the pool only grew past pool_minsize when concurrent commands
on an empty pool each opened a connection, and once pool_minsize
connections were open, a command that found them all busy waited
instead of opening another.

When no idle connection is queued and the pool has room, open a new
connection instead of waiting. Up to pool_size connections still open
in parallel, and never more than pool_size.
The failed-connect test used a missing Unix socket, which fails without
yielding, so the acquire() calls failed one after another and none ever
waited for another's connection; it passed even if a failed connect
woke no waiter. The connect-count test only counted opens, so it also
passed if connections opened one at a time. Gate the opens instead, and
check that callers queue behind a pending open, that up to pool_size
opens are in flight at once, and that a failed open hands its slot to
each waiter in turn.

Add a test that a failed connect does not hand its slot to a waiter
when another command has already taken it, which would make size()
exceed pool_size.

Bound each scenario, setup included, with asyncio.timeout(), so a
regression fails instead of hanging the suite, and cancel the tasks a
test started (and close the spare connection) in a finally block.
Comment thread aiomcache/pool.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Comment thread tests/pool_test.py Resolved
Put None back in the queue as soon as opening a connection fails or is
cancelled, as acquire() already did on reconnect. Drop the deferred
_wake_waiter() and simplify the pool tests.
@RayBB

RayBB commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Dreamsorcerer Thanks for the detailed feedback, this is great. I agree with all of it, and we've simplified it a lot in d831409.

Comment thread aiomcache/pool.py Resolved
@Dreamsorcerer
Dreamsorcerer merged commit bb24934 into aio-libs:master Nov 24, 2026
14 checks passed
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.

Concurrent commands on an empty pool each open a connection, ignoring pool_size

2 participants