Conversation
_RunningActivity.cancel() already skips cancelling the task once the activity is done, but done was only set after the result or failure had been encoded. A cancel from Core (workflow cancel, heartbeat failure, or worker shutdown with the default zero grace period) that arrived while encode() or encode_failure() was awaiting raised CancelledError past the handler's except Exception, so complete_activity_task was never called. Core kept the activity outstanding, wait_all_completed never returned and worker.shutdown() hung. Mark the activity done as soon as its code returns or raises, before encoding, and make wait_all_completed collect task exceptions so it keeps its no-raise contract. Add a regression test with a codec that yields during encode; it fails on main with a 20s shutdown timeout.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The regression test uses a timing-dependent sleep that does not guarantee cancellation occurs during encoding.
Review effort: Balanced
Findings: 1
What changed in this PR
Ensures completed activity outcomes survive cancellation during asynchronous encoding and prevents shutdown task exceptions from escaping.
Changes:
- Marks activity execution complete before encoding its outcome.
- Collects and logs shutdown-time task exceptions.
- Adds regression coverage and a changelog entry.
| File | Description |
|---|---|
temporalio/worker/_activity.py |
Protects outcome reporting and hardens shutdown waiting. |
tests/worker/test_activity.py |
Adds cancellation-during-encoding coverage. |
CHANGELOG.md |
Documents the fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A fixed sleep in encode let a slow enough cancel arrive after encoding had finished, in which case an unfixed worker would also pass. The codec now blocks until the test has seen the "Cancelling activity" log, which the cancel handler emits in the same synchronous block as cancel().
This branch has not been deployed
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.

What was changed
_run_activitymarks the activity done as soon as its code returns or raises, before encoding the result or failure, so_RunningActivity.cancel()no longer cancels the task while the outcome is being reported.wait_all_completedcollects the activity tasks' exceptions instead of letting one escape, as its comment already promised. A regression test uses a payload codec that yields duringencode. CHANGELOG entry under Unreleased.Why
A Core-issued cancel (workflow cancel, heartbeat failure, or worker shutdown with the default zero grace period) that landed while
encodeorencode_failurewas awaiting raisedCancelledErrorpast the handler'sexcept Exception, socomplete_activity_taskwas never called. Core kept the activity outstanding,wait_all_completednever returned andworker.shutdown()hung. Found while reviewing #1837, whose eviction-side cause is moving to sdk-core.Testing
The new test fails on main with a 20s shutdown timeout and passes with the fix; its captured log shows the
WORKER_SHUTDOWNcancel landing mid-encode. Neighbouring shutdown tests pass, also with four workers.poe lint, pyright, mypy and basedpyright clean.