fix: await in-flight handlers before disposing to prevent ObjectDisposedException#1400
Open
jongio wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
…sedException In stateless HTTP mode, fire-and-forget message handlers could outlive the HTTP request scope. When a connection was aborted (e.g., client timeout), ASP.NET Core disposed the request's IServiceProvider while tool handlers were still executing, causing ObjectDisposedException when accessing scoped services like DbContext. Track handler tasks in ProcessMessagesCoreAsync and await them in the finally block. Since ProcessMessagesCoreAsync is awaited by the session disposal chain (which runs inside the HTTP request handler), this keeps the request scope alive until all handlers complete. Closes modelcontextprotocol#1269 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes a race condition in stateless HTTP mode where
ObjectDisposedExceptionis thrown when an HTTP connection is aborted while tool handlers are still executing.Closes #1269
Problem
In stateless mode,
McpSessionHandler.ProcessMessagesCoreAsyncfires and forgets message handler tasks (_ = ProcessMessageAsync()). When the HTTP connection is aborted (e.g., client timeout), the disposal chain runs:context.RequestAbortedfiresProcessMessagesCoreAsyncexits theforeachloopcontext.RequestServices(the HTTP request scope)But in step 3, fire-and-forget handler tasks may still be running. When they try to access scoped services (like
DbContextorILogger),ObjectDisposedExceptionis thrown because the request scope was disposed in step 5.Fix
Track handler tasks in
ProcessMessagesCoreAsyncand await them in thefinallyblock before allowing the method to return. SinceProcessMessagesCoreAsyncis awaited indirectly by the HTTP request handler (viaServerRunTask), this keeps the request scope alive until all handlers complete:List<Task>instead of being discardedfinallyblock callsTask.WhenAll(pendingHandlers)before failing pending requestsProcessMessageAsync, so they are caught and suppressed inWhenAllThis is a minimal change to the core message processing loop that fixes the issue without changing the service provider or scope configuration.
Testing
ScopedServices_AccessibleInToolHandler_AfterConnectionAbortthat:ObjectDisposedExceptionafter connection abortTaskCompletionSource) to verify handler outcome since the client call is abortedStatelessServerTestscontinue to pass