Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Log canceled requests as canceled, not as handler failures - #2049

Open
sivaraman-krishnan wants to merge 3 commits into
modelcontextprotocol:mainfrom
sivaraman-krishnan:fix/2045-cancelled-request-warning
Open

sivaraman-krishnan wants to merge 3 commits into
modelcontextprotocol:mainfrom
sivaraman-krishnan:fix/2045-cancelled-request-warning

Conversation

@sivaraman-krishnan

@sivaraman-krishnan sivaraman-krishnan commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #2045

Problem

When a request is canceled, by the peer's notifications/cancelled, an aborted HTTP request, or session shutdown, McpSessionHandler.HandleMessageCoreAsync logs the resulting OperationCanceledException at Warning as request handler failed. The exception often comes from sending the response after the handler returns.

subscriptions/listen requests 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-running tools/call.

Fix

In HandleMessageCoreAsync, an OperationCanceledException caught while the request's token is canceled is now logged with a new request handler canceled after {ElapsedMilliseconds}ms message at Information, then rethrown. All other exceptions still go through LogRequestHandlerException at Warning.

  • Logging only. The exception is still rethrown. ProcessMessageAsync still decides whether to send an error response, and activity and metric tags (including error.type) are unchanged.
  • Which cancellations count. The check uses the per-request token, which is canceled both by the peer and by session shutdown. That is the same condition McpServerImpl uses to skip ToolCallError for canceled tool calls. It's intentionally broader than ProcessMessageAsync's isUserCancellation, which also requires the session to be running. During shutdown the request also ends by cancellation, so it's logged as canceled too.
  • Why not skip sending the response? SendMessageAsync calls ThrowIfCancellationRequested before writing anything, so no response bytes are sent today. Skipping the call wouldn't change what goes over the wire, and ProcessMessageAsync already suppresses the error response for this case. HTTP aborts reach the same per-request token, so Streamable HTTP takes this path too.
  • Log level. Information matches the sibling lifecycle messages: request handler called, request handler completed, canceled request ... per client notification and message processing canceled. Warning was the bug.
  • Genuine failures are unaffected. A handler that throws OperationCanceledException while 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.CancellationToken against the request token instead would miss real cancellations surfaced through linked tokens.

Tests

Added to CancellationTests:

  • CancelingRequest_LogsHandlerCanceled_NotFailed, a theory with two cases:

    • a tool that throws when its token is canceled;
    • a tool that returns normally after cancellation, like a subscriptions/listen handler, so the response send throws.

    Both assert one request handler canceled entry at Information and no request 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 throws OperationCanceledException while 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:

  • The new tests pass on net10.0, net9.0, net8.0 and net472, and passed 15 repeated runs.
  • The full ModelContextProtocol.Tests and ModelContextProtocol.AspNetCore.Tests suites pass locally in Release on every target.
  • The Release build has no warnings, and the AOT compatibility app and dotnet pack succeed.

Note

This PR was prepared with the help of GitHub Copilot. I reviewed the change and the description.

sivaraman-krishnan and others added 3 commits November 25, 2026 21:54
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
@sivaraman-krishnan

Copy link
Copy Markdown
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ending a subscriptions/listen request always logs "request handler failed" at Warning (OperationCanceledException from sending the response)

1 participant