MCP client graceful shutdown times out after 10 seconds
Problem
When shutting down MCP clients, closeGracefully() times out after 10 seconds if the server is unresponsive, causing:
[MCP] Client didn't close within timeout of 10000 ms.
java.lang.IllegalStateException: Timeout on blocking read for 10000000000 NANOSECONDS
This happens in:
stop-server!function when stopping individual MCP serversshutdown!function during application exit
Impact
- Application shutdown is delayed by 10 seconds per unresponsive MCP server
- Error stack traces clutter logs even though we don't need graceful shutdown
- Poor user experience when MCP servers become unresponsive
Solution
Added exception handling with fallback to force close:
- Attempt graceful shutdown with
closeGracefully() - On timeout, log a warning and fall back to
close()(force close) - Continue with cleanup regardless of server response
Files Changed
src/eca/features/tools/mcp.cljstop-server!(line ~367)shutdown!(line ~439)
Testing
- Verify MCP servers shut down cleanly when responsive
- Verify unresponsive servers don't block shutdown
- Check warning logs appear for forced closes
Updated Solution - Ready for Review
The fix has been improved to handle all edge cases properly:
Changes Summary
1. stop-server! (line ~367) - Individual server stop:
(try
(.closeGracefully ^McpSyncClient client)
(catch Exception e
(logger/warn logger-tag (format "MCP server %s graceful close timed out, forcing close" name) e)
(try
(.close ^McpSyncClient client)
(catch Exception _ nil))))
2. shutdown! (lines ~439-456) - Application shutdown:
(let [servers (:mcp-clients @db*)
futures (doall
(pmap (fn [[name {:keys [^McpSyncClient client]}]]
(future
(try
(.closeGracefully client)
(catch Exception e
(logger/warn logger-tag
(format "MCP server %s graceful close timed out during shutdown, forcing close" name) e)
(try
(.close client)
(catch Exception _ nil))))))
servers))]
(doseq [f futures]
(try (deref f 5000 nil) (catch Exception _ nil))))
Key Improvements
| Aspect | Old Code | New Code |
|---|---|---|
| Parallel shutdown | ❌ Sequential | ✅ pmap + future |
| Timeout control | ❌ 10s hardcoded | ✅ 5s via deref |
| Force-close fallback | ❌ None | ✅ try/catch with .close |
| Logging | ❌ Silent on failure | ✅ Named warning logs |
| Error handling | ❌ Exception ignored | ✅ Graceful fallback |
What This Fixes
-
Sequential shutdown delay: Original code blocked for 10s per unresponsive server. Now all servers shut down in parallel with 5s timeout.
-
Hung clients: If
closeGracefully()times out, we now log it and force-close with.close(). -
Debugging: Named log messages make it clear which MCP server caused the timeout.
Testing Needed
- [ ] Responsive MCP servers shut down cleanly
- [ ] Unresponsive servers don't block shutdown (>5s)
- [ ] Warning logs appear for forced closes with server name
- [ ] Multiple unresponsive servers are all handled in parallel (total time ~5s, not N×10s)
Patch: [Attach diff or link to PR]
use opencode + kimi-k2.5 draft it, need expert review
Please avoid sending vibe coded without checking that this was already fixed on #287