Commit 9b21522
Fix: swallow _receiveTask exceptions in SseClientSessionTransport.CloseAsync
When SseClientSessionTransport.ConnectAsync fails (e.g. the SSE GET
returns 405), its catch block calls CloseAsync() before wrapping the
error in InvalidOperationException("Failed to connect transport", ex).
However, CloseAsync() awaits _receiveTask which is already faulted
with the same HttpRequestException. Since there is no try-catch around
that await, the exception propagates out of CloseAsync, out of the
catch block, and the InvalidOperationException wrapping is never
reached.
This means callers of ConnectAsync receive an unwrapped
HttpRequestException instead of the documented InvalidOperationException
wrapper, which breaks exception filtering in downstream code.
The fix wraps `await _receiveTask` in CloseAsync with a try-catch that
swallows both OperationCanceledException (normal shutdown) and other
exceptions (already observed and forwarded via
_connectionEstablished.TrySetException in ReceiveMessagesAsync).
Includes a regression test that verifies ConnectAsync throws
InvalidOperationException (not HttpRequestException) when both
Streamable HTTP POST and SSE GET fallback fail.
Co-authored-by: Copilot <[email protected]>1 parent aae77b1 commit 9b21522
File tree
2 files changed
+67
-1
lines changed- src/ModelContextProtocol.Core/Client
- tests/ModelContextProtocol.Tests/Transport
2 files changed
+67
-1
lines changedLines changed: 17 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
119 | 135 | | |
120 | 136 | | |
121 | 137 | | |
| |||
Lines changed: 50 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
50 | 100 | | |
51 | 101 | | |
52 | 102 | | |
| |||
0 commit comments