Skip to content

Add activation batching flag with task-local cancellation - #2149

Draft
VegetarianOrc wants to merge 19 commits into
mainfrom
amazzeo/workflow-wait-condition-cancellation
Draft

VegetarianOrc wants to merge 19 commits into
mainfrom
amazzeo/workflow-wait-condition-cancellation

Conversation

@VegetarianOrc

@VegetarianOrc VegetarianOrc commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

Add the internal workflow logic flag APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS (4). The flag is off by default. When the flag is on, the worker does these things:

  • It applies all jobs in an activation before it runs workflow tasks, as flag 2 (PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH) does.
  • It sends cancellation only to the task that receives it. workflow.wait_condition awaits its future directly inside asyncio.timeout, so a shielded wait can finish after the workflow is cancelled.
  • It raises the cancellation when a condition, child workflow result or Nexus operation result becomes ready in the same workflow task as the cancellation.
  • A child workflow start or a Nexus operation start can resolve in the same workflow task as the cancellation. The worker then sends a cancel request to the child or operation before it raises, because the caller gets no handle to cancel it. Flag 2 sends no cancel request. If the child result also resolved in that workflow task, the worker sends no cancel. Core fails the workflow task on a cancel for a terminated child.

Histories that record flag 2 keep their current behavior. The worker does not check or record flag 4 for them. Otherwise the worker checks flag 4 before flag 2. A run whose history holds flag 4 never records flag 2. A new run on a worker with both flags as defaults records flag 4.

In _WorkflowInstanceImpl, the flag attributes are now _single_batch_flag (flag 2) and _apply_jobs_first_flag (flag 4). Start awaits pass the new is_start parameter to _await_temporal_operation. A new helper, _cancellation_wins_over_result, decides whether a cancellation wins over a result that is ready.

The changelog fragment changelog/added/tumbling-job-basket.md lists the effects of flag 4 that users can see. The flag 4 comment in temporalio/_workflow_context.py has a reminder for the release that enables flag 4 by default. That release must name the minimum SDK version that replays flag 4 histories.

This PR replaces #2148. It includes the task-local wait_condition approach from #2124, and it includes #2192, #2193, #2194 and #2195.

Why?

