Skip to content

Commit f18b52f

Browse files
committed
Reconnection logic fixed
1 parent 62b70cb commit f18b52f

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@ async def loop_time() -> float:
575575
async def _cancel(self):
576576
try:
577577
self._send_recv_task.cancel()
578-
await self._send_recv_task
579578
await self.ws.close()
580579
except (
581580
AttributeError,
@@ -619,7 +618,7 @@ async def _handler(self, ws: ClientConnection) -> None:
619618
for task in pending:
620619
task.cancel()
621620
for task in done:
622-
if isinstance(task.result(), (asyncio.TimeoutError, ConnectionClosed)):
621+
if isinstance(task.result(), (asyncio.TimeoutError, ConnectionClosed, TimeoutError)):
623622
should_reconnect = True
624623
if should_reconnect is True:
625624
for original_id, payload in list(self._inflight.items()):
@@ -628,7 +627,7 @@ async def _handler(self, ws: ClientConnection) -> None:
628627
await self._sending.put(to_send)
629628
logger.info("Timeout occurred. Reconnecting.")
630629
await self.connect(True)
631-
await self._handler(ws=ws)
630+
await self._handler(ws=self.ws)
632631
elif isinstance(e := recv_task.result(), Exception):
633632
return e
634633
elif isinstance(e := send_task.result(), Exception):
@@ -691,13 +690,16 @@ async def _start_receiving(self, ws: ClientConnection) -> Exception:
691690
)
692691
await self._recv(recd)
693692
except Exception as e:
694-
logger.exception("Start receiving exception", exc_info=e)
695693
if isinstance(e, ssl.SSLError):
696694
e = ConnectionClosed
697-
for fut in self._received.values():
698-
if not fut.done():
699-
fut.set_exception(e)
700-
fut.cancel()
695+
if not isinstance(e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)):
696+
logger.exception("Websocket receiving exception", exc_info=e)
697+
for fut in self._received.values():
698+
if not fut.done():
699+
fut.set_exception(e)
700+
fut.cancel()
701+
else:
702+
logger.warning("Timeout occurred. Reconnecting.")
701703
return e
702704

703705
async def _start_sending(self, ws) -> Exception:
@@ -713,14 +715,19 @@ async def _start_sending(self, ws) -> Exception:
713715
raw_websocket_logger.debug(f"WEBSOCKET_SEND> {to_send}")
714716
await ws.send(to_send)
715717
except Exception as e:
716-
logger.exception("Start sending exception", exc_info=e)
717-
if to_send is not None:
718-
self._received[to_send["id"]].set_exception(e)
719-
self._received[to_send["id"]].cancel()
718+
if isinstance(e, ssl.SSLError):
719+
e = ConnectionClosed
720+
if not isinstance(e, (asyncio.TimeoutError, TimeoutError, ConnectionClosed)):
721+
logger.exception("Websocket sending exception", exc_info=e)
722+
if to_send is not None:
723+
self._received[to_send["id"]].set_exception(e)
724+
self._received[to_send["id"]].cancel()
725+
else:
726+
for i in self._received.keys():
727+
self._received[i].set_exception(e)
728+
self._received[i].cancel()
720729
else:
721-
for i in self._received.keys():
722-
self._received[i].set_exception(e)
723-
self._received[i].cancel()
730+
logger.warning("Timeout occurred. Reconnecting.")
724731
return e
725732

726733
async def send(self, payload: dict) -> str:

0 commit comments

Comments
 (0)