From b8e69e1bc16f10b75fda752b66ae192dac58a39e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 4 Oct 2020 09:23:06 +0530 Subject: [PATCH] Fix calibre-server not exiting on ctrl+c on Windows --- src/calibre/srv/loop.py | 9 +++++++-- src/calibre/srv/standalone.py | 13 +++++++------ src/calibre/srv/utils.py | 9 ++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/calibre/srv/loop.py b/src/calibre/srv/loop.py index 228c7dea20..da5940d05f 100644 --- a/src/calibre/srv/loop.py +++ b/src/calibre/srv/loop.py @@ -763,7 +763,12 @@ class EchoLine(Connection): # {{{ # }}} -if __name__ == '__main__': +def main(): + print('Starting Echo server') s = ServerLoop(EchoLine) - with HandleInterrupt(s.wakeup): + with HandleInterrupt(s.stop): s.serve_forever() + + +if __name__ == '__main__': + main() diff --git a/src/calibre/srv/standalone.py b/src/calibre/srv/standalone.py index b635c5b3f7..7b7361d380 100644 --- a/src/calibre/srv/standalone.py +++ b/src/calibre/srv/standalone.py @@ -20,7 +20,7 @@ from calibre.srv.loop import BadIPSpec, ServerLoop from calibre.srv.manage_users_cli import manage_users_cli from calibre.srv.opts import opts_to_parser from calibre.srv.users import connect -from calibre.srv.utils import RotatingLog +from calibre.srv.utils import HandleInterrupt, RotatingLog from calibre.utils.config import prefs from calibre.utils.localization import localize_user_manual_link from calibre.utils.lock import singleinstance @@ -188,7 +188,7 @@ def main(args=sys.argv): if getattr(opts, 'daemonize', False): raise SystemExit( 'Cannot specify --auto-reload and --daemonize at the same time') - from calibre.srv.auto_reload import auto_reload, NoAutoReload + from calibre.srv.auto_reload import NoAutoReload, auto_reload try: from calibre.utils.logging import default_log return auto_reload(default_log, listen_on=opts.listen_on) @@ -241,7 +241,8 @@ def main(args=sys.argv): # Needed for dynamic cover generation, which uses Qt for drawing from calibre.gui2 import ensure_app, load_builtin_fonts ensure_app(), load_builtin_fonts() - try: - server.serve_forever() - finally: - shutdown_delete_service() + with HandleInterrupt(server.stop): + try: + server.serve_forever() + finally: + shutdown_delete_service() diff --git a/src/calibre/srv/utils.py b/src/calibre/srv/utils.py index 910ea78ff4..d5da92999e 100644 --- a/src/calibre/srv/utils.py +++ b/src/calibre/srv/utils.py @@ -391,7 +391,7 @@ class HandleInterrupt(object): # {{{ # On windows socket functions like accept(), recv(), send() are not # interrupted by a Ctrl-C in the console. So to make Ctrl-C work we have to # use this special context manager. See the echo server example at the - # bottom of this file for how to use it. + # bottom of srv/loop.py for how to use it. def __init__(self, action): if not iswindows: @@ -414,12 +414,7 @@ class HandleInterrupt(object): # {{{ if self.action is not None: self.action() self.action = None - # Typical C implementations would return 1 to indicate that - # the event was processed and other control handlers in the - # stack should not be executed. However, that would - # prevent the Python interpreter's handler from translating - # CTRL-C to a `KeyboardInterrupt` exception, so we pretend - # that we didn't handle it. + return 1 return 0 self.handle = handle