Fix calibre-server not exiting on ctrl+c on Windows

This commit is contained in:
Kovid Goyal 2020-10-04 09:23:06 +05:30
parent 2d4c770d3d
commit b8e69e1bc1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 16 additions and 15 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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