Skip to content

Cancel background task runners on server disposal#1715

Open
jstar0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jstar0:fix/server-lifetime-task-runners
Open

Cancel background task runners on server disposal#1715
jstar0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
jstar0:fix/server-lifetime-task-runners

Conversation

@jstar0

@jstar0 jstar0 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Background Tasks runners used cancellation sources independent of the owning
server and discarded their Task handles. As a result, server disposal could
finish before a runner was cancelled, including when the runner registered
during disposal.

Changes

  • Add a server-lifetime cancellation and background-task registration seam for
    extension infrastructure.
  • Cancel the lifetime at the start of stateful server disposal, close
    registration after request handlers drain, and await the remaining runners.
  • Link Tasks and long-lived MRTR handlers to the server lifetime while removing
    completed registrations.
  • Keep stateless HTTP request servers non-owning so Tasks can continue across
    independent requests.
  • Add deterministic coverage for cancellation, awaiting, late registration,
    and the stateless ownership boundary.

The change does not alter protocol messages, task-store contracts, polling, or
explicit tasks/cancel behavior.

Verification

dotnet test tests/ModelContextProtocol.Tests/ModelContextProtocol.Tests.csproj --framework net10.0
dotnet test tests/ModelContextProtocol.AspNetCore.Tests/ModelContextProtocol.AspNetCore.Tests.csproj --framework net10.0
dotnet test tests/ModelContextProtocol.Analyzers.Tests/ModelContextProtocol.Analyzers.Tests.csproj --framework net9.0
dotnet build ModelContextProtocol.slnx --configuration Release
dotnet pack ModelContextProtocol.slnx --configuration Release --no-build
dotnet docfx docs/docfx.json --warningsAsErrors true

Results: Core 2290 passed; ASP.NET Core 507 passed; analyzers 57 passed;
package validation and DocFX succeeded.

Closes #1707

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.

Deterministically cancel background task-store runners on server disposal via a server-lifetime CTS

1 participant