Skip to content

Commit 26359f1

Browse files
committed
Fix dangling requests when server unexpectedly shuts down
If the client makes a request to the server and while waiting for the response the server shuts down, the client hangs. Make sure that when the client's message loops ends due to the server's output it's reading from ends, any pending requests are notified.
1 parent 63c17f7 commit 26359f1

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/ModelContextProtocol/Shared/McpSession.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ await _transport.SendMessageAsync(new JsonRpcError
174174
// Normal shutdown
175175
_logger.EndpointMessageProcessingCancelled(EndpointName);
176176
}
177+
finally
178+
{
179+
// Fail any pending requests, as they'll never be satisfied.
180+
foreach (var entry in _pendingRequests)
181+
{
182+
entry.Value.TrySetException(new InvalidOperationException("The server shut down unexpectedly."));
183+
}
184+
}
177185
}
178186

179187
private async Task HandleMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)