diff --git a/changelog/fixed/stubborn-carrier-pigeon.md b/changelog/fixed/stubborn-carrier-pigeon.md new file mode 100644 index 000000000..5902507cb --- /dev/null +++ b/changelog/fixed/stubborn-carrier-pigeon.md @@ -0,0 +1 @@ +**Experimental**: Standalone Nexus operations started with `USE_EXISTING` now attach `completion_callbacks` to an already-running operation instead of silently dropping them. diff --git a/temporalio/client/_impl.py b/temporalio/client/_impl.py index 4877ef22c..767dac356 100644 --- a/temporalio/client/_impl.py +++ b/temporalio/client/_impl.py @@ -1618,6 +1618,11 @@ async def start_nexus_operation( await cb._to_proto(data_converter) for cb in input.completion_callbacks ], ) + # Under the USE_EXISTING conflict policy, attach callbacks to an + # already-running operation instead of dropping them. + if req.completion_callbacks: + req.on_conflict_options.attach_request_id = True + req.on_conflict_options.attach_completion_callbacks = True if input.schedule_to_close_timeout is not None: req.schedule_to_close_timeout.FromTimedelta(input.schedule_to_close_timeout) diff --git a/tests/nexus/test_nexus_handler_callbacks.py b/tests/nexus/test_nexus_handler_callbacks.py index eb318babe..66d36e629 100644 --- a/tests/nexus/test_nexus_handler_callbacks.py +++ b/tests/nexus/test_nexus_handler_callbacks.py @@ -25,7 +25,7 @@ NexusOperationFailureError, WorkflowUpdateStage, ) -from temporalio.common import RawValue +from temporalio.common import NexusOperationIDConflictPolicy, RawValue from temporalio.converter import PayloadCodec from temporalio.exceptions import ApplicationError, CancelledError, TerminatedError from temporalio.nexus import WorkflowRunOperationContext, workflow_run_operation @@ -792,6 +792,59 @@ async def received_callbacks() -> None: assert request.result.value.message == "callback test termination" +@pytest.mark.requires_local_server +async def test_use_existing_attaches_completion_callbacks( + client: Client, env: WorkflowEnvironment +): + """A USE_EXISTING start attaches its callbacks to the already-running operation.""" + if env.supports_time_skipping: + pytest.skip( + "Standalone Nexus Operation tests don't work with time-skipping server" + ) + operation_handler = StandaloneTestServiceHandler() + completion_handler = EchoOnCompleteHandler() + blocking_input = f"use-existing-{uuid.uuid4()}" + operation_id = str(uuid.uuid4()) + async with _callback_workers( + client, env, completion_handler, operation_handler + ) as (endpoint_name, completion_task_queue): + nexus_client = client.create_nexus_client( + service=StandaloneTestService, endpoint=endpoint_name + ) + handles = [ + await nexus_client.start_operation( + StandaloneTestService.blocking_async, + EchoInput(value=blocking_input), + id=operation_id, + id_conflict_policy=NexusOperationIDConflictPolicy.USE_EXISTING, + schedule_to_close_timeout=timedelta(seconds=30), + completion_callbacks=[ + nexus.create_completion_callback( + operation=EchoOnCompleteHandler.on_complete, + task_queue=completion_task_queue, + source_context=CallbackContext(label=label), + ) + ], + ) + for label in ("first", "second") + ] + assert handles[0].run_id == handles[1].run_id + + await asyncio.wait_for(operation_handler.started_blocking.wait(), timeout=10) + await client.get_workflow_handle( + f"blocking_async-{blocking_input}" + ).start_update( + BlockingHandlerWorkflow.unblock, + wait_for_stage=WorkflowUpdateStage.COMPLETED, + ) + assert await handles[1].result() == EchoOutput(value=blocking_input) + + async def received_callbacks() -> None: + assert set(completion_handler.requests) == {"first", "second"} + + await assert_eventually(received_callbacks) + + @pytest.mark.requires_local_server async def test_multiple_completion_callbacks_on_operation( client: Client, env: WorkflowEnvironment