From 44dca9ba037b5090e608b759563e6aa5eed156fa Mon Sep 17 00:00:00 2001 From: Alex Mazzeo Date: Thu, 19 Nov 2026 10:28:26 -0800 Subject: [PATCH 1/2] Add batching-only workflow logic flag --- temporalio/worker/_workflow_instance.py | 19 +++++++--- tests/worker/test_replayer.py | 23 +++++++++++-- tests/worker/test_workflow.py | 46 +++++++++++++++++-------- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index bf7c6a4273..c231227807 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -500,9 +500,17 @@ def activate( self._is_replaying = act.is_replaying self._current_thread_id = threading.get_ident() self._current_internal_flags = act.available_internal_flags - self._single_batch_activation = self._workflow_logic_flag_enabled( + # The original flag also changed cancellation behavior. Histories using + # it must retain those semantics; the new flag only changes scheduling. + self._single_batch_cancellation = self._workflow_logic_flag_enabled( _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH ) + single_batch_activation = ( + self._single_batch_cancellation + or self._workflow_logic_flag_enabled( + _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY + ) + ) activation_err: Exception | None = None try: # Split into job sets with patches, then signals + updates, then @@ -532,7 +540,7 @@ def activate( if start_job: self._workflow_input = self._make_workflow_input(start_job) - if self._single_batch_activation: + if single_batch_activation: # Applying every job before giving workflow tasks a chance to # run prevents their order in the activation from hiding state # that arrived in the same workflow task. @@ -700,7 +708,7 @@ def _apply_cancel_workflow( # workflow the ability to receive the cancellation, so we must defer # this cancellation to the next iteration of the event loop. self.call_soon(self._primary_task.cancel) - elif self._single_batch_activation: + elif self._single_batch_cancellation: # Initialization is the only job that creates the primary task, so # retain a same-activation cancellation until that task exists. self._cancel_primary_task_pending = True @@ -2028,7 +2036,7 @@ async def workflow_wait_condition( # cancellation and keep going. def cancellation_arrived() -> bool: return ( - self._single_batch_activation + self._single_batch_cancellation and not cancellation_requested_before and self._cancel_reason is not None ) @@ -2423,7 +2431,7 @@ async def _await_temporal_operation( if fut.done(): # Retrying the shield after both futures become ready would # return the result and erase the task cancellation. - if self._single_batch_activation: + if self._single_batch_cancellation: raise if completed_cancellation_flag is not None and ( not self._is_replaying @@ -4266,6 +4274,7 @@ class _WorkflowLogicFlag(IntEnum): RAISE_ON_CANCELLING_COMPLETED_ACTIVITY = 1 PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH = 2 + PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY = 3 _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS: frozenset[_WorkflowLogicFlag] = frozenset() diff --git a/tests/worker/test_replayer.py b/tests/worker/test_replayer.py index b0274ca46b..db67922b74 100644 --- a/tests/worker/test_replayer.py +++ b/tests/worker/test_replayer.py @@ -557,18 +557,37 @@ async def test_replayer_async_ordering( Verify legacy and single-batch histories replay with the asyncio scheduling order that their original executions observed. """ + global test_replayer_workflow_res + with Path(__file__).with_name(history_filename).open() as f: history = WorkflowHistory.from_json("fake", f.read()) assert ( _history_uses_workflow_logic_flag(history, _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG) is uses_single_batch ) - await Replayer( + replayer = Replayer( workflows=[SignalsActivitiesTimersUpdatesTracingWorkflow], interceptors=[WorkerWorkflowResultInterceptor()], - ).replay_workflow(history) + ) + test_replayer_workflow_res = None + await replayer.replay_workflow(history) assert test_replayer_workflow_res == expected + if uses_single_batch: + # These histories do not exercise cancellation, so the batching-only + # flag must replay them with the same scheduling order. + for event in history.events: + if event.HasField("workflow_task_completed_event_attributes"): + flags = event.workflow_task_completed_event_attributes.sdk_metadata.lang_used_flags + if _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG in flags: + flags.remove(_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG) + flags.append( + _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY + ) + test_replayer_workflow_res = None + await replayer.replay_workflow(history) + assert test_replayer_workflow_res == expected + async def test_replayer_unflagged_history_uses_legacy_async_ordering() -> None: with ( diff --git a/tests/worker/test_workflow.py b/tests/worker/test_workflow.py index da5688215b..6fbdccfd01 100644 --- a/tests/worker/test_workflow.py +++ b/tests/worker/test_workflow.py @@ -1518,11 +1518,19 @@ async def run(self) -> str: _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG = ( _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH ) +_BATCH_ONLY_WORKFLOW_LOGIC_FLAG = ( + _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY +) -def test_single_batch_workflow_activation_jobs_default_disabled() -> None: +@pytest.mark.parametrize( + "flag", [_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, _BATCH_ONLY_WORKFLOW_LOGIC_FLAG] +) +def test_single_batch_workflow_activation_jobs_default_disabled( + flag: _WorkflowLogicFlag, +) -> None: assert ( - _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG + flag not in temporalio.worker._workflow_instance._DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS ) @@ -1781,8 +1789,15 @@ def child_started(self) -> bool: return self._child_started +@pytest.mark.parametrize( + "flag", + [ + pytest.param(_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, id="with-cancellation-changes"), + pytest.param(_BATCH_ONLY_WORKFLOW_LOGIC_FLAG, id="batch-only"), + ], +) async def test_workflow_cancel_and_child_completion_in_same_activation( - client: Client, + client: Client, flag: _WorkflowLogicFlag ): parent_task_queue = str(uuid.uuid4()) child_task_queue = str(uuid.uuid4()) @@ -1795,9 +1810,6 @@ async def test_workflow_cancel_and_child_completion_in_same_activation( CompleteChildOnSignalWorkflow, task_queue=child_task_queue, ) - child_worker._set_default_workflow_logic_flag( - _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True - ) async with child_worker: handle = await client.start_workflow( CancelAtChildCompletionWorkflow.run, @@ -1815,9 +1827,7 @@ async def test_workflow_cancel_and_child_completion_in_same_activation( # worker stops on the normal queue. max_cached_workflows=0, ) - parent_worker._set_default_workflow_logic_flag( - _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True - ) + parent_worker._set_default_workflow_logic_flag(flag, enabled=True) async with parent_worker: async def child_started() -> bool: @@ -1847,14 +1857,20 @@ async def child_completion_recorded() -> None: task_queue=parent_task_queue, workflow_runner=runner, ) - parent_worker._set_default_workflow_logic_flag( - _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG, enabled=True - ) + parent_worker._set_default_workflow_logic_flag(flag, enabled=True) async with parent_worker: - with pytest.raises(WorkflowFailureError) as err: - await handle.result() - assert isinstance(err.value.cause, CancelledError) + if flag == _BATCH_ONLY_WORKFLOW_LOGIC_FLAG: + # Legacy cancellation handling lets the completed child result win. + assert await handle.result() == "child complete" + else: + with pytest.raises(WorkflowFailureError) as err: + await handle.result() + assert isinstance(err.value.cause, CancelledError) + assert any( + flag in completion.successful.used_internal_flags + for _, completion in runner._pairs + ) assert any( {"resolve_child_workflow_execution", "cancel_workflow"}.issubset( {job.WhichOneof("variant") for job in activation.jobs} From 05879bc2bc26b7e519d93495edb82b0d0eed3304 Mon Sep 17 00:00:00 2001 From: Alex Mazzeo Date: Thu, 19 Nov 2026 10:43:30 -0800 Subject: [PATCH 2/2] Name batching flag for activation ordering --- temporalio/worker/_workflow_instance.py | 4 ++-- tests/worker/test_replayer.py | 2 +- tests/worker/test_workflow.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/temporalio/worker/_workflow_instance.py b/temporalio/worker/_workflow_instance.py index c231227807..94d71983ac 100644 --- a/temporalio/worker/_workflow_instance.py +++ b/temporalio/worker/_workflow_instance.py @@ -508,7 +508,7 @@ def activate( single_batch_activation = ( self._single_batch_cancellation or self._workflow_logic_flag_enabled( - _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY + _WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS ) ) activation_err: Exception | None = None @@ -4274,7 +4274,7 @@ class _WorkflowLogicFlag(IntEnum): RAISE_ON_CANCELLING_COMPLETED_ACTIVITY = 1 PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH = 2 - PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY = 3 + APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS = 3 _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS: frozenset[_WorkflowLogicFlag] = frozenset() diff --git a/tests/worker/test_replayer.py b/tests/worker/test_replayer.py index db67922b74..5c80f274d2 100644 --- a/tests/worker/test_replayer.py +++ b/tests/worker/test_replayer.py @@ -582,7 +582,7 @@ async def test_replayer_async_ordering( if _SINGLE_BATCH_WORKFLOW_LOGIC_FLAG in flags: flags.remove(_SINGLE_BATCH_WORKFLOW_LOGIC_FLAG) flags.append( - _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY + _WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS ) test_replayer_workflow_res = None await replayer.replay_workflow(history) diff --git a/tests/worker/test_workflow.py b/tests/worker/test_workflow.py index 6fbdccfd01..f2acbc7685 100644 --- a/tests/worker/test_workflow.py +++ b/tests/worker/test_workflow.py @@ -1519,7 +1519,7 @@ async def run(self) -> str: _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH ) _BATCH_ONLY_WORKFLOW_LOGIC_FLAG = ( - _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH_ONLY + _WorkflowLogicFlag.APPLY_ACTIVATION_JOBS_BEFORE_RUNNING_TASKS )