From 22371730d6c089f10769b1f6c0d5a2276b5f0866 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 29 Oct 2022 02:58:09 +0000 Subject: [PATCH] daemon.run_daemon: always stop if daemon.taskgroup dies If running in daemon mode, and daemon.taskgroup died, we were not stopping. Instead, we should gracefully stop and exit the process. To reproduce: ``` $ ./run_electrum --testnet -o setconfig rpcport 1 $ ./run_electrum --testnet daemon -v ``` --- electrum/daemon.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/electrum/daemon.py b/electrum/daemon.py index e0c4fc184..f38d2ba11 100644 --- a/electrum/daemon.py +++ b/electrum/daemon.py @@ -278,7 +278,7 @@ class CommandsServer(AuthenticatedServer): site = web.TCPSite(self.runner, self.host, self.port) else: raise Exception(f"unknown socktype '{self.socktype!r}'") - await site.start() + await site.start() # socket = site._server.sockets[0] if self.socktype == 'unix': addr = self.sockpath @@ -517,7 +517,12 @@ class Daemon(Logger): try: self._stopping_soon_or_errored.wait() except KeyboardInterrupt: - asyncio.run_coroutine_threadsafe(self.stop(), self.asyncio_loop).result() + self.logger.info("got KeyboardInterrupt") + # we either initiate shutdown now, + # or it has already been initiated (in which case this is a no-op): + self.logger.info("run_daemon is calling stop()") + asyncio.run_coroutine_threadsafe(self.stop(), self.asyncio_loop).result() + # wait until "stop" finishes: self._stopped_event.wait() async def stop(self):