Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/MCP/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public actor Client {
)
}
}
// 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.

// 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.

} catch let error where MCPError.isResourceTemporarilyUnavailable(error) {
try? await Task.sleep(for: .milliseconds(10))
continue
Expand Down
Loading