Windows: single-threaded stop() blocks on an idle keep-alive connection (own pytest suite never runs in CI)

Author: sxh313Created Sep 20, 2026Updated Sep 20, 2026

Summary

On Windows, McpServer.stop() does not return while an HTTP client holds an idle keep-alive connection: it blocks until that client hangs up or the handler's 30 s read timeout fires. tests/test_mcp_spec_http_e2e.py::Http11TransportTests:: test_single_threaded_stop_closes_idle_keepalive_connection asserts exactly this property and fails deterministically here.

The suite that contains it is never run by CI. .github/workflows/idalib-tests.yml is the only workflow, it is runs-on: ubuntu-latest, and its only test step is uv run ida-mcp-test tests/crackme03.elf -q -- that runner (src/ida_pro_mcp/test.py) discovers tests through ida_mcp.framework.run_tests/TESTS over ida_mcp/tests/, so the root tests/ pytest suite never executes anywhere automated. Platform regressions in it are therefore invisible unless someone runs it by hand.

Reproduce

Clean checkout of 00e4aa7, no edits, Windows 11, Python 3.12.13:

$ PYTHONPATH=src python -m pytest tests -q
1 failed, 243 passed, 114 subtests passed in 19.81s

FAILED tests/test_mcp_spec_http_e2e.py::Http11TransportTests::
  test_single_threaded_stop_closes_idle_keepalive_connection
>     self.assertTrue(stopped_while_connection_open)
E     AssertionError: False is not true

It fails on every run, not intermittently (2/2).

Instrumented replica of the same scenario, printing where the time goes:

[2] first request ok (200), connection left idle keep-alive
[2] registered live sockets: 1
[2] timings: {'conn_shutdown_start': 0.02, 'conn_shutdown_done': 0.02}   # "succeeded"
[2] stop() still running after 2s (test asserts NOT): True
[2] handler exit   t=2.02       # <-- only once the *client* closed at t=2.0
[2] stop() returned after 2.00s

_shutdown_http_connections() runs and reports no error, yet the handler thread is still parked. Scope check with the same script:

threaded=False: stop() returned within 2s while the keep-alive was idle: False   (blocked 2.00s)
threaded=True : stop() returned within 2s while the keep-alive was idle: True    (0.50s)

So the default serve(threaded=True) is fine and only threaded=False is affected - which today means "only that one test". Still, threaded=False is a documented option of serve(), so the hang is a real property of the API on Windows.

Root cause

_shutdown_http_connections() (src/ida_pro_mcp/ida_mcp/zeromcp/mcp.py:816) calls connection.shutdown(socket.SHUT_RDWR). On Linux that wakes the peer-read; on Windows it does not wake a thread already blocked in a read on that socket. Measured with a worker blocked reading an accepted (idle) socket and the wake-up issued from another thread, recv armed with a 3 s timeout so "never woken" resolves as a timeout:

wake-up attempted from the other thread result latency
shutdown(SHUT_RDWR) (current code) TIMEOUT, never woken 3.00 s
shutdown(SHUT_WR) only TIMEOUT, never woken 3.00 s
settimeout(0.2) TIMEOUT, never woken 3.00 s
shutdown(SHUT_RDWR) + CancelIoEx(fileno, NULL) CancelIoEx -> True, still TIMEOUT 3.00 s
plain close(), no makefile() reference alive wakes: OSError WinError 10038 0.22 s

The last row is the interesting one, and it does not transfer to the real handler: StreamRequestHandler.setup() reads through connection.makefile('rb'), which pins socket._io_refs, so a close() from the stopping thread is deferred and changes nothing:

1 shutdown + socket.close()   -> socket._closed=True io_refs=1 -> TIMEOUT, never woken (3.02s)
2 shutdown + close + _real_close()                          -> wakes in 0.22s (WinError 10038)
3 shutdown + rfile.close()                                  -> TIMEOUT, never woken (3.00s)

i.e. only an actual closesocket() interrupts the pending read on Windows, and reaching it from outside means socket._real_close() - private API, and it then surfaces as an uncaught OSError(10038) inside readline(), which McpHttpRequestHandler.handle() does not catch (it catches Connection*/BrokenPipe/ Timeout only) and would print a handle_error() traceback.

So I did not open a PR: every fix I could verify either uses private API or changes the idle-connection design, which is the maintainer's call.

Options that look clean

  • Make the idle read interruptible instead of trying to break it: wait in select(..., 0.5) slices and stop when mcp_server._running is false, keeping the existing 30 s idle policy as a cumulative budget. This mirrors what the SSE path already does at zeromcp/mcp.py:622 for the same reason ("Detect disconnect without writing").
  • Or accept the platform difference in the test: it encodes a property Windows cannot deliver this way, so a sys.platform == "win32" skip/xfail with a link to this issue would at least make pytest tests/ meaningful on Windows.
  • Or shorten McpHttpRequestHandler.timeout in the stopping path only.

Notes

  • The code is vendored: src/ida_pro_mcp/ida_mcp/zeromcp/__init__.py says "Vendored from zeromcp 1.3.0", so the real fix probably belongs upstream with a re-vendor here. I kept the diagnosis here because that is where the failing test lives.
  • Not #267: that one is background=False with _handle_sse_get spinning; this is the plain HTTP keep-alive read path and is Windows-specific.
  • Nothing in this report involves the discovery pruning fixed in #525 - different file, different mechanism (a probe that completes with an error vs. a shutdown that never lands).