Skip to content

fix(mcp): deterministic request timeout test; keep handshake timeout as the failure cause - #74

Merged
huangjeff5 merged 1 commit into
mainfrom
fix/mcp-timeout-test-flake
Nov 10, 2026
Merged

huangjeff5 merged 1 commit into
mainfrom
fix/mcp-timeout-test-flake

Conversation

@huangjeff5

Copy link
Copy Markdown
Collaborator

test_a_request_timeout_fails_that_call_alone failed whenever the fake server took longer than 8s to start, because the request timeout also bounds initialize. Under 10 parallel runs on main it failed 20/20. It now passes 20/20 under the same load. Two other things fall out of the same investigation: a handshake timeout was sometimes reported with the wrong cause, and the fake server's marker files could be read while still empty (the '' == 'wait' CI flake in test_close_interrupts_an_in_flight_call).

# --- BEFORE ---
# Server answers initialize just after the timeout fires.
await connection.list_tools()
# => _ConnectionFailedError: UNAVAILABLE ... failed
#    __cause__ = BrokenResourceError()     # the 408 is gone

# --- AFTER ---
await connection.list_tools()
# => _ConnectionFailedError: UNAVAILABLE ... failed
#    __cause__ = McpError(408, 'Timed out while waiting for response to ClientRequest...')

Main Changes

  • Handshake timeout stays the cause (product fix): Previously, if the server's initialize reply arrived after the timeout, the SDK's stdout reader tried to hand it to the read stream ClientSession had already closed. That raised BrokenResourceError, which cancelled the stdio shutdown that was still carrying the 408. The connection failed with the right status but the wrong cause, both in the log and in __cause__. _run now records the McpError from initialize and reports it ahead of whatever the SDK's task groups surface. late_handshake_server.py reproduces this every time: it holds the reply until stdin closes. The new test fails on main.
  • The stalled call carries its own timeout: Previously the test set request_timeout_millis=8000, which bounded the stall and also the handshake. The handshake's length depends on machine load, so the test was a race. The stall now goes through connection._request with the SDK's per-call read_timeout_seconds=100ms. The SDK enforces that with the same fail_after and raises the same 408, so _execute and _answered_error take the same path as before. test_a_handshake_which_never_answers_gives_up still covers wiring the session-wide timeout into ClientSession. The follow-up call is now await_stall, which only answers once the timed-out stall is actually in flight on the server.
  • Atomic marker files: Previously the fake server wrote markers with write_text, which creates the file, truncates it, then writes. A poller could see the file exist and read ''. Markers are now written to a .partial file and then os.replaced into place.
  • Fake server skips rfc3987_syntax: mcp.server imports jsonschema, which loads rfc3987_syntax when it's installed (dotpromptz pulls it in via jsonschema[format-nongpl]). That module compiles a Lark grammar at import time: 4.5s of self time in -X importtime. At load average ~70, importing mcp.server took 11-21s with it and 4-5s without. Every MCP test spawns this server, so this shortens every handshake in the suite. The server never runs IRI format checks.

Verification

  • 10 parallel x 2 rounds of -k test_a_request_timeout_fails_that_call_alone on main: 0/20.
  • Same harness on this branch, also running test_close_interrupts_an_in_flight_call and the new handshake test: 20/20 (load average 51 → 139 during the run).
  • Full packages/genkit-mcp/tests: 247 passed.
  • ruff format/check, pyright, and ty are clean. pyrefly shows the same 4 pre-existing errors as main.

This also applies on top of #72. The only conflict is in the stall test, where #72 adds the name argument to McpConnection('fake', ...).

…dshake timeout as the failure cause

- The stall test's request timeout also bounded initialize, which races fake
  server startup. The stalled call now carries its own SDK timeout.
- A late initialize reply broke the SDK's closed read stream and replaced the
  408 with BrokenResourceError as the connection's cause. Record the handshake
  McpError and report it.
- Fake server marker files are written atomically, so a poller never reads an
  empty file.
- Fake server skips importing rfc3987_syntax, a Lark grammar compiled at import
  that dominated its startup.
@huangjeff5
huangjeff5 force-pushed the fix/mcp-timeout-test-flake branch from b892605 to d7ef376 Compare November 10, 2026 20:37
@huangjeff5
huangjeff5 enabled auto-merge (squash) November 10, 2026 20:38
@huangjeff5
huangjeff5 merged commit 4b93780 into main Nov 10, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/mcp-timeout-test-flake branch November 10, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants