Skip to content

Commit 2a5e1d7

Browse files
Copilothalter73
andcommitted
Propagate caller-triggered cancellation in transport exception handling
Cherry-pick from halter73/transport-exceptions (fd2d71e, 19a1f2c): - StreamClientSessionTransport.SendMessageAsync: re-throw OperationCanceledException when the caller's token triggers it, instead of wrapping in IOException. - StreamServerTransport.SendMessageAsync: same pattern. - SseClientSessionTransport.ConnectAsync: add OperationCanceledException guard and change InvalidOperationException to IOException for consistency (connection failure is I/O, not invalid state). Co-authored-by: Stephen Halter <halter73@gmail.com> Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/f1ba1333-443c-4239-8205-c32db49e9f22 Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
1 parent 4983cfb commit 2a5e1d7

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/ModelContextProtocol.Core/Client/SseClientSessionTransport.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
5858

5959
await _connectionEstablished.Task.WaitAsync(_options.ConnectionTimeout, cancellationToken).ConfigureAwait(false);
6060
}
61+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
62+
{
63+
throw;
64+
}
6165
catch (Exception ex)
6266
{
6367
LogTransportConnectFailed(Name, ex);
6468
await CloseAsync().ConfigureAwait(false);
65-
throw new InvalidOperationException("Failed to connect transport", ex);
69+
throw new IOException("Failed to connect transport.", ex);
6670
}
6771
}
6872

src/ModelContextProtocol.Core/Client/StreamClientSessionTransport.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public override async Task SendMessageAsync(JsonRpcMessage message, Cancellation
8585
await _serverInputStream.WriteAsync(s_newlineBytes, cancellationToken).ConfigureAwait(false);
8686
await _serverInputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
8787
}
88+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
89+
{
90+
throw;
91+
}
8892
catch (Exception ex)
8993
{
9094
LogTransportSendFailed(Name, id, ex);

src/ModelContextProtocol.Core/Server/StreamServerTransport.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public override async Task SendMessageAsync(JsonRpcMessage message, Cancellation
8080
await _outputStream.WriteAsync(s_newlineBytes, cancellationToken).ConfigureAwait(false);
8181
await _outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
8282
}
83+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
84+
{
85+
throw;
86+
}
8387
catch (Exception ex)
8488
{
8589
LogTransportSendFailed(Name, id, ex);

0 commit comments

Comments
 (0)