Skip to content

Commit b71a7ba

Browse files
committed
fix: fix issue where it would consume 100% CPU when an MCP server is terminated
1 parent c0407a0 commit b71a7ba

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Sources/MCP/Client/Client.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public actor Client {
179179
// Start message handling loop
180180
task = Task {
181181
guard let connection = self.connection else { return }
182-
repeat {
182+
loop: while true {
183183
// Check for cancellation before starting the iteration
184184
if Task.isCancelled { break }
185185

@@ -207,6 +207,8 @@ public actor Client {
207207
)
208208
}
209209
}
210+
await logger?.debug("Client receive stream finished; exiting loop.")
211+
break loop
210212
} catch let error where MCPError.isResourceTemporarilyUnavailable(error) {
211213
try? await Task.sleep(for: .milliseconds(10))
212214
continue
@@ -215,7 +217,7 @@ public actor Client {
215217
"Error in message handling loop", metadata: ["error": "\(error)"])
216218
break
217219
}
218-
} while true
220+
}
219221
await self.logger?.debug("Client message handling loop task is terminating.")
220222
}
221223

0 commit comments

Comments
 (0)