diff --git a/temporalio/worker/_worker.py b/temporalio/worker/_worker.py index 60f824c4d..ee7ad08b3 100644 --- a/temporalio/worker/_worker.py +++ b/temporalio/worker/_worker.py @@ -731,11 +731,13 @@ def client(self, value: temporalio.client.Client) -> None: Changing the client will make sure the worker starts using it for the next calls it makes. However, outstanding client calls will still - complete with the existing client. The new client cannot be "lazy" and - must be using the same runtime as the current client. + complete with the existing client. The new client cannot be "lazy" and, + when configured with an explicit runtime, must use the current client's + runtime. """ bridge_client = _extract_bridge_client_for_worker(value) - if self._runtime is not bridge_client.config.runtime: + new_runtime = bridge_client.config.runtime + if new_runtime is not None and self._runtime is not new_runtime: raise ValueError( "New client is not on the same runtime as the existing client" ) diff --git a/tests/worker/test_workflow.py b/tests/worker/test_workflow.py index 283357c2d..dd3d1fd5b 100644 --- a/tests/worker/test_workflow.py +++ b/tests/worker/test_workflow.py @@ -6279,6 +6279,22 @@ async def test_workflow_replace_worker_client_diff_runtimes_fail( worker.client = other_client +async def test_workflow_replace_worker_client_implicit_runtime( + env: WorkflowEnvironment, +): + # An implicit runtime must not be resolved just to compare it with the + # worker's explicit runtime. The worker owns the runtime for its bridge + # client replacement. + worker_runtime = Runtime(telemetry=TelemetryConfig()) + worker_client = await env.connect_client(runtime=worker_runtime) + default_runtime_client = await Client.connect( + worker_client.service_client.config.target_host, + namespace=worker_client.namespace, + ) + async with new_worker(worker_client, HelloWorkflow) as worker: + worker.client = default_runtime_client + + @activity.defn(dynamic=True) async def return_name_activity(_args: Sequence[RawValue]) -> str: return activity.info().activity_type