[Bug]: McpClientWrapper.close() blocks forever when the MCP server stops responding
Describe the bug
McpClientWrapper.close() can block the calling thread forever when the MCP server stops responding:
McpAsyncClientWrapper.close()waits onclient.closeGracefully()with no timeout, so when the graceful-close future never completes the forcefulclient.close()fallback is never reached.McpSyncClientWrapper.close()calls the blockingMcpSyncClient.closeGracefully()with no ceiling either, so the same hang is possible for sync clients.
close() is reached from the shared agent/toolkit teardown paths (e.g. Toolkit.closeMcpClients()), so a single unresponsive server can park a request thread indefinitely.
To Reproduce
- Build a client with
McpClientBuilder.create("my-mcp").streamableHttpTransport(url).buildAsync(), pointing at a server that later becomes unresponsive (keeps the connection but stops answering requests). - Initialize it and issue some tool calls.
- Close it, e.g.
client.close()/reActAgentteardown /Toolkit.closeMcpClients().
Expected behavior
close() returns within a bounded time and still releases the underlying transport, whether or not the server is responsive.
Error messages
No exception is raised — the caller just parks. Thread dump of a stuck close:
"<app-thread>" #49 prio=5 os_prio=0 tid=0x... nid=0x... waiting on condition
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(Native Method)
- parking to wait for <0x...> (a java.util.concurrent.CompletableFuture$Signaller)
at java.util.concurrent.CompletableFuture$Signaller.block(CompletableFuture.java:1864)
at java.util.concurrent.CompletableFuture.join(CompletableFuture.java:2117)
...
In a high-QPS deployment each stuck close also keeps the MCP HTTP transport reachable from ForkJoinPool.commonPool (... -> HttpClientStreamableHttpTransport -> HttpClientTransport -> Http1Response -> Http1Exchange); jmap -histo:live showed 228,464 Http1Exchange instances still alive after a Full GC (unchanged count), with the heap pinned until OutOfMemoryError: Java heap space. The fixable defect is the unbounded block itself.
Environment (please complete the following information):
- AgentScope-Java Version: reproduced against 2.0.3, still present on
main(2.0.4-SNAPSHOT) - Java Version: 21
- OS: linux (production); the blocking behaviour is platform independent
Additional context
Fix proposed in #3271: bound the graceful-close wait with a configurable timeout, cancel the pending chain when the wait expires, and keep the forceful client.close() as the release guarantee — applied to both the async and the sync wrapper.