fix: Atomic job reservation to prevent race condition#1399
Merged
dimitri-yatsenko merged 2 commits intomasterfrom Feb 17, 2026
Merged
fix: Atomic job reservation to prevent race condition#1399dimitri-yatsenko merged 2 commits intomasterfrom
dimitri-yatsenko merged 2 commits intomasterfrom
Conversation
Replace the non-atomic SELECT-then-UPDATE pattern in Job.reserve() with a single atomic UPDATE that includes status='pending' in the WHERE clause. Check cursor.rowcount to determine if the reservation succeeded. This eliminates the race window where multiple workers could simultaneously reserve the same job. The previous implementation allowed concurrent workers to both read status='pending' and then both successfully UPDATE (since the WHERE matched only on primary key). Now only the first UPDATE succeeds; all others see rowcount=0 and return False. Also reduces three database round-trips to one. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mweitzel
approved these changes
Feb 17, 2026
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
Job.reserve()with a single atomicUPDATE ... WHERE status='pending', checkingcursor.rowcountfor successRoot cause
The previous
reserve()performed a SELECT to checkstatus='pending', then a separate UPDATE matching only on primary key. When concurrent workers (e.g., SLURM array jobs) hit this simultaneously, both SELECTs seestatus='pending', both UPDATEs succeed (since the WHERE matched only on PK), and both workers proceed to callmake()on the same key.Fix
A single atomic UPDATE includes
AND status='pending' AND scheduled_time <= CURRENT_TIMESTAMP(3)in the WHERE clause. The database guarantees only one concurrent UPDATE can match; all others getrowcount=0and returnFalse.Test plan
🤖 Generated with Claude Code