Repository navigation
Log canceled requests as canceled, not as handler failures - #2049
Open
sivaraman-krishnan wants to merge 3 commits into
Open
sivaraman-krishnan wants to merge 3 commits into
sivaraman-krishnan wants to merge 3 commits into
Conversation
When a request was canceled, by the peer or by session shutdown, the resulting OperationCanceledException was logged at Warning as "request handler failed". subscriptions/listen requests only end this way, so every listening client that disconnected produced a warning. Log an OperationCanceledException observed while the request's token is canceled as "request handler canceled" at Information instead. Handlers that throw OperationCanceledException without the request being canceled are still logged as failures. Fixes modelcontextprotocol#2045 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
…ests Replace polling with Task.Delay with an incoming message filter that signals once the server has finished handling and logging the tools/call request. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
…cancel test Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9c8906e7-0320-4f31-b724-aba346882b96
Author
|
Hi! Friendly ping on this one: the CI workflows are still waiting for maintainer approval to run, since this is my first contribution here. Could someone approve them when you have a moment? Happy to make any changes. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2045
Problem
When a request is canceled, by the peer's
notifications/cancelled, an aborted HTTP request, or session shutdown,McpSessionHandler.HandleMessageCoreAsynclogs the resultingOperationCanceledExceptionat Warning asrequest handler failed. The exception often comes from sending the response after the handler returns.subscriptions/listenrequests only end by cancellation, so every listening client that disconnects produces a warning with a stack trace, even though the handler behaved correctly. The same happens for any other canceled request, such as a long-runningtools/call.Fix
In
HandleMessageCoreAsync, anOperationCanceledExceptioncaught while the request's token is canceled is now logged with a newrequest handler canceled after {ElapsedMilliseconds}msmessage at Information, then rethrown. All other exceptions still go throughLogRequestHandlerExceptionat Warning.ProcessMessageAsyncstill decides whether to send an error response, and activity and metric tags (includingerror.type) are unchanged.McpServerImpluses to skipToolCallErrorfor canceled tool calls. It's intentionally broader thanProcessMessageAsync'sisUserCancellation, which also requires the session to be running. During shutdown the request also ends by cancellation, so it's logged as canceled too.SendMessageAsynccallsThrowIfCancellationRequestedbefore writing anything, so no response bytes are sent today. Skipping the call wouldn't change what goes over the wire, andProcessMessageAsyncalready suppresses the error response for this case. HTTP aborts reach the same per-request token, so Streamable HTTP takes this path too.request handler called,request handler completed,canceled request ... per client notificationandmessage processing canceled. Warning was the bug.OperationCanceledExceptionwhile its request is still active is still logged as a failure at Warning, for example from its own internal timeout.One trade-off: if a handler's own timeout fires at the same moment its request is canceled, it is logged as a cancellation. The request was canceled regardless. Checking
OperationCanceledException.CancellationTokenagainst the request token instead would miss real cancellations surfaced through linked tokens.Tests
Added to
CancellationTests:CancelingRequest_LogsHandlerCanceled_NotFailed, a theory with two cases:subscriptions/listenhandler, so the response send throws.Both assert one
request handler canceledentry at Information and norequest handler failed. An incoming message filter signals when the server has finished handling and logging the request, so the test doesn't poll. Both cases fail without the fix.HandlerThrowingOperationCanceled_WithoutRequestCancellation_LogsHandlerFailed: a prompt that throwsOperationCanceledExceptionwhile its request is active is still logged at Warning with the exception. It uses a prompt because tool exceptions are converted into error results.Validation:
ModelContextProtocol.TestsandModelContextProtocol.AspNetCore.Testssuites pass locally in Release on every target.dotnet packsucceed.Note
This PR was prepared with the help of GitHub Copilot. I reviewed the change and the description.