Repository navigation
Conversation
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.
| ) | ||
| } | ||
| } | ||
| // The receive stream ended cleanly (e.g., EOF or transport disconnect). |
There was a problem hiding this comment.
Minor nit: line 224 (if Task.isCancelled { break }) also exits the for loop to here, so mentioning cancellation keeps the comment accurate for future readers:
| // The receive stream ended cleanly (e.g., EOF or transport disconnect). | |
| // The receive stream ended cleanly (e.g., EOF or transport disconnect) or the task was cancelled. |
| } | ||
| // 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 |
There was a problem hiding this comment.
Great catch on the busy-loop! Could we add a regression test in Tests/MCPTests/ClientTests.swift that fails without this break?
Existing tests didn't catch this because Client.disconnect() cancels the loop task before closing the transport, and MockTransport.receive() returns a brand-new open stream on every call. A small test Transport returning a single stored AsyncThrowingStream (matching StdioTransport/HTTPClientTransport/NetworkTransport) that counts receive() calls and finishes the stream cleanly—or calling disconnect() directly on InMemoryTransport—would verify the loop exits with receive() called only once.
|
Thanks for the report. This part of the client is going away in 1.0. so that PR wont be applicable soon. |
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 infiniterepeat { ... } while trueloop intended to retry transientEAGAINerrors. When the transport'sAsyncThrowingStreamfinishes cleanly,for try await data in streamexits normally, bypasses both catch blocks, and immediately re-enters repeat with zero backoff—re-callingconnection.receive()on an already-finished stream in a tight busy-loop.How Has This Been Tested?
Breaking Changes
No
Types of changes
Checklist
Additional context