Windows: Fix error if calibre is launched before the previous instance has fully shutdown

This commit is contained in:
Kovid Goyal 2022-01-25 11:09:42 +05:30
parent 4b946c6743
commit 1ec0085eff
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -447,7 +447,11 @@ def run_gui_(opts, args, app, gui_debug=None):
singleinstance_name = 'GUI' singleinstance_name = 'GUI'
def send_message(msg): class FailedToCommunicate(Exception):
pass
def send_message(msg, retry_communicate=False):
try: try:
send_message_in_process(msg) send_message_in_process(msg)
except Exception: except Exception:
@ -455,6 +459,10 @@ def send_message(msg):
try: try:
send_message_in_process(msg) send_message_in_process(msg)
except Exception as err: except Exception as err:
# can happen because the Qt local server pipe is shutdown before
# the single instance mutex is released
if retry_communicate:
raise FailedToCommunicate('retrying')
print(_('Failed to contact running instance of calibre'), file=sys.stderr, flush=True) print(_('Failed to contact running instance of calibre'), file=sys.stderr, flush=True)
print(err, file=sys.stderr, flush=True) print(err, file=sys.stderr, flush=True)
if Application.instance(): if Application.instance():
@ -476,14 +484,14 @@ def shutdown_other():
raise SystemExit(_('Failed to shutdown running calibre instance')) raise SystemExit(_('Failed to shutdown running calibre instance'))
def communicate(opts, args): def communicate(opts, args, retry_communicate=False):
if opts.shutdown_running_calibre: if opts.shutdown_running_calibre:
shutdown_other() shutdown_other()
else: else:
if len(args) > 1: if len(args) > 1:
args[1:] = [os.path.abspath(x) if os.path.exists(x) else x for x in args[1:]] args[1:] = [os.path.abspath(x) if os.path.exists(x) else x for x in args[1:]]
import json import json
if not send_message(b'launched:'+as_bytes(json.dumps(args))): if not send_message(b'launched:'+as_bytes(json.dumps(args)), retry_communicate=retry_communicate):
raise SystemExit(_('Failed to contact running instance of calibre')) raise SystemExit(_('Failed to contact running instance of calibre'))
raise SystemExit(0) raise SystemExit(0)
@ -533,18 +541,24 @@ def main(args=sys.argv):
app, opts, args = init_qt(args) app, opts, args = init_qt(args)
except AbortInit: except AbortInit:
return 1 return 1
with SingleInstance(singleinstance_name) as si: try:
if si and opts.shutdown_running_calibre: with SingleInstance(singleinstance_name) as si:
return 0 if si and opts.shutdown_running_calibre:
run_main(app, opts, args, gui_debug, si) return 0
run_main(app, opts, args, gui_debug, si, retry_communicate=True)
except FailedToCommunicate:
with SingleInstance(singleinstance_name) as si:
if si and opts.shutdown_running_calibre:
return 0
run_main(app, opts, args, gui_debug, si, retry_communicate=False)
if after_quit_actions['restart_after_quit']: if after_quit_actions['restart_after_quit']:
restart_after_quit() restart_after_quit()
def run_main(app, opts, args, gui_debug, si): def run_main(app, opts, args, gui_debug, si, retry_communicate=False):
if si: if si:
return run_gui(opts, args, app, gui_debug=gui_debug) return run_gui(opts, args, app, gui_debug=gui_debug)
communicate(opts, args) communicate(opts, args, retry_communicate)
return 0 return 0