niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.46k stars 531 forks source link

chessengine.close() timeout issue #791

Closed WolfgangFahl closed 3 years ago

WolfgangFahl commented 3 years ago

https://github.com/WolfgangFahl/play-chess-with-a-webcam/commit/a403e80 makes my CI fail on play-chess-with a webcam. I still have to analyze this but i assume the async-Io handling might need changes on my side.

I used to have

def close(self):
        '''
        close the given engine
        '''
        if self.engine is not None:
            try:
                self.engine.quit()
            except concurrent.futures._base.TimeoutError:
                # so what?
                if self.debug:
                    print ("timeout after %1.f secs for %s on close forcing close now ..." % (self.timeout,self.name))
                self.engine.close()    
                pass

what should i do now?

niklasf commented 3 years ago

The exception type now is asyncio.TimeoutError.

Btw. it's also possible to use the engine as a context manager to ensure it gets killed no matter what. For example, the following script will fail due to an uncaught timeout error, but at least shutdown cleanly.

with chess.engine.SimpleEngine.popen_uci(["/bin/bash", "-c", "sleep 1 && echo uciok && sleep 120"]) as engine:
    engine.quit()

Most engines don't do any work at shutdown, so it may also be reasonable to not even call engine.quit().