Nicer shutdown of MDNS server

This commit is contained in:
Kovid Goyal 2026-02-17 15:08:37 +05:30
parent 0fedba67a6
commit 8ee4408fe8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 1 deletions

View File

@ -1236,6 +1236,9 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
def shutdown(self, write_settings=True):
from time import monotonic
from calibre.utils.mdns import stop_server_with_joinable
wait_for_mdns_server_to_shutdown = stop_server_with_joinable()
st = monotonic()
timed_print('Shutdown starting...')
self.shutting_down = True
@ -1329,10 +1332,11 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
wait_for_cleanup = cleanup_overseers()
from calibre.live import async_stop_worker
wait_for_stop = async_stop_worker()
timed_print('Waiting for overseers and live to shutdown')
timed_print('Waiting for overseers, mdns and live to shutdown')
self.istores.join()
wait_for_cleanup()
wait_for_stop()
wait_for_mdns_server_to_shutdown()
self.shutdown_completed.emit()
timed_print(f'Shutdown complete in {monotonic()-st:.2f}, quitting...')
try:

View File

@ -211,3 +211,17 @@ def stop_server(wait_for_stop=True):
t.join()
else:
t.join(wait_for_stop)
def stop_server_with_joinable():
global _server
srv, _server = _server, None
if srv is None:
def fake_join(timeout=None):
pass
return fake_join
def shutdown():
srv.close()
t = Thread(target=shutdown, name='CloseMDNSServer', daemon=True)
t.start()
return t.join