From 0f7ad23e3c2ff362f0794702d7e0697db0afc23e Mon Sep 17 00:00:00 2001 From: tomaae <23486452+tomaae@users.noreply.github.com> Date: Fri, 13 Mar 2020 02:39:35 +0100 Subject: [PATCH] catching socket.timeout using tuple exception #8 --- .../librouteros_custom/connections.py | 4 ++-- custom_components/mikrotik_router/mikrotikapi.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/custom_components/mikrotik_router/librouteros_custom/connections.py b/custom_components/mikrotik_router/librouteros_custom/connections.py index d7cabba..40cd66b 100644 --- a/custom_components/mikrotik_router/librouteros_custom/connections.py +++ b/custom_components/mikrotik_router/librouteros_custom/connections.py @@ -22,13 +22,13 @@ def read(self, length): """ data = bytearray() while len(data) != length: + tmp = None try: tmp = self.sock.recv((length - len(data))) except: raise ConnectionClosed('Socket recv failed.') - finally: - data += tmp + data += tmp if not data: raise ConnectionClosed('Connection unexpectedly closed.') return data diff --git a/custom_components/mikrotik_router/mikrotikapi.py b/custom_components/mikrotik_router/mikrotikapi.py index 391cbf4..b52c314 100644 --- a/custom_components/mikrotik_router/mikrotikapi.py +++ b/custom_components/mikrotik_router/mikrotikapi.py @@ -172,6 +172,19 @@ def path(self, path) -> list: self.lock.release() return None + try: + tuple(response) + except librouteros_custom.exceptions.ConnectionClosed as api_error: + _LOGGER.error("Mikrotik %s error while path %s", self._host, api_error) + self.disconnect() + self.lock.release() + return None + except: + _LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown") + self.disconnect() + self.lock.release() + return None + self.lock.release() return response if response else None