Skip to content

Commit 14909ae

Browse files
committed
fix: added cleanup code in AsyncClient for the object destruction phase
1 parent 1418b58 commit 14909ae

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

pyinjective/async_client.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ def __init__(
270270
cookie_assistant=network.explorer_cookie_assistant,
271271
)
272272

273+
def __del__(self):
274+
self._cancel_timeout_height_sync_task()
275+
276+
async def close_exchange_channel(self):
277+
await self.exchange_channel.close()
278+
self._cancel_timeout_height_sync_task()
279+
280+
async def close_chain_channel(self):
281+
await self.chain_channel.close()
282+
self._cancel_timeout_height_sync_task()
283+
284+
async def close_chain_stream_channel(self):
285+
await self.chain_stream_channel.close()
286+
self._cancel_timeout_height_sync_task()
287+
273288
async def all_tokens(self) -> Dict[str, Token]:
274289
if self._tokens_by_symbol is None:
275290
async with self._tokens_and_markets_initialization_lock:
@@ -309,14 +324,6 @@ def get_number(self):
309324
async def fetch_tx(self, hash: str) -> Dict[str, Any]:
310325
return await self.tx_api.fetch_tx(hash=hash)
311326

312-
async def close_exchange_channel(self):
313-
await self.exchange_channel.close()
314-
self._cancel_timeout_height_sync_task()
315-
316-
async def close_chain_channel(self):
317-
await self.chain_channel.close()
318-
self._cancel_timeout_height_sync_task()
319-
320327
async def sync_timeout_height(self):
321328
try:
322329
block = await self.fetch_latest_block()
@@ -2568,5 +2575,11 @@ async def _timeout_height_sync_process(self):
25682575

25692576
def _cancel_timeout_height_sync_task(self):
25702577
if self._timeout_height_sync_task is not None:
2571-
self._timeout_height_sync_task.cancel()
2578+
try:
2579+
self._timeout_height_sync_task.cancel()
2580+
asyncio.get_event_loop().run_until_complete(asyncio.wait_for(self._timeout_height_sync_task, timeout=1))
2581+
except Exception as e:
2582+
logger = LoggerProvider().logger_for_class(logging_class=self.__class__)
2583+
logger.warning("error canceling timeout height sync task")
2584+
logger.debug("error canceling timeout height sync task", exc_info=e)
25722585
self._timeout_height_sync_task = None

0 commit comments

Comments
 (0)