From edddc90d73f2b82dc3eee517dfe6f8d318ff5d64 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 1 Jan 2023 13:16:46 +0100 Subject: [PATCH] Run register_service in a separate thread in `backend.http`. The Zeroconf registration part may randomly get stuck, resulting in the web server not being properly started. It's therefore better to run the Zeroconf registration process asynchronously, for it's not strictly required for the web server to execute. --- platypush/backend/http/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 21c649887..966ba615c 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -251,6 +251,7 @@ class HttpBackend(Backend): self.disable_websocket = disable_websocket self.websocket_thread = None self._websocket_loop = None + self._service_registry_thread = None self.bind_address = bind_address if resource_dirs: @@ -328,6 +329,10 @@ class HttpBackend(Backend): self._websocket_loop.stop() self.logger.info('HTTP websocket service terminated') + if self._service_registry_thread and self._service_registry_thread.is_alive(): + self._service_registry_thread.join(timeout=5) + self._service_registry_thread = None + def _acquire_websocket_lock(self, ws): try: acquire_ok = self._websocket_lock.acquire( @@ -453,14 +458,16 @@ class HttpBackend(Backend): return proc - def run(self): - super().run() + def _register_service(self): try: self.register_service(port=self.port) except Exception as e: self.logger.warning('Could not register the Zeroconf service') self.logger.exception(e) + def run(self): + super().run() + if not self.disable_websocket: self.logger.info('Initializing websocket interface') self.websocket_thread = threading.Thread(target=self.websocket) @@ -483,5 +490,8 @@ class HttpBackend(Backend): + 'for the webserver (e.g. nginx)' ) + self._service_registry_thread = threading.Thread(target=self._register_service) + self._service_registry_thread.start() + # vim:sw=4:ts=4:et: