fix(pymsg): 捕获 UnicodeDecodeError 防止同步模式服务崩溃

recv() payload.decode("utf-8") 抛出 UnicodeDecodeError(ValueError子类)不在 _handle_client 异常捕获列表中,同步模式(workers=0)下收到无效UTF-8字节会导致整个服务进程崩溃。修复:recv()中将UnicodeDecodeError包装为ConnectionError;_handle_client()异常列表增加防御性兜底。
This commit is contained in:
qing committed 2026-08-01 22:11:38 +08:00
1 parent af5cd02663
commit 89b374c21d
1 file changed
+5 -1
+5 -1
View File
@@ -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))