Skip to content

Commit 97d4dc5

Browse files
committed
Still dispose McpSession when MessageProcessingTask throws non-OCE exceptions
1 parent 065e3c6 commit 97d4dc5

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/ModelContextProtocol/Shared/McpJsonRpcEndpoint.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,30 @@ public virtual async ValueTask DisposeUnsynchronizedAsync()
9494
{
9595
_logger.CleaningUpEndpoint(EndpointName);
9696

97-
if (_sessionCts is not null)
97+
try
9898
{
99-
await _sessionCts.CancelAsync().ConfigureAwait(false);
100-
}
101-
102-
if (MessageProcessingTask is not null)
103-
{
104-
try
99+
if (_sessionCts is not null)
105100
{
106-
await MessageProcessingTask.ConfigureAwait(false);
101+
await _sessionCts.CancelAsync().ConfigureAwait(false);
107102
}
108-
catch (OperationCanceledException)
103+
104+
if (MessageProcessingTask is not null)
109105
{
110-
// Ignore cancellation
106+
try
107+
{
108+
await MessageProcessingTask.ConfigureAwait(false);
109+
}
110+
catch (OperationCanceledException)
111+
{
112+
// Ignore cancellation
113+
}
111114
}
112115
}
113-
114-
_session?.Dispose();
115-
_sessionCts?.Dispose();
116+
finally
117+
{
118+
_session?.Dispose();
119+
_sessionCts?.Dispose();
120+
}
116121

117122
_logger.EndpointCleanedUp(EndpointName);
118123
}

0 commit comments

Comments
 (0)