Fix race condition in task replacement where duplicate executions occur#151
Merged
chrisguidry merged 1 commit intomainfrom Aug 14, 2025
Merged
Fix race condition in task replacement where duplicate executions occur#151chrisguidry merged 1 commit intomainfrom
chrisguidry merged 1 commit intomainfrom
Conversation
Previously, `docket.replace()` could not cancel tasks that had already been moved from the queue to the stream by the scheduler, causing duplicate execution when the old task ran alongside the replacement. This happened because: 1. Scheduler moves due tasks from queue → stream, assigning Redis message IDs 2. `replace()` only checked the queue, missing tasks already in the stream 3. Both old and new tasks would execute This implements atomic task scheduling and cancellation using Lua scripts that track stream message IDs. The solution: - **Atomic scheduling**: New Lua script handles task existence checks, replacement cancellation, and scheduling in a single operation - **Stream message ID tracking**: When tasks move to stream, their message IDs are stored in the known task metadata - **Atomic cancellation**: Can delete tasks from stream using stored message IDs or from queue for scheduled tasks - **Race-free replacement**: Uses Redis locks per task key and atomic operations Comprehensive test coverage includes race condition scenarios and idempotent operations. Closes #149 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
📚 Documentation has been built for this PR! You can download the documentation directly here: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #151 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 31 31
Lines 4382 4411 +29
Branches 246 244 -2
=========================================
+ Hits 4382 4411 +29
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Collaborator
|
Snap! |
abrookins
reviewed
Aug 14, 2025
|
|
||
| class _schedule_task(Protocol): | ||
| async def __call__( | ||
| self, keys: list[str], args: list[str | float | bytes] |
Collaborator
There was a problem hiding this comment.
This ... is a cool idea
Merged
chrisguidry
added a commit
that referenced
this pull request
Aug 18, 2025
These fixes seem to get at least one of my systems into a state where it's infinitely looping on perpetual tasks. This may have been due to some of the intermediate problems in 0.9.0 and 0.9.1, but I don't want to take that chance. We'll come back and revisit this. This reverts commit 85f293c. Revert "Fix WRONGTYPE error and add memory leak detection tests (#153)" This reverts commit c2b8187. Revert "Fix race condition in task replacement where duplicate executions occur (#151)" This reverts commit 9dc405c.
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.
Fix race condition in task replacement where duplicate executions occur
Previously,
docket.replace()could not cancel tasks that had already been moved from the queue to the stream by the scheduler, causing duplicate execution when the old task ran alongside the replacement. This happened because:replace()only checked the queue, missing tasks already in the streamThis implements atomic task scheduling and cancellation using Lua scripts that track stream message IDs. The solution:
Comprehensive test coverage includes race condition scenarios and idempotent operations.
Closes #149
🤖 Generated with Claude Code