@@ -946,9 +946,7 @@ async def communicate(self, command_factory: Callable[[Self], BaseCommand[T]]) -
946946 assert command .state == CommandState .NEW , command .state
947947
948948 if self .next_command is not None :
949- self .next_command .result .cancel ()
950- self .next_command .finished .cancel ()
951- self .next_command .set_finished ()
949+ self .next_command ._cancel ()
952950
953951 self .next_command = command
954952
@@ -957,20 +955,14 @@ def previous_command_finished() -> None:
957955 if self .command is not None :
958956 cmd = self .command
959957
960- def cancel_if_cancelled (result : asyncio .Future [T ]) -> None :
961- if result .cancelled ():
962- cmd ._cancel ()
963-
964- cmd .result .add_done_callback (cancel_if_cancelled )
965- cmd ._start ()
958+ if cmd .state == CommandState .NEW :
959+ cmd ._start ()
966960 cmd .add_finished_callback (previous_command_finished )
967961
968- if self .command is None :
969- previous_command_finished ()
970- elif not self .command .result .done ():
971- self .command .result .cancel ()
972- elif not self .command .result .cancelled ():
962+ if self .command is not None :
973963 self .command ._cancel ()
964+ else :
965+ previous_command_finished ()
974966
975967 return await command .result
976968
@@ -1233,7 +1225,12 @@ def set_finished(self) -> None:
12331225 self ._dispatch_finished ()
12341226
12351227 def _cancel (self ) -> None :
1236- if self .state != CommandState .CANCELLING and self .state != CommandState .DONE :
1228+ if self .state == CommandState .NEW :
1229+ self .state = CommandState .DONE
1230+ self .result .cancel ()
1231+ self .finished .cancel ()
1232+ self ._dispatch_finished ()
1233+ elif self .state != CommandState .CANCELLING and self .state != CommandState .DONE :
12371234 assert self .state == CommandState .ACTIVE , self .state
12381235 self .state = CommandState .CANCELLING
12391236 self .cancel ()
0 commit comments