fix(tasks): prevent duplicate dispatch on requeue race#3963
Draft
cursor[bot] wants to merge 1 commit into
Draft
Conversation
When a task was re-queued, callers enqueued it before sending EventTypeRequeued. If EventTypeEmpty was already waiting in queueEvents (the 5s ticker), handleQueue could ClaimAndDequeue the task while it still occupied the running/active sets and spawn a duplicate dispatch. Move enqueue into the EventTypeRequeued handler so it runs only after onTaskStop releases pool bookkeeping. Add a regression test for the concurrent Empty+Requeued ordering. Co-authored-by: Denis Gukov <fiftin@outlook.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.
Bug and impact
When a remote task was re-queued (runner offline, dispatch lost, or all runners busy), callers enqueued it before emitting
EventTypeRequeued. If anEventTypeEmptytick was already waiting inqueueEvents,handleQueuecould process that tick first,ClaimAndDequeuethe task while it still occupied the running/active sets, and spawn a duplicate dispatch — potentially running the same playbook twice (including on different runners).Root cause
Re-queue paths called
state.Enqueue()synchronously, then sentEventTypeRequeuedto an unbuffered/buffered channel.handleQueueprocesses events in FIFO order, so a queuedEventTypeEmptycould run between enqueue andonTaskStop, violating the invariant that only waiting, non-running tasks enter the claim loop.Fix
Enqueueinto theEventTypeRequeuedhandler, afteronTaskStopreleases running/active/claim state.TaskRunner.run(all runners busy) and both reconciler requeue helpers.dispatchQueueEventfor testability.TestTaskPool_RequeuedEventNotClaimedByConcurrentEmptyPass.Validation
go test ./services/tasks/... -short