From c5adc141eaabc0a2494511f923d6a7a4df0e3241 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 6 Feb 2020 19:30:40 +0100 Subject: [PATCH] More robust mechanism for websocket message send section locking --- platypush/backend/http/__init__.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index a92cfcc9c0..c54b9d9664 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -225,12 +225,18 @@ class HttpBackend(Backend): raise TimeoutError('Websocket on address {} not ready to receive data'.format(addr)) def _release_websocket_lock(self, ws): - addr = ws.local_address - if addr in self._websocket_locks: - try: + try: + acquire_ok = self._websocket_lock.acquire(timeout=self._websocket_lock_timeout) + if not acquire_ok: + raise TimeoutError('Websocket lock acquire timeout') + + addr = ws.remote_address + if addr in self._websocket_locks: self._websocket_locks[addr].release() - except RuntimeError: - pass + except Exception as e: + self.logger.warning('Unhandled exception while releasing websocket lock: {}'.format(str(e))) + finally: + self._websocket_lock.release() def notify_web_clients(self, event): """ Notify all the connected web clients (over websocket) of a new event """ @@ -246,8 +252,8 @@ class HttpBackend(Backend): self._release_websocket_lock(ws) loop = get_or_create_event_loop() - wss = self.active_websockets.copy() + for _ws in wss: try: loop.run_until_complete(send_event(_ws))