You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
memcached: never read a late reply as the answer to a later command - #113
A reply that arrives after the memcached client timeout can no longer be read as the answer to a later command. Before this fix, a URI could silently resolve to another URI's path, and stay wrong for the life of the process.
Changelog
queryMemcached reads through memcached_mget and memcached_fetch_result and checks the key of each reply. A reply for another key is never returned.
The connection is closed (memcached_quit) after a wrong reply, a failed read or write, or a read that used up the timeout without an answer. That removes any late reply; the next command reconnects.
getMany does the same when a chunk fails or uses up the timeout. Its results were already stored under the key in each reply.
Bug fix (non-breaking change which fixes an issue)
Additional info
The bug. libmemcached 1.1.0 leaves a late reply on the connection, and the next command reads it as its own, so every later command is one reply behind. memcached_get does not check that a reply is for the key asked. With one read held for 1500 ms and a 500 ms timeout, 13 of 26 URIs resolved to the previous URI's path:
Lookup
Result
asset_00
correct path
asset_01
asset_00's path
asset_02
correct path
asset_03
asset_02's path
The wrong answers went into the in-process cache, so asking again gave the same wrong paths. Memcached's values did not change, and a new process was not affected.
A second symptom. With a shorter stall (600 or 800 ms at a 500 ms timeout) nothing was wrong and nothing was logged, but every lookup in that process went to AYON even though memcached held the values. libmemcached reports that timeout as a plain miss, which is why this PR also checks the elapsed time and not only the return code.
When it triggers. It depends on the stall length, not only on exceeding the timeout. At the default 1000 ms timeout, 3000 ms gave wrong paths, while 1200, 2000 and 4000 ms did not. These stalls were simulated with a proxy on one key, one run per length on each backend, so how often a real server would hit this is unknown.
Behaviour change. A slow miss that takes at least the timeout now closes the connection and logs memcached get failed: no reply within the timeout, even if no reply was left behind. The cost is one reconnect. memcached_quit affects every configured server, not only the slow one.
Testing, against a live AYON server, on memcached and Dragonfly:
New scenarios: 22 of 26 failed without this change, 26 of 26 pass with it. They cover wrong paths, the cache staying in use after a timeout, a late multi-key reply reaching a single get, and a late miss marker.
Not tested: the time spent in memcached_quit while a server is stalled, which runs under s_memcachedMutex.
Safeguard, not an observed failure. A get that used up the timeout now reconnects even if a value arrived, as getMany does, in case its END is late and reported as a plain miss. libmemcached reported a timeout every time this was tried, so this case was not reproduced. A complete reply that took longer than the timeout costs one reconnect and one warning. Full suite on this PR merged with #114: 225 passed, 2 expected failures.
Late write acknowledgements. A STORED or DELETED read by a later get looks like a miss to libmemcached, and every later lookup in that session then goes to AYON with nothing logged. This PR keeps that from happening for a set or delete acknowledged after the timeout, because the connection is closed: 16 of 16 scenarios fail without it, and all pass with it. A late reply to add was not tested, though it shares the same failure handling. Any other route to a stray reply would not be detected.
libmemcached leaves a reply that arrives after the client timeout on the
connection, and the next command reads it as its own. memcached_get does
not check the key of a reply, so a URI could resolve to another URI's path,
and every later command stayed one reply behind for the life of the process.
Check the key of each reply and close the connection after a wrong reply,
a failure, or a read that used up the timeout. libmemcached reports some
timeouts as a plain miss, so the elapsed time has to be checked as well.
queryMemcached only checked the elapsed time when no value had arrived, so a
late END reported as a plain miss would stay on the connection. getMany already
checks either way. The value is still returned.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Late acknowledgements can be misclassified as cache misses, leaving get replies unread, while multi-get send failures can be overwritten, causing required connection resets to be skipped.
A MEMCACHED_SOME_ERRORS result from memcached_mget is lost when fetching the healthy servers' replies overwrites rc. If fetching ends normally before the timeout, this condition is false and no connection reset occurs, contrary to the PR's stated cleanup after a failed chunk. Preserve the send-failure status and close the connections after draining the valid replies; those replies can still be returned.
libmemcached reports a STORED or DELETED read first as NOTFOUND, so the check
does not catch those. They do not stay behind, because a write that times out
closes the connection.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Runtime validation is needed because memcached_quit latency under stalled servers remains untested, and cleanup holds the process-wide mutex, potentially blocking concurrent lookups and invalidations beyond the configured timeout.
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
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.
Description
A reply that arrives after the memcached client timeout can no longer be read as the answer to a later command. Before this fix, a URI could silently resolve to another URI's path, and stay wrong for the life of the process.
Changelog
queryMemcachedreads throughmemcached_mgetandmemcached_fetch_resultand checks the key of each reply. A reply for another key is never returned.memcached_quit) after a wrong reply, a failed read or write, or a read that used up the timeout without an answer. That removes any late reply; the next command reconnects.getManydoes the same when a chunk fails or uses up the timeout. Its results were already stored under the key in each reply.Linked issues/branches
enhancement/featuriing-memcached(Memcached support #82)test_memcached_timeouts.py. They fail on builds without this PR, so merge this one first, or expect those failures on Memcached: add live server integration tests #107 until it lands.Type of Change
Additional info
The bug. libmemcached 1.1.0 leaves a late reply on the connection, and the next command reads it as its own, so every later command is one reply behind.
memcached_getdoes not check that a reply is for the key asked. With one read held for 1500 ms and a 500 ms timeout, 13 of 26 URIs resolved to the previous URI's path:asset_00asset_01asset_00's pathasset_02asset_03asset_02's pathThe wrong answers went into the in-process cache, so asking again gave the same wrong paths. Memcached's values did not change, and a new process was not affected.
A second symptom. With a shorter stall (600 or 800 ms at a 500 ms timeout) nothing was wrong and nothing was logged, but every lookup in that process went to AYON even though memcached held the values. libmemcached reports that timeout as a plain miss, which is why this PR also checks the elapsed time and not only the return code.
When it triggers. It depends on the stall length, not only on exceeding the timeout. At the default 1000 ms timeout, 3000 ms gave wrong paths, while 1200, 2000 and 4000 ms did not. These stalls were simulated with a proxy on one key, one run per length on each backend, so how often a real server would hit this is unknown.
Behaviour change. A slow miss that takes at least the timeout now closes the connection and logs
memcached get failed: no reply within the timeout, even if no reply was left behind. The cost is one reconnect.memcached_quitaffects every configured server, not only the slow one.Testing, against a live AYON server, on memcached and Dragonfly:
get, and a late miss marker.Not tested: the time spent in
memcached_quitwhile a server is stalled, which runs unders_memcachedMutex.Safeguard, not an observed failure. A
getthat used up the timeout now reconnects even if a value arrived, asgetManydoes, in case itsENDis late and reported as a plain miss. libmemcached reported a timeout every time this was tried, so this case was not reproduced. A complete reply that took longer than the timeout costs one reconnect and one warning. Full suite on this PR merged with #114: 225 passed, 2 expected failures.Late write acknowledgements. A
STOREDorDELETEDread by a latergetlooks like a miss to libmemcached, and every later lookup in that session then goes to AYON with nothing logged. This PR keeps that from happening for asetordeleteacknowledged after the timeout, because the connection is closed: 16 of 16 scenarios fail without it, and all pass with it. A late reply toaddwas not tested, though it shares the same failure handling. Any other route to a stray reply would not be detected.