From 9da0437e3d8df29c4a49c8a338d20cf11399f100 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 4 Jun 2024 19:52:19 +0200 Subject: [PATCH] Improve exchange_ws shutdown --- freqtrade/exchange/exchange_ws.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/exchange_ws.py b/freqtrade/exchange/exchange_ws.py index 9083bcba3..e84c04d88 100644 --- a/freqtrade/exchange/exchange_ws.py +++ b/freqtrade/exchange/exchange_ws.py @@ -45,10 +45,13 @@ class ExchangeWS: self._klines_watching.clear() for task in self._background_tasks: task.cancel() - if hasattr(self, "_loop"): + if hasattr(self, "_loop") and not self._loop.is_closed(): self.reset_connections() self._loop.call_soon_threadsafe(self._loop.stop) + time.sleep(0.1) + if not self._loop.is_closed(): + self._loop.close() self._thread.join() logger.debug("Stopped") @@ -57,7 +60,7 @@ class ExchangeWS: """ Reset all connections - avoids "connection-reset" errors that happen after ~9 days """ - if hasattr(self, "_loop"): + if hasattr(self, "_loop") and not self._loop.is_closed(): logger.info("Resetting WS connections.") asyncio.run_coroutine_threadsafe(self._cleanup_async(), loop=self._loop) while not self.__cleanup_called: