Repository navigation
Validate arguments before taking a pooled connection - #525
Merged
Dreamsorcerer merged 11 commits intoNov 23, 2026
Merged
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
RayBB
force-pushed
the
fix/validate-before-acquire
branch
from
November 22, 2026 19:44
06b220e to
878cf3f
Compare
[Medium impact] This PR appears safe to merge. Reviews (8) · Last reviewed commit: "Parse the version reply while holding th..." · Reviewed by Greptile |
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
force-pushed
the
fix/validate-before-acquire
branch
from
November 23, 2026 21:52
6f3bd96 to
34f5b75
Compare
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.
Member
|
Lost coverage of pool.py:74, is it still reachable? |
Contributor
Author
|
@Dreamsorcerer Yes, it's still reachable. This PR doesn't touch |
Member
|
Cool, thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #519.
Arguments were checked after a connection was taken from the pool, so every invalid call, or a
set_flag_handlerthat raised, closed a healthy connection. Now@acquirewraps 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). Thetimeoutno longer coversset_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.