Flag 2 checks for cancellation across the whole workflow. After the workflow is cancelled, that check also cancels waits that asyncio.shield protects (#2116). Histories that 1.36.0 recorded with flag 2 need that behavior to replay, so the fix needs a new flag.

Batching alone changes which result wins when a cancellation and a result arrive in the same workflow task. With separate flags, a worker could use batching without the task-local cancellation handling. One flag prevents that combination.

Without the cancel request on a start, a child or Nexus operation that started with the cancellation kept running. With the default TERMINATE parent close policy, the child was terminated when the parent closed, not cancelled, so its cleanup code did not run.

An older SDK can replay a flag 4 history with different results and no nondeterminism error. The release that enables flag 4 must therefore name the minimum SDK version.

Before the flag order changed, a worker with flag 2 as a default recorded flag 2 on a run whose history held flag 4. The run then changed to flag 2 behavior. Only the private test hook can make flag 2 a default today, so that change is defensive.

Checklist

  1. Closes: N/A. Refs [Feature Request] Public worker option to keep the legacy activation order (workflow logic flag 2) #2116, SDK-456, SDK-604, SDK-605, SDK-606 and SDK-607.

  2. How was this tested:

    • Three cancellation tests run with flag 2 and with flag 4. They cover cancellation before the workflow starts, with a ready condition, and with a child completion. The child completion test also runs with no flag, where the child result still wins.
    • A shielded wait test checks that the cleanup finishes under flag 4. It also checks that flag 4 takes priority when both flags are on.
    • test_workflow_cancel_and_child_start_in_same_activation runs with no flag, flag 2 and flag 4. A fourth case uses flag 4 with the child terminated in the same workflow task. test_workflow_cancel_and_operation_start_in_same_activation (tests/nexus/test_workflow_caller_cancel_in_same_activation.py) runs with no flag, flag 2 and flag 4. Without the start fix, the flag 4 cases fail.
    • The timer options test runs with and without flag 4.
    • test_workflow_cancel_and_operation_completion_in_same_activation and test_workflow_cancel_and_external_signal_in_same_activation put a Nexus operation completion or an external signal result in the same workflow task as the cancellation.
    • test_workflow_handler_wait_condition_after_cancel checks that a signal handler wait can finish after cancellation under flag 4 and not under flag 2. Two of its cases change the worker defaults after the first task.
    • test_workflow_logic_flag_enabled_after_replay runs for flag 2 and flag 4. Its result shows that a flag first enabled on a later task takes effect on that task.
    • Each outcome in these tests sends different commands, so a replay that makes the wrong flag decision fails.
    • The replay ordering test replays flag 2 histories with flag 4 and gets the same order.
    • On this branch after the rebase onto main, on Python 3.13.7: test_workflow.py, test_replayer.py, test_workflow_flags.py and tests/nexus/: 707 passed, 2 skipped.
    • Before the rebase and before Cancel a started child or Nexus operation on start cancellation with flag 4 #2192 to Add missing flag 4 tests #2195: the full poe test suite passed, with 2,028 passed and 10 skipped.
    • poe lint passed. changelog-tool check passed.
  3. Any docs updates needed? No public API docs need updating. The changelog fragment describes the flag and the behavior changes.

Known issue, not in this PR: without any flag, the SDK can send a cancel for a child that terminated in the same workflow task. Core then fails every workflow task with a nondeterminism error. Core's child state machine has no Cancel transition from Terminated.

🤖 Generated with Claude Code

@VegetarianOrc VegetarianOrc changed the title amazzeo/workflow wait condition cancellation Preserve task cancellation when waits complete concurrently Nov 19, 2026
@VegetarianOrc
VegetarianOrc force-pushed the amazzeo/workflow-wait-condition-cancellation branch 2 times, most recently from 0c16a66 to 38e9237 Compare November 21, 2026 01:27
@VegetarianOrc
VegetarianOrc force-pushed the amazzeo/workflow-wait-condition-cancellation branch from f7c9ba8 to a2912e0 Compare November 30, 2026 23:05
@VegetarianOrc
VegetarianOrc removed this pull request from stack #2150 December 1, 2026 00:11
@VegetarianOrc VegetarianOrc changed the title Preserve task cancellation when waits complete concurrently Add activation batching flag with task-local cancellation Dec 1, 2026
@VegetarianOrc
VegetarianOrc changed the base branch from amazzeo/workflow-activation-batch-only to main December 1, 2026 00:14
@VegetarianOrc
VegetarianOrc added this pull request to stack #2196 December 2, 2026 23:39
@VegetarianOrc
VegetarianOrc removed this pull request from stack #2196 December 3, 2026 00:18
@VegetarianOrc
VegetarianOrc added this pull request to stack #2197 December 3, 2026 00:18
VegetarianOrc and others added 16 commits December 2, 2026 16:19
Flag 4 now applies all activation jobs before it runs workflow tasks,
and sends cancellation only to the task that receives it. Remove flag 5.
Batching alone changes which result wins when a cancellation arrives in
the same workflow task, so one flag controls both behaviors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With flag 4, a start that resolves in the same activation as a
cancellation raises CancelledError. The caller gets no handle, so the
SDK now sends the cancel request to the child or operation before it
raises. Flag 2 behavior does not change.

Refs SDK-604.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With flag 4, the start cancel path can run after the child result
resolved in the same activation. Core fails the task on a cancel for
a terminated child, so skip the cancel when the result is known.

Also address review findings in the start cancellation tests:
- Use one worker for the child test, with a short task timeout.
- Add a case where the child terminates in the same activation.
- Skip the child test on the Java test server.
- Run the Nexus test on time-skipping V2, and reuse
  WaitForCancelWorkflow.

Refs SDK-604.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Name the flag 2 and flag 4 attributes after their flags, and name the
start parameter after what the caller awaits. Move the decision that a
cancellation wins over a resolved operation into its own helper, so
_await_temporal_operation has one branch for resolved operations.

Refs SDK-604.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs SDK-604.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
List the flag 4 effects that users can see but the fragment left out:
external and child signals, handle cancel after a result,
all_handlers_finished(), job failures during apply, Python 3.11
timeout rounding, and handler waits after cancellation. Also add a
reminder to name the minimum replay version when flag 4 becomes a
default.

Refs SDK-605.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Address review findings on the flag 4 fragment:
- Split the first entry into three entries.
- Name the exact call or handler that each entry affects.
- Add update handlers to the job failure entry.
- Name asyncio.CancelledError.
- Follow the existing flag reminder form in the TODO.

Refs SDK-605.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The worker checked flag 2 before flag 4. A worker with flag 2 as a
default then recorded flag 2 on a run whose history holds flag 4, and
the run changed to flag 2 behavior. Check flag 4 first when history
does not hold flag 2, and check flag 2 last. A new run on a worker
with both flags as defaults now records flag 4.

Refs SDK-606.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VegetarianOrc and others added 3 commits December 2, 2026 16:20
Address review findings on the flag precedence tests:
- Start the handler wait on the second worker, so the result shows
  which flag the run uses.
- Compare the full set of recorded flags.
- Set both flags explicitly on each worker.
- Remove cases that run the same code path.

Refs SDK-606.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add tests that run with no flag, flag 2 and flag 4:
- A Nexus operation result and a workflow cancellation in one
  activation.
- An external signal and a workflow cancellation in one activation.
- A signal handler wait after workflow cancellation on one worker.
Also run the test for a flag first enabled on a later task with
flag 4. The child start test now ends its hold when the cancellation
arrives, so a regression fails instead of hanging.

Refs SDK-607.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Address review findings on the new flag 4 tests:
- Give each outcome different commands, so replay detects a wrong
  flag decision.
- Use a condition and timeout race in the later task test, so the
  result shows that the flag takes effect there.
- Compare only the two batching flags in recorded flag sets.
- Name the Nexus helpers and handler workflow for their role.

Refs SDK-607.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VegetarianOrc
VegetarianOrc force-pushed the amazzeo/workflow-wait-condition-cancellation branch from 9925e45 to 05dd78e Compare December 3, 2026 00:22
@VegetarianOrc
VegetarianOrc removed this pull request from stack #2197 December 3, 2026 00:37
@VegetarianOrc
VegetarianOrc added this pull request to stack #2199 December 3, 2026 00:37
@VegetarianOrc
VegetarianOrc removed this pull request from stack #2199 December 3, 2026 00:47

This branch has not been deployed

No deployments
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.

1 participant