From cdb46ae198149316eaaccc79a4849dfa195aa267 Mon Sep 17 00:00:00 2001 From: Dave MacLachlan Date: Fri, 4 Dec 2026 11:15:24 -0800 Subject: [PATCH] Stop pinning multiple CPU cores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mcp pins multiple CPU cores in MCP/Client.swift after a stdio MCP server subprocess exits or StdioMCPClient is cancelled/disconnected without throwing an error. Client.connect(transport:) wraps `let stream = await connection.receive(); for try await data in stream { ... }` in an infinite `repeat { ... } while true` loop intended to retry transient `EAGAIN` errors. When the transport's `AsyncThrowingStream` finishes cleanly, `for try await data in stream` exits normally, bypasses both catch blocks, and immediately re-enters repeat with zero backoff—re-calling `connection.receive()` on an already-finished stream in a tight busy-loop. --- Sources/MCP/Client/Client.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/MCP/Client/Client.swift b/Sources/MCP/Client/Client.swift index bf07d092..b33cae2b 100644 --- a/Sources/MCP/Client/Client.swift +++ b/Sources/MCP/Client/Client.swift @@ -244,6 +244,9 @@ public actor Client { ) } } + // The receive stream ended cleanly (e.g., EOF or transport disconnect). + // Break out of the outer retry loop rather than spinning on a finished stream. + break } catch let error where MCPError.isResourceTemporarilyUnavailable(error) { try? await Task.sleep(for: .milliseconds(10)) continue