From 3bf49123f547854a16cbf75a3a6a8d216317c479 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Mon, 11 Apr 2022 04:48:48 -0400 Subject: [PATCH] Better node disconnect handling --- matrix_appservice_kakaotalk/rpc/rpc.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/matrix_appservice_kakaotalk/rpc/rpc.py b/matrix_appservice_kakaotalk/rpc/rpc.py index 3626260..0914015 100644 --- a/matrix_appservice_kakaotalk/rpc/rpc.py +++ b/matrix_appservice_kakaotalk/rpc/rpc.py @@ -144,9 +144,9 @@ class RPCClient: self._reader = r self._writer = w self._read_task = self.loop.create_task(self._try_read_loop()) + await self._raw_request("register", peer_id=self.config["appservice.address"]) self._is_connected.set() self._is_disconnected.clear() - await self.request("register", peer_id=self.config["appservice.address"]) async def disconnect(self) -> None: async with self._connection_lock: @@ -258,9 +258,11 @@ class RPCClient: try: await self._read_loop() except asyncio.CancelledError: - pass + return except: self.log.exception("Fatal error in read loop") + self.log.debug("Reader disconnected") + self._on_disconnect() async def _read_loop(self) -> None: while self._reader is not None and not self._reader.at_eof(): @@ -275,9 +277,6 @@ class RPCClient: except asyncio.LimitOverrunError as e: self.log.warning(f"Buffer overrun: {e}") line += await self._reader.read(self._reader._limit) - except ConnectionResetError: - if self._reader is not None: - raise except asyncio.CancelledError: raise if not line: @@ -293,8 +292,6 @@ class RPCClient: raise except: self.log.exception("Failed to handle incoming request %s", line_str) - self.log.debug("Reader disconnected") - self._on_disconnect() async def _raw_request(self, command: str, is_secret: bool = False, **data: JSON) -> asyncio.Future[JSON]: req_id = self._next_req_id