fix: sort zoids in CacheWarmer._flush to prevent PK-index deadlock#61
Merged
Conversation
Concurrent Waitress workers with overlapping pending sets deadlocked on cache_warm_stats PK index when their INSERT ... ON CONFLICT DO UPDATE acquired row locks in opposing orders. Set iteration order is not stable across processes, so two flushes could see the same zoids in different orders. Sort zoids before the upsert for a deterministic row-lock acquisition order. Added regression test verifying the INSERT parameter is sorted. Co-Authored-By: Claude Opus 4.7 (1M context) <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
INSERT ... ON CONFLICT DO UPDATEintocache_warm_statsso concurrent Waitress workers acquire PK row locks in the same order and can never deadlock.psycopg.errors.DeadlockDetectedonINSERT ... while inserting index tuple (…) in relation "cache_warm_stats"between two worker processes.zoids = list(self._pending)—setiteration order is unstable across processes, so two workers with overlapping pending sets upsert the same rows in opposing orders and deadlock on the PK index.TestCacheWarmerFlushEdgethat mocks the cursor and asserts the bigint[] bound to the INSERT is sorted. Verified the test fails onlist(...)and passes onsorted(...).Test plan
pytest tests/test_cache_warmer.py— 24/24 green locallyDeadlockDetectedfromcache_warmer._flushafter deploy🤖 Generated with Claude Code