agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: McpClientWrapper.close() blocks forever when the MCP server stops responding

Open lwx11111 opened this issue 1 month ago • 0 comments

Describe the bug

McpClientWrapper.close() can block the calling thread forever when the MCP server stops responding:

  • McpAsyncClientWrapper.close() waits on client.closeGracefully() with no timeout, so when the graceful-close future never completes the forceful client.close() fallback is never reached.
  • McpSyncClientWrapper.close() calls the blocking McpSyncClient.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

  1. 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).
  2. Initialize it and issue some tool calls.
  3. Close it, e.g. client.close() / reActAgent teardown / 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.

lwx11111 avatar Oct 08 '26 08:10 lwx11111