Repository navigation
fix: end tracking of stuck CANCELING tasks - #450
Conversation
A task still `CANCELING` `polling.max_cancel_time` seconds after its cancel (new setting, default 3600) is set to `SYSTEM_ERROR`, no longer tracked (no poll claims it, also not a redelivered one), and logged with a warning; before, a decommissioned or failing backend kept it polled forever. A final state that the backend reports wins. Cancels store their time (`tracking.canceled_at`); for tasks canceled by earlier versions, the time counts from when the workers resume the tracking. Documented in the configuration reference, task tracking, the API table, the base config, the chart values and the upgrade notes. Closes #437 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A poll running while another delivery ends the tracking of a task stores no unfinished state over its `SYSTEM_ERROR`, so the task is never resumed. Tests of the race, of a backend that no longer has the task after the limit, and of the limits of the setting; the stamping of earlier cancels is a helper of its own; one-year limit as a constant; docs say when exactly tracking ends. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A worker of an earlier version can store an unfinished state over a task whose cancel timed out during a rolling upgrade. Skip tasks whose tracking ended when resuming tracking, so they are not polled again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideSchema and migrationsStore each cancel timestamp atomically with the
Add the cancellation timestamp and ended-tracking markers, enforce the timeout, and make timeout handling safe across poll deliveries and worker upgrades.
Data structuresDefine
APIDocument the cancellation endpoint’s timeout outcome and explain the new polling configuration setting.
ImplementationExercise timeout validation, final backend outcomes, concurrency protection, legacy cancellation timestamps, and ended-tracking resumption behavior.
ConfigurationProvide a commented example for configuring the cancellation timeout in both the chart values and base configuration.
DocumentationExplain timeout behavior during task tracking and document upgrade effects for cancellations already in progress.
TestsVerify that canceling a task records its cancellation timestamp.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #450 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 2696 2719 +23
=========================================
+ Hits 2696 2719 +23 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 6 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pro_tes/ga4gh/tes/task_runs.py" line_range="1015" />
<code_context>
if db_connector.update_task_state(
state=TesState.CANCELING.value,
if_state=States.TO_CANCEL,
+ fields={CANCELED_AT_FIELD: time.time()},
):
logger.info(
</code_context>
<issue_to_address>
**Accepted cancels never time out**
When a poll changes the task to another unfinished state after the backend accepts cancellation but before `update_task_state` runs, `update_task_state` only matches `States.TO_CANCEL`, so it stores neither `canceled_at` nor `CANCELING`; tracking never applies `max_cancel_time`, and the task remains unfinished indefinitely.
Allow an accepted cancellation to record its timestamp and enter `CANCELING` despite a concurrent unfinished-state update.
</issue_to_address>
### Comment 2
<location path="pro_tes/tasks/track_task_progress.py" line_range="549" />
<code_context>
raise
except Exception as exc:
- if _deleted_after_cancel(collection=collection, worker_id=worker_id, exc=exc):
+ if _deleted_after_cancel(
+ collection=collection, worker_id=worker_id, exc=exc
+ ) or _cancel_timed_out(collection=collection, worker_id=worker_id):
</code_context>
<issue_to_address>
**Deleted tasks remain failed**
When a timeout changes the task to `SYSTEM_ERROR` while another poll handles a backend 404/410, `_deleted_after_cancel` only changes `CANCELING` or `CANCELED` tasks, and `_cancel_timed_out` also returns false, so the task stays `SYSTEM_ERROR` even though the backend confirmed it is gone.
Allow `_deleted_after_cancel` to record `CANCELED` when a timeout has already changed the task to `SYSTEM_ERROR`.
Also at `pro_tes/tasks/track_task_progress.py:500`, `pro_tes/tasks/track_task_progress.py:551`.
</issue_to_address>
### Comment 3
<location path="pro_tes/tasks/track_task_progress.py" line_range="501" />
<code_context>
+ {
+ "$set": {
+ "task.state": TesState.SYSTEM_ERROR.value,
+ POLL_FIELD: TRACKING_ENDED,
+ }
+ },
</code_context>
<issue_to_address>
**Timed-out task returns to RUNNING**
When an earlier-version worker has an in-flight poll when a new worker times out the cancel during a rolling upgrade, the older worker's `_store_state` matches `SYSTEM_ERROR` because its guard excludes only canceled states, then writes `RUNNING` over the timed-out task. The sentinel prevents resumption, so the task remains stored as `RUNNING` instead of `SYSTEM_ERROR`.
Before skipping sentinel tasks during resumption, restore `SYSTEM_ERROR` on any whose state is still unfinished, or drain earlier workers before enabling the timeout.
</issue_to_address>
### Comment 4
<location path="pro_tes/tasks/track_task_progress.py" line_range="749" />
<code_context>
"tes_endpoint.host": {"$nin": ["", None]},
"tes_endpoint.task_id": {"$type": "string"},
+ # also if a worker of an earlier version stored a state over the end
+ POLL_FIELD: {"$ne": TRACKING_ENDED},
"$or": [
{POLLED_AT_FIELD: {"$exists": False}},
</code_context>
<issue_to_address>
**Ended tasks resume without timeout**
When an earlier-version worker overwrites an ended task's state with an unfinished state during a rolling upgrade and then resumes it, the older worker's resume query ignores the sentinel and replaces the ended poll ID; because the task is no longer `CANCELING`, the resumed poller never applies the timeout and tracking continues.
Prevent an older worker's resume from replacing the ended sentinel after it overwrites the task state.
</issue_to_address>
### Comment 5
<location path="pro_tes/tasks/track_task_progress.py" line_range="500-501" />
<code_context>
+ },
+ {
+ "$set": {
+ "task.state": TesState.SYSTEM_ERROR.value,
+ POLL_FIELD: TRACKING_ENDED,
+ }
+ },
</code_context>
<issue_to_address>
**Timed-out poll blocks final-state redelivery**
When one poll delivery receives a final response while another delivery times out the cancellation, and the final-response worker dies before storing the result, `_cancel_timed_out` replaces the poll identifier with `TRACKING_ENDED`. If another delivery has already received a final backend response but its worker dies before storing it, Celery redelivers that poll and `_claim` rejects it because the sentinel no longer matches; the final state is lost and the task remains `SYSTEM_ERROR`.
Ensure an in-flight final response can still be persisted or redelivered after timeout handling, rather than making the timeout sentinel unconditionally reject that poll.
</issue_to_address>
### Comment 6
<location path="pro_tes/ga4gh/tes/task_runs.py" line_range="1015" />
<code_context>
if db_connector.update_task_state(
state=TesState.CANCELING.value,
if_state=States.TO_CANCEL,
+ fields={CANCELED_AT_FIELD: time.time()},
):
logger.info(
</code_context>
<issue_to_address>
**Re-cancelled task loses tracking**
When an older worker restores an unfinished state over an ended task during a rolling upgrade, and the caller then cancels that task, `cancel_task` changes the task back to `CANCELING` and stores a new cancel time, but leaves `tracking.poll` as `TRACKING_ENDED`. `_claim` cannot claim that sentinel, and `task__resume_tracking` excludes it, so no poll checks the cancel timeout or records the backend’s final state; the task remains `CANCELING` indefinitely.
When starting a new cancellation, clear the ended marker and ensure the task is eligible for a fresh tracking poll.
</issue_to_address>Linked issues
#437 fix: end tracking of stuck CANCELING tasks · 1 of 1 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Decide and implement or document how tracking ends when a CANCELING task’s backend never answers. | ✅ Met | The configurable timeout ends tracking by setting the task to SYSTEM_ERROR, marking its poll as ended, and logging a warning; legacy cancels are also covered. |
Sourcery assessment
Needs a human reviewer. 6 findings to address first, and after the timeout, proTES stores SYSTEM_ERROR and marks tracking ended; if the backend task is still running, it can continue without being monitored. Reverting the code does not restore tracking for those tasks, and the new default timeout silently determines when this happens.
Blocking findings: pro_tes/ga4gh/tes/task_runs.py:1015, pro_tes/tasks/track_task_progress.py:549, pro_tes/tasks/track_task_progress.py:501, pro_tes/tasks/track_task_progress.py:749, pro_tes/tasks/track_task_progress.py:501, and 1 more
During a rolling upgrade, a worker of an earlier version can store an unfinished state over a task whose cancel timed out, and the task can then be canceled again; no poll tracks it. Each resume now sets such tasks back to SYSTEM_ERROR, with a warning. Also document that the cancel time is stamped on resume when a poll stored CANCELING first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
As decided in #437: a task still
CANCELINGpolling.max_cancel_timeseconds after its cancel is set toSYSTEM_ERROR, no longer tracked, and logged with a warning.custom.controllers.post_task.polling.max_cancel_time(default 3600 seconds, at most a year).tracking.canceled_at), together withCANCELING;DbDocumentConnector.update_task_statetakes furtherfields.404/410(CANCELED), still wins. The task's poll is set to a sentinel, so that no poll claims it, and polls running meanwhile store no unfinished state overSYSTEM_ERROR, so the task is never resumed. If a worker of the earlier version stores a state over the end during a rolling upgrade, each resume sets the task back toSYSTEM_ERROR, and never resumes its tracking.Docs: configuration reference (Tasks), task tracking (operations), API table, base config, chart
values.yamlexample, upgrade notes (To version 0.6.0).Closes #437
Verification
coverage run -m pytest(mongomock, Helm 3.22.1 for the chart tests): 1642 passed, 11 skipped; coverage 100%MONGODB_TEST_URI, ownmongod --nounixsocket): 1652 passed, 1 skipped.semgrep): passmkdocs build --strict: passCI passed, tests on mongomock and MongoDB 7.0, chart, smoke test, docs, image scan), also after the Sourcery fixesReview
Full review (adversarial + quality, 2 rounds). Fixed: a race in which another delivery of the same poll could store an unfinished state over
SYSTEM_ERRORand resume the tracking (regression testtest_cancel_time_exceeded_concurrently); tests of the limits of the setting, and of a404after the limit; the stamping of earlier cancels as a helper with its reason; docs on when exactly tracking ends; resuming skips ended tracking (rolling upgrades with workers of the earlier version).Sourcery: fixed, each resume sets unfinished tasks whose tracking ended back to
SYSTEM_ERROR(a worker of the earlier version may store a state over the end during a rolling upgrade, and the task may be canceled again;test_ended_tracking_stays_failed); documented that the cancel time of a task that a poll set toCANCELINGfirst is stamped on resume.Lane: full review (behaviour change in task tracking).
Declined review findings
Sourcery: a
404/410racing the timeout leavesSYSTEM_ERRORinstead ofCANCELED; a final response lost with its worker while another delivery times out the cancel. Both need concurrent deliveries of one poll after the limit has passed, and both end in the documented final state.Sourcery: a worker of the earlier version resumes a task over the end during a rolling upgrade. That worker tracks it as the earlier version does; this ends with the rollout.
Clock skew between the app (which stores
canceled_at) and the workers (which compare it) shifts the limit by the skew: irrelevant at the default of an hour; the same holds formax_create_time.One canonical wording for the upgrade note: operations and upgrade notes address different readers; the upgrade note links to the operations page.
🤖 Generated with Claude Code