Skip to content

Commit 90a2449

Browse files
committed
Rearrange exception handling to guarantee task removal from dict
1 parent c1f7661 commit 90a2449

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

temporalio/worker/_nexus.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,24 @@ async def _handle_cancel_operation_task(
178178
client=self._client,
179179
).set()
180180
try:
181-
await self._handler.cancel_operation(ctx, request.operation_token)
182-
except BaseException as err:
183-
logger.warning("Failed to execute Nexus cancel operation method")
184-
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
185-
task_token=task_token,
186-
error=await self._handler_error_to_proto(
187-
_exception_to_handler_error(err)
188-
),
189-
)
190-
else:
191-
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
192-
task_token=task_token,
193-
completed=temporalio.api.nexus.v1.Response(
194-
cancel_operation=temporalio.api.nexus.v1.CancelOperationResponse()
195-
),
196-
)
197-
try:
181+
try:
182+
await self._handler.cancel_operation(ctx, request.operation_token)
183+
except BaseException as err:
184+
logger.warning("Failed to execute Nexus cancel operation method")
185+
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
186+
task_token=task_token,
187+
error=await self._handler_error_to_proto(
188+
_exception_to_handler_error(err)
189+
),
190+
)
191+
else:
192+
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
193+
task_token=task_token,
194+
completed=temporalio.api.nexus.v1.Response(
195+
cancel_operation=temporalio.api.nexus.v1.CancelOperationResponse()
196+
),
197+
)
198+
198199
await self._bridge_worker().complete_nexus_task(completion)
199200
except Exception:
200201
logger.exception("Failed to send Nexus task completion")
@@ -220,25 +221,24 @@ async def _handle_start_operation_task(
220221
"""
221222

222223
try:
223-
start_response = await self._start_operation(start_request, headers)
224-
except BaseException as err:
225-
logger.warning("Failed to execute Nexus start operation method")
226-
handler_err = _exception_to_handler_error(err)
227-
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
228-
task_token=task_token,
229-
error=await self._handler_error_to_proto(handler_err),
230-
)
231-
if isinstance(err, concurrent.futures.BrokenExecutor):
232-
self._fail_worker_exception_queue.put_nowait(err)
233-
else:
234-
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
235-
task_token=task_token,
236-
completed=temporalio.api.nexus.v1.Response(
237-
start_operation=start_response
238-
),
239-
)
240-
241-
try:
224+
try:
225+
start_response = await self._start_operation(start_request, headers)
226+
except BaseException as err:
227+
logger.warning("Failed to execute Nexus start operation method")
228+
handler_err = _exception_to_handler_error(err)
229+
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
230+
task_token=task_token,
231+
error=await self._handler_error_to_proto(handler_err),
232+
)
233+
if isinstance(err, concurrent.futures.BrokenExecutor):
234+
self._fail_worker_exception_queue.put_nowait(err)
235+
else:
236+
completion = temporalio.bridge.proto.nexus.NexusTaskCompletion(
237+
task_token=task_token,
238+
completed=temporalio.api.nexus.v1.Response(
239+
start_operation=start_response
240+
),
241+
)
242242
await self._bridge_worker().complete_nexus_task(completion)
243243
except Exception:
244244
logger.exception("Failed to send Nexus task completion")

0 commit comments

Comments
 (0)