521 日 , 2025 16:44:34
MCP-Java-SDK socket fd泄露问题

这个MCP官方的Java SDK调用ServerResponse.sse()提供的SSE服务永远不会关闭socket,客户端关闭TCP连接后一直TIME_WAIT,如果是linux系统fd永远释放不了,直到爆掉上限无法建立网络连接。lsof显示的僵尸fd如下:

java 22507 root 73u sock 0,7 0t0 3469578 protocol: TCPv6

(一样的代码,windows系统居然会在某种条件下一段时间后自动关闭,未深究)

 

这个里面调用的是AsyncServerResponse,官方文档如下:

The Servlet API does not provide any notification when a remote client goes away. Therefore, while streaming to the response, whether through SseEmitter or reactive types, it is important to send data periodically, since the write fails if the client has disconnected. The send could take the form of an empty (comment-only) SSE event or any other data that the other side would have to interpret as a heartbeat and ignore.

这么大的问题居然一个多月都没修,说明就没有多少人高强度使用(Java)MCP……

另外,靠谱的客户端断连检测只尝试读取socket是不行的,还是要靠心跳机制。可能官方怕MCP客户端不支持ping所以没实现

暂无评论

发送评论 编辑评论