diff --git a/src/velbustcp/__main__.py b/src/velbustcp/__main__.py index 7417206..8899663 100644 --- a/src/velbustcp/__main__.py +++ b/src/velbustcp/__main__.py @@ -43,8 +43,12 @@ async def stop(self): await self.__bridge.stop() -async def main_async(args=None): - """Main asynchronous method.""" +async def main_async(args=None) -> int: + """Main asynchronous method. + + Returns: + int: The process exit code, non-zero if the bridge failed. + """ parser = argparse.ArgumentParser(description="Velbus communication") parser.add_argument("--settings", help="Settings file", required=False) args = parser.parse_args() @@ -63,6 +67,7 @@ async def main_async(args=None): # Create main class main = Main() + exit_code = 0 try: loop = asyncio.get_event_loop() @@ -74,17 +79,19 @@ async def main_async(args=None): except Exception as e: logger.exception(e) + exit_code = 1 finally: await main.stop() logger.info("Shutdown") + return exit_code # entrypoint for the snap -def main(args=None): +def main(args=None) -> int: """Main method.""" - asyncio.run(main_async(args)) + return asyncio.run(main_async(args)) if __name__ == '__main__': diff --git a/src/velbustcp/lib/connection/tcp/client.py b/src/velbustcp/lib/connection/tcp/client.py index 6156a3d..491c9ac 100644 --- a/src/velbustcp/lib/connection/tcp/client.py +++ b/src/velbustcp/lib/connection/tcp/client.py @@ -1,11 +1,18 @@ import asyncio +import errno import logging -from typing import Any, List +from typing import Any, List, Optional from velbustcp.lib.connection.tcp.clientconnection import ClientConnection from velbustcp.lib.packet.packetparser import PacketParser from velbustcp.lib.signals import on_tcp_receive, on_client_close +CONNECTION_LOST_ERRNOS = {errno.EHOSTUNREACH, errno.ENETUNREACH, errno.ETIMEDOUT} + + +def is_connection_lost(e: Optional[BaseException]) -> bool: + return isinstance(e, ConnectionError) or (isinstance(e, OSError) and e.errno in CONNECTION_LOST_ERRNOS) + class Client: @@ -51,9 +58,16 @@ async def stop(self) -> None: self.__is_active = False self.__logger.info("Closing client connection for %s", self.address()) self.__connection.writer.close() - await self.__connection.writer.wait_closed() - self.__received_packets.clear() - on_client_close.send(self) + + try: + await self.__connection.writer.wait_closed() + except OSError as e: + # wait_closed re-raises the error the transport was closed with, e.g. when the peer reset + # the connection. The connection is gone either way, so still clean up below. + self.__logger.info("Connection for %s was not closed cleanly: %s", self.address(), e) + finally: + self.__received_packets.clear() + on_client_close.send(self) async def send(self, data: bytearray) -> None: """Sends data to the client. @@ -110,8 +124,11 @@ async def __handle_authorization(self) -> bool: return self.__connection.authorization_key == data.decode("utf-8").strip() - except Exception: - self.__logger.exception("Exception during authorization for %s", self.address()) + except Exception as e: + if is_connection_lost(e): + self.__logger.info("Connection lost during authorization for %s: %s", self.address(), e) + else: + self.__logger.exception("Exception during authorization for %s", self.address()) return False @@ -124,8 +141,11 @@ async def __handle_packets(self) -> None: while self.is_active(): try: data = await self.__connection.reader.read(1024) - except Exception: - self.__logger.exception("Exception during packet receiving") + except Exception as e: + if is_connection_lost(e): + self.__logger.info("Connection lost with client %s: %s", self.address(), e) + else: + self.__logger.exception("Exception during packet receiving") return if not data: diff --git a/src/velbustcp/lib/connection/tcp/network.py b/src/velbustcp/lib/connection/tcp/network.py index 44d7d12..f2a0bef 100644 --- a/src/velbustcp/lib/connection/tcp/network.py +++ b/src/velbustcp/lib/connection/tcp/network.py @@ -2,7 +2,7 @@ import ssl import logging from typing import List, Optional -from velbustcp.lib.connection.tcp.client import Client +from velbustcp.lib.connection.tcp.client import Client, is_connection_lost from velbustcp.lib.connection.tcp.clientconnection import ClientConnection from velbustcp.lib.settings.network import NetworkSettings from velbustcp.lib.signals import on_client_close @@ -110,5 +110,11 @@ async def send(self, data: bytearray) -> None: if self.__logger.isEnabledFor(logging.DEBUG): # pragma: no cover self.__logger.debug("[TCP OUT] %s", " ".join(hex(x) for x in data)) - tasks = [client.send(data) for client in self.__clients] - await asyncio.gather(*tasks) + clients = list(self.__clients) + results = await asyncio.gather(*(client.send(data) for client in clients), return_exceptions=True) + + for client, result in zip(clients, results): + if is_connection_lost(result): + self.__logger.warning("Connection lost while sending to client %s: %s", client.address(), result) + elif isinstance(result, Exception): + self.__logger.error("Could not send data to client %s", client.address(), exc_info=result) diff --git a/src/velbustcp/lib/settings/logging.py b/src/velbustcp/lib/settings/logging.py index 8854e6e..d6b1be4 100644 --- a/src/velbustcp/lib/settings/logging.py +++ b/src/velbustcp/lib/settings/logging.py @@ -1,7 +1,7 @@ from typing import Dict # noqa: F401 LOGGING_TYPES = ["debug", "info"] -LOGGING_OUTPUT = ["syslog", "stream"] +LOGGING_OUTPUT = ["syslog", "syslog-local", "stream"] class LoggingSettings(): @@ -26,7 +26,9 @@ def parse(settings_dict): if "output" in settings_dict: if (not settings_dict["output"] in LOGGING_OUTPUT): - raise ValueError("Provided option logging.output incorrect, expected 'syslog' or 'stream', got '{0}'".format(settings_dict["output"])) + raise ValueError( + "Provided option logging.output incorrect, expected 'syslog', 'syslog-local' or 'stream', got '{0}'".format(settings_dict["output"]) + ) settings.output = settings_dict["output"] diff --git a/src/velbustcp/lib/util/util.py b/src/velbustcp/lib/util/util.py index 5a5d59f..2d802e2 100644 --- a/src/velbustcp/lib/util/util.py +++ b/src/velbustcp/lib/util/util.py @@ -1,5 +1,7 @@ import logging import logging.handlers +import os +import socket from typing import List import serial import serial.tools.list_ports @@ -7,6 +9,9 @@ from velbustcp.lib.consts import PRODUCT_IDS +SYSLOG_SOCKETS = ["/dev/log", "/var/run/syslog"] + + def str2bool(v: str) -> bool: return str(v).lower() in ["true", "yes", "y", "t", "1"] @@ -20,8 +25,6 @@ def setup_logging(settings: LoggingSettings) -> logging.Logger: logger = logging.getLogger(settings.name) - print(settings.type) - # Set type if settings.type == "debug": logger.setLevel(logging.DEBUG) @@ -30,12 +33,22 @@ def setup_logging(settings: LoggingSettings) -> logging.Logger: # Set handler handler: logging.Handler + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s') if settings.output == "syslog": handler = logging.handlers.SysLogHandler() + elif settings.output == "syslog-local": + if not hasattr(socket, "AF_UNIX"): + raise ValueError("Logging output 'syslog-local' is not supported on this platform") + + address = next((path for path in SYSLOG_SOCKETS if os.path.exists(path)), SYSLOG_SOCKETS[0]) + handler = logging.handlers.SysLogHandler(address=address) + handler.ident = "velbustcp: " + handler.append_nul = False + # The local syslog adds its own timestamp + formatter = logging.Formatter('%(levelname)s - %(module)s - %(message)s') else: handler = logging.StreamHandler() - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) diff --git a/tests/velbustcp/lib/connection/tcp/test_client.py b/tests/velbustcp/lib/connection/tcp/test_client.py index 8cc6f57..8b6fb8b 100644 --- a/tests/velbustcp/lib/connection/tcp/test_client.py +++ b/tests/velbustcp/lib/connection/tcp/test_client.py @@ -1,4 +1,6 @@ import asyncio +import errno +import logging import pytest from velbustcp.lib.connection.tcp.client import Client from pytest_mock import MockFixture, MockerFixture @@ -239,3 +241,74 @@ async def test_client_send_not_own_packet(mocker: MockerFixture): await client.stop() await task + + +CONNECTION_LOST_ERRORS = [ + ConnectionResetError(errno.ECONNRESET, "Connection reset by peer"), + OSError(errno.EHOSTUNREACH, "No route to host"), + OSError(errno.ENETUNREACH, "Network is unreachable"), + OSError(errno.ETIMEDOUT, "Connection timed out"), +] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("error", CONNECTION_LOST_ERRORS) +async def test_connection_reset(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, error: OSError): + # Create connection where the peer is gone, which is also what wait_closed reports + conn = get_mock_connection(mocker) + conn.reader.read = mocker.AsyncMock(side_effect=error) + conn.writer.wait_closed = mocker.AsyncMock(side_effect=error) + conn.should_authorize = False + + closed = [] + + def handle_client_close(sender, **kwargs): + closed.append(sender) + + on_client_close.connect(handle_client_close) + + # Must not raise, and must still report the client as closed + client = Client(conn) + await client.start() + + assert not client.is_active() + assert client in closed + assert not [r for r in caplog.records if r.levelno >= logging.ERROR] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("error", CONNECTION_LOST_ERRORS) +async def test_auth_connection_reset(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, error: OSError): + conn = get_mock_connection(mocker) + conn.reader.read = mocker.AsyncMock(side_effect=error) + conn.writer.wait_closed = mocker.AsyncMock(side_effect=error) + conn.should_authorize = True + conn.authorization_key = "velbus" + + closed = [] + + def handle_client_close(sender, **kwargs): + closed.append(sender) + + on_client_close.connect(handle_client_close) + + client = Client(conn) + await client.start() + + assert not client.is_active() + assert client in closed + assert not [r for r in caplog.records if r.levelno >= logging.ERROR] + + +@pytest.mark.asyncio +async def test_unexpected_os_error(mocker: MockerFixture, caplog: pytest.LogCaptureFixture): + # OSErrors that do not indicate a lost connection must still be reported as errors + conn = get_mock_connection(mocker) + conn.reader.read = mocker.AsyncMock(side_effect=OSError(errno.EBADF, "Bad file descriptor")) + conn.should_authorize = False + + client = Client(conn) + await client.start() + + assert not client.is_active() + assert [r for r in caplog.records if r.levelno >= logging.ERROR] diff --git a/tests/velbustcp/lib/connection/tcp/test_network.py b/tests/velbustcp/lib/connection/tcp/test_network.py index 6efbf34..0d5f5e1 100644 --- a/tests/velbustcp/lib/connection/tcp/test_network.py +++ b/tests/velbustcp/lib/connection/tcp/test_network.py @@ -1,4 +1,5 @@ import asyncio +import errno import pytest from pytest_mock import MockFixture @@ -54,3 +55,31 @@ async def test_send_not_active(mocker: MockFixture): # Assert spy.assert_called_once() assert not spy.spy_return + + +@pytest.mark.asyncio +@pytest.mark.parametrize("error", [ + ConnectionResetError(104, "Connection reset by peer"), + OSError(errno.EHOSTUNREACH, "No route to host"), + RuntimeError("Thrown on purpose"), +]) +async def test_send_client_failure(mocker: MockFixture, error: Exception): + + # Arrange + network = Network(options=NetworkSettings()) + mocker.patch.object(network, 'is_active', return_value=True) + + failing = mocker.Mock() + failing.send = mocker.AsyncMock(side_effect=error) + healthy = mocker.Mock() + healthy.send = mocker.AsyncMock() + network._Network__clients = [failing, healthy] # type: ignore[attr-defined] + + data = bytearray([0x0F]) + + # Act, must not raise + await network.send(data) + + # Assert, the healthy client still got the data + failing.send.assert_awaited_once_with(data) + healthy.send.assert_awaited_once_with(data) diff --git a/tests/velbustcp/lib/util/test_util.py b/tests/velbustcp/lib/util/test_util.py index 4a90b8d..f7a39eb 100644 --- a/tests/velbustcp/lib/util/test_util.py +++ b/tests/velbustcp/lib/util/test_util.py @@ -1,6 +1,8 @@ import logging import logging.handlers +import pytest + from velbustcp.lib.settings.logging import LoggingSettings from velbustcp.lib.util.util import setup_logging, str2bool @@ -43,3 +45,37 @@ def test_output_syslog(): logger = setup_logging(settings) assert logger.name == settings.name assert isinstance(logger.handlers[0], logging.handlers.SysLogHandler) + assert logger.handlers[0].address == ("localhost", logging.handlers.SYSLOG_UDP_PORT) + + +@pytest.mark.parametrize("existing,expected", [ + (["/dev/log", "/var/run/syslog"], "/dev/log"), + (["/var/run/syslog"], "/var/run/syslog"), + ([], "/dev/log"), +]) +def test_output_syslog_local(mocker, existing, expected): + mocker.patch("velbustcp.lib.util.util.socket", spec=["AF_UNIX"]) + mocker.patch("velbustcp.lib.util.util.os.path.exists", side_effect=lambda path: path in existing) + handler_class = mocker.patch("velbustcp.lib.util.util.logging.handlers.SysLogHandler") + handler_class.return_value = logging.NullHandler() + + settings = LoggingSettings() + settings.name = "test-logger-syslog-local-" + expected + settings.output = "syslog-local" + + logger = setup_logging(settings) + handler_class.assert_called_once_with(address=expected) + assert logger.handlers[0].ident == "velbustcp: " + assert logger.handlers[0].append_nul is False + + +def test_output_syslog_local_unsupported(mocker): + + mocker.patch("velbustcp.lib.util.util.socket", spec=[]) + + settings = LoggingSettings() + settings.name = "test-logger-syslog-local-unsupported" + settings.output = "syslog-local" + + with pytest.raises(ValueError): + setup_logging(settings)