Wait for in-flight message handlers before ProcessMessagesCoreAsync returns#1403
Open
Wait for in-flight message handlers before ProcessMessagesCoreAsync returns#1403
Conversation
…CoreAsync Use an in-flight counter (starting at 1) and a TaskCompletionSource to ensure ProcessMessagesCoreAsync waits for all fire-and-forget ProcessMessageAsync tasks to complete before returning. This prevents ObjectDisposedException when the transport closes while tool handlers are still executing. Fixes: ObjectDisposedException when IServiceProvider is disposed while tool handler execution is in progress. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ObjectDisposedException during tool handler execution
Wait for in-flight message handlers before ProcessMessagesCoreAsync returns
Feb 28, 2026
This was referenced Feb 28, 2026
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.
ProcessMessagesCoreAsyncfire-and-forgetsProcessMessageAsynctasks but returns as soon as the transport channel completes — without waiting for in-flight handlers. The caller (RunAsync) then disposes service scopes, causingObjectDisposedExceptionin still-running tool handlers. This is easily triggered by OpenAI's stateless HTTP transport closing the connection while a long-running tool is executing.Changes
McpSessionHandler.ProcessMessagesCoreAsync: Track in-flight handlers with anInterlockedcounter (starts at 1 for the loop itself) and aTaskCompletionSource<bool>. Increment before eachProcessMessageAsyncdispatch, decrement in itsfinally. When the count hits zero, signal the TCS. Thefinallyblock ofProcessMessagesCoreAsyncdecrements its own count and awaits the TCS if handlers are still running.McpServerTests.RunAsync_WaitsForInFlightHandlersBeforeReturning: Regression test that blocks a tool handler, disposes the transport, then verifiesRunAsyncdoesn't return until the handler completes.Uses
TaskCompletionSource<bool>instead of non-genericTaskCompletionSourcefor .NET Standard 2.0 compatibility.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.