Skip to content

Commit bf7d169

Browse files
committed
wip cancel engine commands
1 parent aa98f31 commit bf7d169

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

chess/engine.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3142,6 +3142,27 @@ def test_sf_quit(self):
31423142
with self.assertRaises(chess.engine.EngineTerminatedError), engine:
31433143
engine.ping()
31443144

3145+
@catchAndSkip(FileNotFoundError, "need stockfish")
3146+
def test_sf_cancel(self):
3147+
class TerminateTaskGroup(Exception):
3148+
pass
3149+
3150+
async def terminate_task_group():
3151+
await asyncio.sleep(0.001)
3152+
raise TerminateTaskGroup()
3153+
3154+
async def main():
3155+
try:
3156+
async with asyncio.TaskGroup() as group:
3157+
_, engine = await chess.engine.popen_uci("stockfish")
3158+
group.create_task(engine.analyse(chess.Board(), chess.engine.Limit()))
3159+
group.create_task(engine.analyse(chess.Board(), chess.engine.Limit()))
3160+
group.create_task(terminate_task_group())
3161+
except* TerminateTaskGroup:
3162+
pass
3163+
3164+
asyncio.run(main())
3165+
31453166
@catchAndSkip(FileNotFoundError, "need fairy-stockfish")
31463167
def test_fairy_sf_initialize(self):
31473168
with chess.engine.SimpleEngine.popen_uci("fairy-stockfish", setpgrp=True, debug=True):

0 commit comments

Comments
 (0)