Skip to content

mcp pins multiple CPU cores in MCP/Client.swift - #415

Open
dmaclach wants to merge 1 commit into
modelcontextprotocol:mainfrom
dmaclach:1035893415
Open

dmaclach wants to merge 1 commit into
modelcontextprotocol:mainfrom
dmaclach:1035893415

Conversation

@dmaclach

@dmaclach dmaclach commented Dec 4, 2026

Copy link
Copy Markdown

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.

How Has This Been Tested?

  • Automated unit / integration tests
  • Tested manually
  • Tested in a real application

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

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.
@dmaclach
dmaclach requested a review from a team as a code owner December 4, 2026 19:29
)
}
}
// The receive stream ended cleanly (e.g., EOF or transport disconnect).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jeanmatthieu

Copy link
Copy Markdown
Contributor

Thanks for the report. This part of the client is going away in 1.0. so that PR wont be applicable soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants