MCP Server Shutdown Tests Expose Timeout and Connection Pitfalls
The author tested an Express-based MCP Server running an eight-second slow_task during deployment shutdowns. After the server received SIGTERM two seconds into the task, one client waited 58 seconds for an already-dead connection before reporting an MCP timeout, while server.close() preserved active requests but waited for connections rather than task completion. A tested shutdown sequence marked the server unhealthy, stopped accepting traffic, waited for tracked in-flight tasks, and actively closed remaining connections.
The measurements show why containerized MCP Servers with long-running tools need explicit task tracking, draining, bounded grace periods, and active connection cleanup rather than relying on server.close() alone.
我模拟了一次发版:一个 8 秒的任务跑到第 2 秒,服务端收到 SIGTERM。
然后我盯着客户端,等它告诉我出事了。
它等了 58 秒,报了一句"请求超时"。
那一刻我意识到,服务端早就没了,客户端却还在等。等到它自己的超时用尽,才给出一个"超时"——而这个错误会让人以为"服务只是慢",于是重试,于是又打到另一个正在被杀的实例。
这篇把发版那一刻的事从头跑了一遍:三种关闭方式,同一个 8 秒的任务,看客户端到底收到什么。
结论先放这儿:server.close() 不是"等我一下",是"等连接关";而客户端分不清"服务死了"和"服务慢了"。