Repository navigation
Fix cancelling the task that awaits an external workflow cancel - #1975
Merged
tconley1428 merged 2 commits intoOct 16, 2026
Merged
tconley1428 merged 2 commits into
tconley1428 merged 2 commits into
Conversation
tconley1428
reviewed
Oct 16, 2026
| self._finish = True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( |
Contributor
There was a problem hiding this comment.
These parameters seem to change the test behavior enough that the test becomes somewhat unreadable. They should probably be separate tests.
tconley1428
approved these changes
Oct 16, 2026
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.
Fixes #1974.
_cancel_external_workflowawaits its pending future directly, so when the task awaitingExternalWorkflowHandle.cancel()is cancelled after the request is sent (the workflow is cancelled, anasyncio.timeoutorasyncio.wait_fordeadline expires, or aTaskGroupsibling fails), the future is cancelled too. Core still resolves the request, and_apply_resolve_request_cancel_external_workflowcallsset_resultorset_exceptionon the cancelled future, which raisesInvalidStateError. Every workflow task after that fails with "invalid state" and the workflow staysRUNNING.This change returns early from the resolver when the future is cancelled. Nothing is waiting on it by then, and the request was already sent, so the target is still cancelled. That is the same approach #1352 took for
workflow.sleep()timers (#782), and matches the TypeScript and .NET SDKs, where a resolution that arrives after the caller stopped waiting is a no-op. A future that is done for any other reason still raises, as the existing comment intends.Awaiting the future through
_shield_await, as the other operations do, also fixes the success case, but when the request then fails (target not found), nothing retrieves the exception and the worker logs "Future exception was never retrieved" at ERROR.With default settings
mainfails the workflow task on this path, so no completed history contains it, and a workflow already stuck on it continues once its worker runs this change (a supervisor stuck on amainworker endedCANCELEDafter switching to this branch). Ifworkflow_failure_exception_typescoversInvalidStateError(for example[Exception]),mainfails the workflow instead, and replaying or querying that closed workflow with this change raises a nondeterminism error.Three new tests, with a
TrapCancelWorkflowtarget where there is one:test_workflow_cancel_external_stop_waiting: the workflow stops waiting oncancel()right after sending it, asasyncio.timeout(0)would, and the target still gets the canceltest_workflow_cancel_external_stop_waiting_target_missing: the same against a workflow ID that does not exist, so Core resolves the request with a failure (skipped on time-skipping V1, whose test server never resolves that request)test_workflow_cancel_external_workflow_cancelled: the client cancels the workflow before its first task, so the cancellation arrives in the same activation that sends the request, and the workflow's cleanup is still running when the request resolvesEach one waits for the resolution event in history before letting the workflow finish, then asserts there is no
WORKFLOW_TASK_FAILED. All three fail onmainat the pytest timeout, with "invalid state" on every workflow task retry, and pass with the fix. Checking only beforeset_resultfailstest_workflow_cancel_external_stop_waiting_target_missing.Validation, run locally on macOS (Apple Silicon) with Python 3.12, against the bridge built with
poe build-develop(no Rust changes):poe lint: ruff import and format checks clean, pyright 0 errors, mypy no issues in 277 source files, basedpyright 0 errors, pydocstyle cleanpytest -n 4 --dist=worksteal -o timeout_func_only=false --timeout 180: 1516 passed, 10 skippedpytest tests/worker/test_workflow.py -n 4 --dist=worksteal: 243 passed, 2 skipped, and with--workflow-environment time-skipping-v1: 211 passed, 34 skippedpytest tests/worker/test_workflow.py -k test_workflow_cancel_external_ --workflow-environment time-skipping-v2: 3 passedmain1 of 5 runs and 5 of 5--cancel-firstruns left the supervisorRUNNINGwith "invalid state" task failures, on this branch all 10 endedCANCELEDwith no task failuresNot run: Python versions other than 3.12, Linux and Windows.