fix: address code review issues for robust cancel#54
Merged
bodymindarts merged 6 commits intotask/robust-cancel-v2-019cf300from Mar 16, 2026
Merged
fix: address code review issues for robust cancel#54bodymindarts merged 6 commits intotask/robust-cancel-v2-019cf300from
bodymindarts merged 6 commits intotask/robust-cancel-v2-019cf300from
Conversation
When a cancel signal fires, both the dispatcher (cooperative cancel) and the monitor task (force-abort timeout) can race to finalize the same job. Both paths call job.cancel_job() which unconditionally pushed ExecutionCancelled + JobCancelled events, leading to duplicate events. Now cancel_job() checks if the job is already cancelled before pushing events, making the operation safely idempotent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Previously, the cancel_job() method for pending jobs performed the DELETE of the execution row on the raw pool, then opened a new transaction to record cancellation events. A crash between the DELETE and the commit would leave the execution row gone but the job entity never marked as cancelled. Now both the DELETE and event recording happen inside the same atomic operation, matching the pattern used by cancel_and_complete_job in the dispatcher. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test A (cooperative cancel): Creates a runner that select!s on cancellation_requested() and returns JobCompletion::Cancelled when triggered. Verifies the job ends up cancelled and completed. Test B (force-abort): Creates a runner that blocks forever (ignores cancellation). Configures a short cancel_timeout (1 second) via JobPollerConfig. Verifies the monitor task force-aborts and records cancellation events. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The cancel_and_complete_job (dispatcher) and force_cancel_job (poller) functions were nearly identical: begin_op → find job → DELETE execution row → cancel_job() → update → commit. If one was updated the other would diverge. Extract finalize_cancelled_job() in repo.rs and call it from both sites. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The sqlx::query_as! macro was replaced with runtime sqlx::query_as when adding the cancelled_at IS NULL filter, losing compile-time schema validation. Restore query_as! with explicit column type overrides (id as "id: JobId") and pass job_type_strings with `as _` cast for the ANY($4) parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The struct is a map from JobId to CancellationToken — it doesn't track general running-job information (that's JobTracker's role). The new name better describes its narrow responsibility. Co-Authored-By: Claude Opus 4.6 <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
Addresses 5 code review issues from PR #52 plus a rename:
Job::cancel_job()now checkscancelled()before pushing events, preventing duplicateExecutionCancelled/JobCancelledevents from the dispatcher + monitor raceselect!oncancellation_requested()) and force-abort (runner blocks forever, 1s cancel timeout triggers abort)finalize_cancelled_job()inrepo.rs, called from bothcancel_and_complete_job(dispatcher) andforce_cancel_job(poller)sqlx::query_as!macro inpoll_jobswith explicit column type overridesRunningJobRegistry→CancellationTokens— the struct only maps job IDs to cancellation tokens, not general running-job infoTest plan
cargo fmtpassescargo clippy --all-targets -- -D warningspassescargo nextest runtest_cooperative_cancelverifies cooperative cancel pathtest_force_abort_cancelverifies force-abort path with short timeout🤖 Generated with Claude Code