Skip to content

Validate arguments before taking a pooled connection - #525

Merged
Dreamsorcerer merged 11 commits into
aio-libs:masterfrom
RayBB:fix/validate-before-acquire
Nov 23, 2026
Merged

Dreamsorcerer merged 11 commits into
aio-libs:masterfrom
RayBB:fix/validate-before-acquire

Conversation

@RayBB

@RayBB RayBB commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #519.

Arguments were checked after a connection was taken from the pool, so every invalid call, or a set_flag_handler that raised, closed a healthy connection. Now @acquire wraps only the methods that talk to the server (_execute_simple_command() and a new _retrieve()). Public methods validate and build the request first. Storage commands build only the header before acquiring, and write the value once a connection is held.

Server replies are still checked while the connection is held, so an unexpected reply discards the connection as before. Each command keeps its exception type and message.

Private helpers no longer take conn, and _execute_simple_command() now takes the reply check, so subclasses that call them need updating (noted in CHANGES). The timeout no longer covers set_flag_handler.

Tests: every command with invalid arguments fails without taking a connection, and an unexpected reply followed by extra bytes discards the connection, so the next command gets its own reply.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.65%. Comparing base (7f3eb46) to head (2d99b9e).

Files with missing lines Patch % Lines
aiomcache/client.py 90.00% 7 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #525      +/-   ##
==========================================
+ Coverage   97.47%   97.65%   +0.17%     
==========================================
  Files          10       10              
  Lines         992     1025      +33     
  Branches       54       57       +3     
==========================================
+ Hits          967     1001      +34     
- Misses         14       15       +1     
+ Partials       11        9       -2     

☔ 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.

@RayBB
RayBB force-pushed the fix/validate-before-acquire branch from 06b220e to 878cf3f Compare November 22, 2026 19:44
@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5 Tier: apex

[Medium impact] This PR appears safe to merge.

Reviews (8) · Last reviewed commit: "Parse the version reply while holding th..." · Reviewed by Greptile

Comment thread aiomcache/client.py Resolved
Comment thread tests/commands_test.py Resolved
Comment thread aiomcache/client.py Resolved
Comment thread tests/commands_test.py Resolved
Comment thread aiomcache/client.py Resolved
Comment thread tests/commands_test.py Resolved
Comment thread aiomcache/client.py Resolved
Moving the reply checks out of @acquire meant that an unexpected reply
returned the connection to the pool, along with any bytes the server sent
after it. The next command could then read a stale reply.

_execute_simple_command() now takes the check and the error message, and
raises while it still holds the connection, as master did. Each command
keeps its exception type and message.
Storage commands built the full command, including a copy of the value,
before waiting for a pooled connection, so every waiting call held an
extra copy. Build only the header first, and write the value and the
trailing \r\n once a connection is held.
Comparing memcached's total_connections caught a replaced connection, but
only indirectly, and another client on the server could throw it off.
Check that the pool hands back the same connection instead.
It yields the reply while holding the connection, so each command checks
its own reply in the block instead of passing valid/error callbacks. An
exception in the block still discards the connection. @acquire now uses
the same _connection() helper.
_retrieve() and stats() now take their connection with
async with self._connection(), like _execute_simple_command().
@RayBB
RayBB force-pushed the fix/validate-before-acquire branch from 6f3bd96 to 34f5b75 Compare November 23, 2026 21:52
Comment thread aiomcache/client.py Resolved
Comment thread tests/pool_test.py Resolved
Dreamsorcerer and others added 2 commits November 23, 2026 22:14
A reply that passed the VERSION prefix check but failed to split released
the connection as healthy, so leftover bytes could reach the next command.
@Dreamsorcerer

Copy link
Copy Markdown
Member

Lost coverage of pool.py:74, is it still reachable?

@RayBB

RayBB commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@Dreamsorcerer Yes, it's still reachable. This PR doesn't touch pool.py, and coverage of those lines is just flaky, including on master. Details in #527, and I'll open a PR with a deterministic test.

@Dreamsorcerer

Copy link
Copy Markdown
Member

Cool, thanks.

@Dreamsorcerer
Dreamsorcerer merged commit 14a07f4 into aio-libs:master Nov 23, 2026
13 of 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.

A ValidationException closes a healthy pooled connection

2 participants