Repository navigation
Fix a lost-update race in entrecote.add() - #77
Merged
Merged
Conversation
db.get()/db.set() each lock, read or write, and unlock on their own, so calling them as two separate steps – as add() did – left a window between them where a concurrent request for the same key read the same starting count this one just had, and both then wrote back the same incremented value, losing one hit. Real traffic hits the same group/key concurrently all the time (mod_wsgi runs multiple threads per process), so this wasn’t theoretical. Hold db.process_lock across the whole read-modify-write instead, exactly like get_recent() already does for its own reasons, reading and writing the file directly rather than going through db.get()/ db.set() again inside that same lock. Added a regression check to the existing entrecote.py smoke test: 50 concurrent add() calls on one key, asserting the final count is exactly 50. Confirmed it actually catches the race by reverting the fix locally first – the old code lost all but a handful of hits. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Summary
db.get()/db.set()in PupDB each lock, read or write, and unlock on their own.add()called them as two separate steps, leaving a window where a concurrent request for the same key reads the same starting count and both writes land the same incremented value – losing a hit. Real traffic hits the same group/key concurrently (mod_wsgi runs multiple threads per process), so this isn't theoretical.db.process_lockacross the whole read-modify-write instead, the same wayget_recent()already does, reading/writing the file directly rather than re-enteringdb.get()/db.set()inside that same lock.Test plan
entrecote.py's existing smoke test: 50 concurrentadd()calls on one key, asserting the final count is exactly 50.venv/bin/python3 entrecote.pypasses twice in a row (checked test isolation between runs)sh ./test.shpasses against a live dev server🤖 Generated with Claude Code