fix(backends): retry Memcached increment when a new counter expires before INCR - #318
Merged
Merged
Conversation
…efore INCR Memcached keeps time in whole seconds, so a counter created with ttl=1 can expire between its ADD and the INCR that follows, and increment() raised 'Counter vanished between ADD and INCR'. Retry the ADD + INCR pair up to _CAS_MAX_RETRIES times inside the same worker call; a counter that expired inside its own window starts a new one at delta. The live ttl test now uses ttl=2 and polls for expiry with a deadline instead of depending on where the clock tick lands. Closes #315
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.
Problem
MemcachedBackend.increment()creates a counter with INCR (miss), ADD with the exptime, then INCR again. Memcached keeps time in whole seconds, so an item stored with exptime 1 can expire almost immediately if the clock ticks right after the ADD. When that happened between the ADD and the second INCR,increment()raisedCacheXError("Counter vanished between ADD and INCR"). This failed once in CI intest_memcached_increment_honors_ttl.Fix
_incrementnow retries the ADD + INCR pair up to_CAS_MAX_RETRIES(16) times, the same bound as the CAS loops, and returns as soon as an INCR lands. A counter that expired inside its own window really is a new window, so starting again atdeltais correct. The whole loop still runs inside the oneasyncio.to_threadcall.increment()raiseCacheXError("Counter vanished between ADD and INCR on each of 16 attempts").now.Tests
CacheXError.test_memcached_increment_honors_ttlusesttl=2(the counter survives the next call regardless of the tick) and polls for expiry with a 5 s deadline instead of a fixed sleep. It also asserts the second call returns 2, so the window was really still open. Ran it 10 times in a row: all passed (1.3 s to 2.1 s each).Full suite including the live Redis and Memcached tests: 1236 passed, 1 skipped, coverage 99.97%. ruff, ruff format and mypy --strict are clean.
Docs
docs/BACKENDS.mdand the zh-TW mirror describe the retry underincrement. Changelog fragment:changelog.d/315.fixed.md.Closes #315