@@ -2035,7 +2035,7 @@ async def run_activity() -> Any:
20352035 try :
20362036 return await self ._await_temporal_operation (
20372037 handle ._result_fut ,
2038- lambda _err , command : handle ._apply_cancel_command ( command ),
2038+ lambda _err : handle ._request_cancel ( ),
20392039 completed_cancellation_flag = _WorkflowLogicFlag .RAISE_ON_CANCELLING_COMPLETED_ACTIVITY ,
20402040 )
20412041 except _ActivityDoBackoffError as err :
@@ -2106,8 +2106,8 @@ async def _outbound_start_child_workflow(
21062106 # Common code for handling cancel for start and run
21072107 def apply_child_cancel_error (
21082108 err : asyncio .CancelledError ,
2109- cancel_command : temporalio .bridge .proto .workflow_commands .WorkflowCommand ,
21102109 ) -> None :
2110+ cancel_command = self ._add_command ()
21112111 # Send a cancel request to the child, forwarding the msg passed to
21122112 # Task.cancel(msg) (if any) as the cancellation reason.
21132113 reason = err .args [0 ] if err .args and isinstance (err .args [0 ], str ) else ""
@@ -2171,7 +2171,7 @@ async def operation_handle_fn() -> OutputT:
21712171 OutputT ,
21722172 await self ._await_temporal_operation (
21732173 handle ._result_fut ,
2174- lambda _err , command : handle ._apply_cancel_command (command ),
2174+ lambda _err : handle ._apply_cancel_command (self . _add_command () ),
21752175 ),
21762176 )
21772177
@@ -2194,7 +2194,7 @@ async def operation_handle_fn() -> OutputT:
21942194
21952195 await self ._await_temporal_operation (
21962196 handle ._start_fut ,
2197- lambda _err , command : handle ._apply_cancel_command (command ),
2197+ lambda _err : handle ._apply_cancel_command (self . _add_command () ),
21982198 reraise_on_workflow_cancellation = True ,
21992199 )
22002200 return handle
@@ -2252,10 +2252,7 @@ async def _await_temporal_operation(
22522252 self ,
22532253 fut : asyncio .Future [_T ],
22542254 apply_cancel : Callable [
2255- [
2256- asyncio .CancelledError ,
2257- temporalio .bridge .proto .workflow_commands .WorkflowCommand ,
2258- ],
2255+ [asyncio .CancelledError ],
22592256 None ,
22602257 ],
22612258 * ,
@@ -2283,7 +2280,7 @@ async def _await_temporal_operation(
22832280 )
22842281 raise
22852282
2286- apply_cancel (err , self . _add_command () )
2283+ apply_cancel (err )
22872284
22882285 # Clear the cancellation counter on Python 3.11+ so the next
22892286 # await does not immediately re-raise CancelledError.
@@ -2798,8 +2795,8 @@ async def _signal_external_workflow(
27982795
27992796 def apply_cancel (
28002797 _err : asyncio .CancelledError ,
2801- command : temporalio .bridge .proto .workflow_commands .WorkflowCommand ,
28022798 ) -> None :
2799+ command = self ._add_command ()
28032800 command .cancel_signal_workflow .seq = seq
28042801
28052802 # Wait until completed or cancelled
@@ -3281,6 +3278,7 @@ def __init__(
32813278 self ._input = input
32823279 self ._result_fut = instance .create_future ()
32833280 self ._started = False
3281+ self ._cancel_command_seq : int | None = None
32843282 instance ._register_task (self , name = f"activity: { input .activity } " )
32853283 self ._payload_converter = self ._instance ._payload_converter_with_context (
32863284 temporalio .converter .ActivitySerializationContext (
@@ -3307,9 +3305,15 @@ def cancel(self, msg: Any | None = None) -> bool:
33073305 # to send a cancel command because the async function won't run to trap
33083306 # the cancel (i.e. cancelled before started)
33093307 if not self ._started and not self .done ():
3310- self ._apply_cancel_command ( self . _instance . _add_command () )
3308+ self ._request_cancel ( )
33113309 return super ().cancel (msg )
33123310
3311+ def _request_cancel (self ) -> None :
3312+ if self ._cancel_command_seq == self ._seq :
3313+ return
3314+ self ._cancel_command_seq = self ._seq
3315+ self ._apply_cancel_command (self ._instance ._add_command ())
3316+
33133317 def _resolve_success (self , result : Any ) -> None :
33143318 # We intentionally let this error if already done
33153319 self ._result_fut .set_result (result )
0 commit comments