From 89b374c21d9d1bbe5a0c0d379b02a2bdcf69e016 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Sat, 1 Aug 2026 22:11:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(pymsg):=20=E6=8D=95=E8=8E=B7=20UnicodeDecod?= =?UTF-8?q?eError=20=E9=98=B2=E6=AD=A2=E5=90=8C=E6=AD=A5=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit recv() payload.decode("utf-8") 抛出 UnicodeDecodeError(ValueError子类)不在 _handle_client 异常捕获列表中,同步模式(workers=0)下收到无效UTF-8字节会导致整个服务进程崩溃。修复:recv()中将UnicodeDecodeError包装为ConnectionError;_handle_client()异常列表增加防御性兜底。 --- pymsg/pymsg/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pymsg/pymsg/__init__.py b/pymsg/pymsg/__init__.py index 502d5b3..94b8f6d 100644 --- a/pymsg/pymsg/__init__.py +++ b/pymsg/pymsg/__init__.py @@ -110,6 +110,8 @@ def recv( ) payload = _recv_exact(sock, length) return payload.decode("utf-8") + except UnicodeDecodeError: + raise ConnectionError("invalid UTF-8 payload") from None except _socket.timeout: raise TimeoutError( f"recv timed out after {timeout}s" @@ -457,8 +459,10 @@ class MsgServer: def _handle_client(self, s, socketlist, on_message): try: text = recv(s, timeout=self._recv_timeout) - except (ConnectionError, ConnectionResetError, OSError, TimeoutError): + except (ConnectionError, ConnectionResetError, OSError, + TimeoutError, UnicodeDecodeError): # TimeoutError: 客户端在超时时间内未发送完整消息(Slowloris 防护),关闭连接 + # UnicodeDecodeError: 防御性捕获,recv() 已包装为 ConnectionError,此处兜底 self._remove_client(s, socketlist) return addr = self._clients.get(s, ("unknown", 0))