Skip to content

Cancel background task runners on server disposal#1715

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

Cancel background task runners on server disposal#1715
jstar0 wants to merge 2 commits 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/
dotnet test tests/ModelContextProtocol.Analyzers.Tests/
dotnet build
dotnet pack
dotnet docfx docs/docfx.json --warningsAsErrors true

The lifecycle focus passed on net10.0, net9.0, and net8.0 (3 tests on each
framework), with 20 additional consecutive net10.0 repetitions. The full Core
suite passed with 2312 passed and 5 skipped; the analyzer suite passed with 57
passed. The Release solution build completed with 0 warnings and 0 errors,
package validation produced all packages, and DocFX completed with 0 warnings
and 0 errors. NativeAOT publish and execution also completed successfully.

The ASP.NET conformance harness has one unrelated local baseline failure under
Node.js v26.5.0 in elicitation-sep1330-enums; the exact current main run
reproduces the same failure, while the remaining ASP.NET suite passes with 583
passed and 30 skipped. The repository CI workflow uses Node.js 22.

Closes #1707

@jstar0
jstar0 force-pushed the fix/server-lifetime-task-runners branch from ff309e9 to 456f064 Compare July 25, 2026 16:50
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