Skip to content

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

Closed

Description

@t11z

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. Its finally block writes stopped at $i of $To to a marker file using [System.IO.File]::WriteAllText, which still works when the pipeline is stopped.
  • The client calls the tool with -TimeoutSeconds 1 and expects a System.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)

  1. Update-McpHttpChannel (line ~462) switches an in-flight POST to SSE once it runs longer than KeepAliveSeconds, then writes ": keep-alive\n\n" to idle open channels.
  2. Write-McpSseChunk (line ~382) does Stream.Write plus Flush. On an exception it aborts the channel and returns $false.
  3. $false triggers Stop-McpHttpChannelRequest (line ~498), which calls Stop-McpInFlightRequest (src/Private/Dispatcher.ps1:354): Cts.Cancel() plus PowerShell.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)

  1. 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.
  2. The client does not actually tear down the TCP connection. Disposing the response might drain it or return the connection to the pool (SocketsHttpHandler response draining), so the server sees a live connection. That could be platform-dependent in timing.
  3. Cancellation happens, but the marker is not written. For example: the finally block does not run after BeginStop on Windows, the worker is stuck in Start-Sleep, or the file write races with the test's polling. Check this by logging (stderr, Debug level) at every step of the chain: switch to SSE, each keep-alive write result, Stop-McpHttpChannelRequest, Stop-McpInFlightRequest, and handler exit.
  4. 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 this Describe block N times, e.g. 30, with Invoke-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-latest cells: 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/-Tag exclusions 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

  1. self-assigned this
    on Sep 29, 2026
  2. added
    bug
    on Sep 29, 2026
  3. added 4 commits that reference this issue on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions