Skip to content

Dispose a response that arrives while a timed-out HTTP send is cancelled - #8

Merged
t11z merged 5 commits into
mainfrom
claude/vigilant-shannon-oubi04
Sep 30, 2026
Merged

t11z merged 5 commits into
mainfrom
claude/vigilant-shannon-oubi04

Conversation

@t11z

@t11z t11z commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #7: stops the handler when the client closes the connection on timeout failed intermittently on windows-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 a TimeoutException. If the response headers arrived between the deadline check and the cancellation, SendAsync completed anyway (RanToCompletion). Its HttpResponseMessage was never disposed, because $response stayed $null. The connection therefore stayed open, the client kernel ACKed the keep-alive chunks, and every server write succeeded, so Stop-McpHttpChannelRequest never 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 = 1 and 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)

Run Scope Fix Windows failures Finding
36621891306 only the test (-FullNameFilter), 30× per pwsh 7.4/7.5/7.6 off 0/90 Not reproducible in isolation. Timeout sweep 0/108.
36622953199 whole HttpServer.Tests.ps1 30×, plus Unit + that file 8× off 14 (7.5: 6/30 + 1/8; 7.6: 7/30; 7.4: 0/30) Every failure shows sendTask = RanToCompletion after Cancel(), both TCP ends Established for 8 s, 10/10 keep-alive writes OK. In the passing runs that were traced, the task was still pending at the cancel.
36624800646 as above on 0/114 The race hit 22 times, and each time the handler still stopped (at step 76–80 of 200).

The second failing test in run 35460089852 (streams SSE events … validates the wire shapes) was a consequence of the first. The timed-out count call 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

  • New 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 bare Cancel() at all three timeout sites: Invoke-McpHttpClientRequest, Open-McpHttpResumedStream and Send-McpHttpClientMessage.
  • The integration test is unchanged: same bound, same wait. The legacy semantics are untouched because the server was not changed.
  • docs/concepts/transports.md gains the client timeout behaviour and the http.sys note. CHANGELOG.md gains an entry under Fixed.

Checklist

  • ./build.ps1 -Task CI passes locally (Analyze, Test, Package, PublishLocal). Locally I ran Analyze and Test on Linux with pwsh 7.6.6: no analyzer findings; 560 passed, 0 failed, 5 skipped. I did not run Package or PublishLocal.
  • New behaviour is covered by tests: tests/Unit/HttpClient.Tests.ps1, deterministic (a late response is disposed, a pending or failed send is tolerated, the settle wait is bounded)
  • CHANGELOG.md has an entry under [Unreleased] for user-visible changes
  • conformance-baseline.yml is unchanged or shrank
  • Documentation updated where behaviour or public API changed

Note: 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

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
@t11z t11z self-assigned this Sep 30, 2026
@t11z t11z added the bug Type: something is not working label Sep 30, 2026
@t11z
t11z merged commit 6637fa4 into main Sep 30, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Type: something is not working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky on Windows: HttpServer.Tests "stops the handler when the client closes the connection on timeout"

2 participants