Skip to content

Fix eviction hang when an already-cancelled activity is cancelled again - #1947

Merged
tconley1428 merged 3 commits into
temporalio:mainfrom
jtwillis92:fix-eviction-recancel-hang
Oct 9, 2026
Merged

tconley1428 merged 3 commits into
temporalio:mainfrom
jtwillis92:fix-eviction-recancel-hang

Conversation

@jtwillis92

@jtwillis92 jtwillis92 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1938.

Since #1814, _ActivityHandle._request_cancel returns early when a cancel command was already sent for the current _seq. On eviction, _apply_remove_from_cache cancels every task, and an activity handle task parked in _await_temporal_operation calls _request_cancel again. Up to 1.32.0 that path always called _add_command(), which raises _WorkflowBeingEvictedError while deleting and ends the task. With the early return nothing raises, so the task uncancels and waits on a future that never resolves. Eviction then fails with Eviction processed, but 1 tasks remain (2 when the workflow itself is still awaiting the handle), retries every 2s forever, the run's workflow tasks time out on that worker, and Worker.shutdown() hangs.

To fix it, _request_cancel now skips the dedup while the instance is deleting, so _add_command() raises as it did before 1.33.0. Outside eviction the dedup is unchanged, so #1813 stays fixed, and nothing is emitted during eviction since _add_command() raises first. Local activities share _ActivityHandle and are covered by the same change. Child workflow, Nexus operation, external signal and timer cancellation have no same-sequence dedup like the activity handle (a timer only skips the command once it is no longer pending), so they were not affected.

The retry loop in _handle_cache_eviction is what turns the stuck task into the shutdown hang. That loop is intentional (it keeps a workflow whose tasks are still alive from being torn down by GC), so it is left as is.

test_workflow_cancel_activity_then_evict covers three shapes:

  • an activity cancelled with TRY_CANCEL, then a workflow task failure in the same activation
  • the same with a local activity
  • an activity cancelled with WAIT_CANCELLATION_COMPLETED on a worker with max_cached_workflows=0 (the repro from the issue comments)

The workflow only cancels after the activity reports that it started (the test signals it), so the cancel always reaches a running activity. Each case asserts the workflow completes, _could_not_evict_count stays 0 and worker shutdown finishes. All 3 fail on main at the 60s pytest timeout with a tasks remain error whose stacks include the parked _ActivityHandle, and pass with the fix.

Validation, run locally on Linux x86_64 with Python 3.11. I didn't have a Rust toolchain on this machine, so I ran against the prebuilt 1.34.0 bridge (this is a Python-only change, and unmodified main passes tests/worker/test_workflow.py on that bridge).

  • pytest tests/worker/test_workflow.py: 234 passed, 2 skipped (main: 231 passed, 2 skipped)
  • pytest tests/worker/test_workflow.py -k test_workflow_cancel_activity_then_evict: 3 passed, run twice (about 9s each)
  • pytest tests/worker/test_workflow.py -k test_workflow_cancel_activity --workflow-environment time-skipping: 6 passed
  • pytest tests/worker/test_activity.py: 57 passed
  • ruff check --select I, ruff format --check, pyright, mypy, basedpyright and pydocstyle: clean

I also ran the repro script from the issue against a dev server. Main times out with the run stuck RUNNING, could_not_evict_count: 1 and a hung shutdown, while the fix prints result: done, could_not_evict_count: 0 and worker shutdown: drained, same as 1.32.0. The issue's other shape (a WAIT_CANCELLATION_COMPLETED cancel, then eviction via max_cached_workflows=1 plus a second workflow) and variants with a local activity or a permanent task failure behave the same way, a stuck eviction and hung shutdown on main and a clean eviction and shutdown with the fix.

@CLAassistant

CLAassistant commented Oct 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@jtwillis92
jtwillis92 force-pushed the fix-eviction-recancel-hang branch from c470ec6 to a496674 Compare October 8, 2026 18:43
@jtwillis92

Copy link
Copy Markdown
Contributor Author

Codex review (2 rounds, approved)

I ran a Codex adversarial review on this change. It checked:

  • whether the _deleting bypass in _request_cancel can now emit a command during eviction or raise somewhere it shouldn't (unstarted handle cancel(), activity backoff and its new _seq, the completed-future and single-batch branches)
  • whether setting _cancel_command_seq before the raise matters across a retried eviction
  • whether the new tests fail on main for the stated reason rather than timing
  • the other cancellation paths (child workflow, Nexus operation, external signal, timer, external cancel)
  • test flakiness, runtime and fit with the repo conventions

Round 1 approved a496674 with no blocking findings. It pulled the real _request_cancel, _add_command and _assert_not_read_only out and ran them in isolation. With the fix, 4 of 4 cancels during eviction raise and none emits a command, compared to 1 of 4 on main. Two notes came out of it:

  • The test cancelled after workflow.sleep(0.1), which doesn't guarantee the activity has started (and fires right away under time-skipping). Fixed in 88ffafd: the activity now reports that it started, and only then does the test tell the workflow to cancel. Still fails on main (3 failed at the 60s timeout) and passes with the fix, including twice under time-skipping.
  • The no-cache case on main leaves 2 tasks behind (the run awaiting the handle plus the handle itself), not 1. The description now says so.

Round 2 approved 88ffafd. Its only remaining note was wording: timer cancellation returns early for a timer that is no longer pending, so "unconditionally" in the description was imprecise. That's updated too.

Approved at 88ffafdef52d659281b5e9734db4224be27a9ebf.

@jtwillis92
jtwillis92 marked this pull request as ready for review October 8, 2026 19:20
@jtwillis92
jtwillis92 requested a review from a team as a code owner October 8, 2026 19:20
Comment thread tests/worker/test_workflow.py Resolved
@tconley1428
tconley1428 merged commit ee25ade into temporalio:main Oct 9, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Workflow eviction never completes when an already-cancelled activity handle is cancelled again during eviction (regression in 1.33.0)

3 participants