Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(webui): quiet non-WebSocket handshake noise on public port
Ho1yShifc062e1af1481
diff --git a/nanobot/webui/websocket_logging.py b/nanobot/webui/websocket_logging.pyindex a9f3ef9b5..8c99f823e 100644--- a/nanobot/webui/websocket_logging.py+++ b/nanobot/webui/websocket_logging.py@@ -4,39 +4,59 @@ from __future__ import annotationsimport logging-from websockets.exceptions import ConnectionClosed+from websockets.exceptions import ConnectionClosed, InvalidMessageOPENING_HANDSHAKE_FAILED_MESSAGE = "opening handshake failed"+# Exceptions that mean the browser hung up mid-handshake (e.g. a restart races an+# open tab) rather than a server fault.+_DISCONNECT_TYPES: tuple[type[BaseException], ...] = (+ BrokenPipeError,+ ConnectionAbortedError,+ ConnectionResetError,+ ConnectionClosed,+ EOFError,+)-def _exception_chain_has_disconnect(exc: BaseException | None) -> bool:+# InvalidMessage is raised when the peer never sends a valid WebSocket/HTTP-GET+# opening handshake: HEAD probes ("unsupported HTTP method; expected GET"),+# port scanners, uptime monitors, and TLS-to-plain-port attempts. On a public+# endpoint (e.g. a Render *.onrender.com service) this is constant background+# noise from the internet, not an operational error.+_MALFORMED_HANDSHAKE_TYPES: tuple[type[BaseException], ...] = (InvalidMessage,)++_SUPPRESSED_TYPES: tuple[type[BaseException], ...] = (+ _DISCONNECT_TYPES + _MALFORMED_HANDSHAKE_TYPES+)+++def _exception_chain_has(exc: BaseException | None, types: tuple[type[BaseException], ...]) -> bool:seen: set[int] = set()while exc is not None:ident = id(exc)if ident in seen:return Falseseen.add(ident)- if isinstance(exc, (- BrokenPipeError,- ConnectionAbortedError,- ConnectionResetError,- ConnectionClosed,- EOFError,- )):+ if isinstance(exc, types):return Trueexc = exc.__cause__ or exc.__context__return Falseclass WebSocketHandshakeNoiseFilter(logging.Filter):- """Suppress restart-time handshakes where the browser already disconnected."""+ """Suppress opening-handshake failures that are peer noise, not server faults.++ Covers browsers that disconnect during a restart and non-WebSocket requests+ (HEAD probes, port scanners, monitors) that hit the public port. Genuine+ server-side handshake errors are left to log.+ """def filter(self, record: logging.LogRecord) -> bool:if record.getMessage() != OPENING_HANDSHAKE_FAILED_MESSAGE:return Trueexc_info = record.exc_infoexc = exc_info[1] if isinstance(exc_info, tuple) and len(exc_info) >= 2 else None- return not _exception_chain_has_disconnect(exc)+ return not _exception_chain_has(exc, _SUPPRESSED_TYPES)def websockets_server_logger() -> logging.Logger:diff --git a/tests/utils/test_webui_websocket_logging.py b/tests/utils/test_webui_websocket_logging.pyindex 28e4c985a..26778cd07 100644--- a/tests/utils/test_webui_websocket_logging.py+++ b/tests/utils/test_webui_websocket_logging.py@@ -4,6 +4,8 @@ from __future__ import annotationsimport logging+from websockets.exceptions import InvalidMessage+from nanobot.webui.websocket_logging import (OPENING_HANDSHAKE_FAILED_MESSAGE,WebSocketHandshakeNoiseFilter,@@ -34,6 +36,23 @@ def test_websocket_handshake_noise_filter_suppresses_disconnects() -> None:assert not filter_.filter(_log_record(OPENING_HANDSHAKE_FAILED_MESSAGE, empty_handshake))+def test_websocket_handshake_noise_filter_suppresses_non_get_probes() -> None:+ """HEAD probes reach the WS port as InvalidMessage wrapping a ValueError."""+ filter_ = WebSocketHandshakeNoiseFilter()+ head_probe = InvalidMessage("did not receive a valid HTTP request")+ head_probe.__cause__ = ValueError("unsupported HTTP method; expected GET; got HEAD")++ assert not filter_.filter(_log_record(OPENING_HANDSHAKE_FAILED_MESSAGE, head_probe))+++def test_websocket_handshake_noise_filter_suppresses_malformed_requests() -> None:+ """Port scanners / TLS-to-plain-port probes raise a bare InvalidMessage."""+ filter_ = WebSocketHandshakeNoiseFilter()+ malformed = InvalidMessage("did not receive a valid HTTP request")++ assert not filter_.filter(_log_record(OPENING_HANDSHAKE_FAILED_MESSAGE, malformed))++def test_websocket_handshake_noise_filter_keeps_real_errors() -> None:filter_ = WebSocketHandshakeNoiseFilter()