Improve exchange_ws shutdown

This commit is contained in:
Matthias
2024-06-04 19:52:19 +02:00
parent 0f9335d242
commit 9da0437e3d

View File

@@ -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: