Repository navigation
Description
Summary
The integration test stops the handler when the client closes the connection on timeout (tests/Integration/HttpServer.Tests.ps1:100) fails intermittently, only on windows-latest. The failing assertion is Test-Path -Path $marker | Should -BeTrue: the handler never records that it was stopped within the 8 s wait. On a rerun, and on every Linux and macOS cell, the test passes. The failure predates M5 (it already occurred on main), so it is not caused by the dual-era change.
This issue is written as a hand-off for a follow-up Claude Code session: root-cause the failure, fix it in product code (or, if the product is correct, make the test's synchronisation robust) and get the test reliably green on Windows.
Evidence
| Run | Job | Cell | Commit | Notes |
|---|---|---|---|---|
| 36184693326 | 108235116682 | windows-latest, pwsh 7.6.6 | 243fd4f (PR #6 head) |
Test took 9.14 s; Expected $true, but got $false at the marker assertion. Green on rerun. |
| 35460089852 | 105942270198 | windows-latest, pwsh 7.5.11 | merge of PR #3 on main |
Same test failed, together with streams SSE events for a request with a progress token and validates the wire shapes (same file). |
A duration of 9.14 s equals the 1 s client timeout plus the full 8 s polling window. So the handler was either never stopped, or it was stopped but never wrote the marker.
What the test does
- The fixture server starts with
KeepAliveSeconds = 1(HttpServer.Tests.ps1:38). - The tool
count(HttpServer.Tests.ps1:11) loops 200 × 50 ms. Each step it checks$Context.CancellationToken, emits progress and a log, and sleeps. Itsfinallyblock writesstopped at $i of $Toto a marker file using[System.IO.File]::WriteAllText, which still works when the pipeline is stopped. - The client calls the tool with
-TimeoutSeconds 1and expects aSystem.TimeoutException. It then checks that the server is still alive (echo). - Finally it polls for the marker for up to 8 s and asserts that the handler stopped before step 150.
Mechanism under test (server side, src/Private/HttpServer.ps1)
Update-McpHttpChannel(line ~462) switches an in-flight POST to SSE once it runs longer thanKeepAliveSeconds, then writes": keep-alive\n\n"to idle open channels.Write-McpSseChunk(line ~382) doesStream.WriteplusFlush. On an exception it aborts the channel and returns$false.$falsetriggersStop-McpHttpChannelRequest(line ~498), which callsStop-McpInFlightRequest(src/Private/Dispatcher.ps1:354):Cts.Cancel()plusPowerShell.BeginStop.
Client side: Receive-McpSseResponse in src/Private/HttpClient.ps1 cancels its CTS and disposes the response when it times out.
The design assumption is that the client going away makes a subsequent write fail promptly. On Windows, System.Net.HttpListener is backed by http.sys and not by the managed implementation used on Linux and macOS, so this assumption is the prime suspect.
Hypotheses to check (unverified)
- http.sys buffers the writes. Writes to a disconnected client succeed into the kernel buffer, and the failure surfaces late or not at all within 8 s. The small keep-alive chunks plus the per-step progress and log notifications may never fill the buffer.
- The client does not actually tear down the TCP connection. Disposing the response might drain it or return the connection to the pool (
SocketsHttpHandlerresponse draining), so the server sees a live connection. That could be platform-dependent in timing. - Cancellation happens, but the marker is not written. For example: the
finallyblock does not run afterBeginStopon Windows, the worker is stuck inStart-Sleep, or the file write races with the test's polling. Check this by logging (stderr,Debuglevel) at every step of the chain: switch to SSE, each keep-alive write result,Stop-McpHttpChannelRequest,Stop-McpInFlightRequest, and handler exit. - The second failing test in run 35460089852 (
streams SSE events … validates the wire shapes) hints at a shared cause in the SSE write path on Windows. Check whether it has the same root cause.
How to reproduce
- The dev container is Linux, so there is no local Windows. Reproduce on CI.
- Suggested approach: a temporary branch-only workflow (
workflow_dispatch,windows-latest, pwsh 7.4/7.5/7.6) that runs only thisDescribeblock N times, e.g. 30, withInvoke-Pester -FullNameFilter, plus server debug logging to stderr. - Remove the temporary workflow before opening the fix PR.
- Establish a baseline failure rate first, then show that the fix brings it to 0 over the same N.
Acceptance criteria
- Root cause identified and stated in the PR description, with the CI evidence behind it.
- The test passes reliably on all
windows-latestcells: 0 failures over ≥30 repeated runs, plus the regular matrix green. - No regression on Linux or macOS. Conformance (both requirement sets) stays green.
- If the product behaviour was wrong, meaning a disconnected client is not detected on Windows, the fix lives in product code. For example: detect disconnects by another means than a failing write, such as checking the connection or request state when http.sys provides a signal, or a bounded write with timeout. Document the platform behaviour in
docs/concepts/transports.md.
Constraints
- Do not skip, quarantine, add
-Skip/-Tagexclusions to, or weaken this test. Do not raise the 150-of-200 bound and do not extend the wait just to mask the problem. Changing the synchronisation is fine only if the new version asserts the same behaviour: the handler is stopped well before completion after the client gives up. - Pure PowerShell script module, in-box .NET assemblies only (no compiled helpers, no extra dependencies). Windows PowerShell 5.1 guard and PSScriptAnalyzer rules apply (
./build.ps1 -Task Analyze). - Keep the M5 legacy semantics intact: a closed legacy POST stream still cancels the handler, and a closed legacy GET stream only detaches from the session (
Stop-McpHttpChannelRequest).
Activity
Dispose a response that arrives while a timed-out send is cancelled
TEMPORARY: issue #7 diagnostics in suite context
TEMPORARY: issue #7 diagnostics with the fix on
Remove the issue #7 diagnostics; document the timeout behaviour