eca
eca copied to clipboard
MCP client shutdown is sequential and can block for N×10 seconds
Problem
The shutdown! function in src/eca/features/tools/mcp.clj:433-438 uses doseq to close MCP clients sequentially:
(defn shutdown! [db*]
(try
(doseq [[_name {:keys [client]}] (:mcp-clients @db*)]
(.closeGracefully ^McpSyncClient client))
(catch Exception _ nil))
(swap! db* assoc :mcp-clients {}))
Each .closeGracefully has a default timeout of 10 seconds (MCP SDK default). With N clients, worst-case shutdown time is N × 10 seconds.
Impact
This compounds with the thread shutdown issue mentioned in previous discussions - slow MCP shutdown makes ECA unresponsive when stopping.
Proposed Solution
Use parallel shutdown with timeout:
(defn shutdown! [db*]
(try
(let [clients (vals (:mcp-clients @db*))
futures (doall
(map (fn [{:keys [^McpSyncClient client]}]
(future
(try (.closeGracefully client)
(catch Exception _ nil))))
clients))]
;; Wait max 5 seconds for all clients
(doseq [f futures]
(try (deref f 5000 nil) (catch Exception _ nil))))
(catch Exception _ nil))
(swap! db* assoc :mcp-clients {}))
Benefits:
- Concurrent shutdown (total time ~5s max regardless of client count)
- Bounded timeout prevents hanging
- Graceful shutdown still attempted for each client