Repository navigation
Dispose a response that arrives while a timed-out HTTP send is cancelled - #8
Merged
Merged
Conversation
Trace points on the client timeout path and the server's keep-alive and cancellation chain, a workflow that runs the flaky test 30 times per Windows cell plus a timeout sweep. Removed again before the fix PR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
When the response headers arrived between the client's deadline check and the cancellation, SendAsync completed anyway and its HttpResponseMessage was never disposed. The connection stayed open, the server's keep-alive writes kept succeeding, and the server never learned that the client had given up, so the handler ran to completion (issue #7). Stop-McpHttpSendTask cancels, lets the send settle and disposes a late response; it replaces the bare Cancel() at the three timeout sites. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
Runs the whole HttpServer test file and Unit plus that file repeatedly, traces slow dispatcher ticks, and can switch the client fix off (MCP_REPRO_NOFIX) to measure the baseline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
Counts late responses (send completed although cancelled) and runs the suite-context repro with the client fix enabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
Drops the temporary trace points, the repro workflow and scripts, and the test-side TCP snapshots. The transport docs now state that a late response is disposed on timeout and that http.sys accepts one write after the client closed the connection; the changelog records the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
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 #7:
stops the handler when the client closes the connection on timeoutfailed intermittently onwindows-latest.Root cause
The bug is a race in the client (
src/Private/HttpClient.ps1,Invoke-McpHttpClientRequest). When a send missed its deadline, the client called a bare$cts.Cancel()and threw aTimeoutException. If the response headers arrived between the deadline check and the cancellation,SendAsynccompleted anyway (RanToCompletion). ItsHttpResponseMessagewas never disposed, because$responsestayed$null. The connection therefore stayed open, the client kernel ACKed the keep-alive chunks, and every server write succeeded, soStop-McpHttpChannelRequestnever ran and the handler ran to completion.The test fixture puts both events at the same moment. The client deadline is 1 s, and the server switches to SSE after
KeepAliveSeconds = 1and only then sends its headers. On Windows the headers land in the race window far more often.CI evidence (temporary branch-only workflow, since removed)
-FullNameFilter), 30× per pwsh 7.4/7.5/7.6HttpServer.Tests.ps130×, plus Unit + that file 8×sendTask=RanToCompletionafterCancel(), both TCP endsEstablishedfor 8 s, 10/10 keep-alive writes OK. In the passing runs that were traced, the task was still pending at the cancel.The second failing test in run 35460089852 (
streams SSE events … validates the wire shapes) was a consequence of the first. The timed-outcountcall has client id 13, and the raw SSE test sends a fixed"id":13. Because the handler was never cancelled, its id was still in flight and the server rejected the second request with "already in flight". This also shows that the handler really was never stopped. The failure was not a missing marker file.A side finding, documented but not the cause: after the client closes the connection, http.sys on Windows still accepts one write and fails the next. The managed listener on Linux fails the first write immediately. Detection therefore takes one keep-alive interval longer on Windows. In the SSE path, the client's response draining (
SocketsHttpHandler, about 2 s) adds to that, so the handler stops about 4 s after the timeout on Windows and about 3 s on Linux. Both are well inside the test window.Change
Stop-McpHttpSendTask: cancels the send, lets it settle for at most 1 s (in practice it settles at once), and disposes a response that arrived late. It replaces the bareCancel()at all three timeout sites:Invoke-McpHttpClientRequest,Open-McpHttpResumedStreamandSend-McpHttpClientMessage.docs/concepts/transports.mdgains the client timeout behaviour and the http.sys note.CHANGELOG.mdgains an entry under Fixed.Checklist
./build.ps1 -Task CIpasses locally (Analyze, Test, Package, PublishLocal). Locally I ranAnalyzeandTeston Linux with pwsh 7.6.6: no analyzer findings; 560 passed, 0 failed, 5 skipped. I did not runPackageorPublishLocal.tests/Unit/HttpClient.Tests.ps1, deterministic (a late response is disposed, a pending or failed send is tolerated, the settle wait is bounded)CHANGELOG.mdhas an entry under[Unreleased]for user-visible changesconformance-baseline.ymlis unchanged or shrankNote: the branch history contains the diagnostic commits, and the last commit removes them again. Squash on merge is recommended.
🤖 Generated with Claude Code
https://claude.ai/code/session_012kC9BM7EcqLbLxn8DocWHe
Generated by Claude Code