Skip to content

memcached: never read a late reply as the answer to a later command - #113

Merged
antirotor merged 3 commits into
enhancement/featuriing-memcachedfrom
fix/memcached-late-replies
Nov 6, 2026
Merged

antirotor merged 3 commits into
enhancement/featuriing-memcachedfrom
fix/memcached-late-replies

Conversation

@antirotor

@antirotor antirotor commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

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

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

Linked issues/branches

Type of Change

  • 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.
  • Full suite with this change: 153 passed, 2 expected failures (the site-roots case memcached: a cached path with an unmapped root is a miss #106 leaves open).

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.
@antirotor antirotor added the type: bug Something isn't working label Nov 5, 2026
@antirotor
antirotor requested a balanced review from Copilot November 6, 2026 12:18
@antirotor antirotor self-assigned this Nov 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical timeout path leaves the connection open after receiving a value, allowing a late END terminator to be consumed by the next command.

1 open issue
What changed in this PR

This PR hardens the resolver’s memcached cache layer against late replies, preventing one URI’s cached path from being returned for another URI.

Changes:

  • Replaces single-key reads with memcached_mget and validates each reply’s key.
  • Closes connections after unexpected replies, failed operations, or detected timeouts.
  • Adds timeout-based connection cleanup to batch reads.
File Description
src/​AyonUsdResolver/​cache/​memcachedHandler.cpp Adds reply-key validation and connection cleanup to prevent stale responses from affecting later commands.

Review effort: Balanced

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Fetching replies overwrites the mget failure status and skips cleanup

src/​AyonUsdResolver/​cache/​memcachedHandler.cpp:300

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.

1 resolved since last review

Review effort: Balanced

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@antirotor
antirotor merged commit 1721398 into enhancement/featuriing-memcached Nov 6, 2026
2 checks passed
@antirotor
antirotor deleted the fix/memcached-late-replies branch November 6, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants