Description
Problem
The fetch server's pytest suite fails when the machine is under heavy load, and passes when it is run alone. It cost two npm run local:gate runs while working on #5652 (a change that does not touch fetch).
Observed (macOS, load average around 11 while other builds ran):
tests/test_missing_content_type.py::test_html_without_content_type_is_still_simplified[doctype-upper]tests/test_readability_subprocess.py::test_npm_install_output_goes_to_stderr_not_stdout
with readability_js logging node did not finish within 10 s and was killed / … within 30 s …, and the suite taking about 5 minutes instead of the usual ~26 s. The same suite then passed alone (608 passed), and the next gate run passed.
Cause (likely)
src/fetch/src/mcp_server_fetch/readability_js.py runs each subprocess under a production timeout (VERSION_TIMEOUT_SECONDS = 10.0 for node -v, EXTRACT_TIMEOUT_SECONDS = 30.0 for extraction). The tests above spawn real processes under those same timeouts, so when the machine is slow enough the subprocess is killed, node_path() / extraction returns None, and the assertion fails. The behavior under test (stderr routing, simplification) is not what failed; the wall-clock budget was.
Expected
The tests pass regardless of machine load. AGENTS.md forbids retries (verify:no-test-retries), so the fix is in what makes the test time-sensitive, for example:
- tests that do not exercise the timeout path run with the timeouts raised (monkeypatched), and
- the timeout behavior itself stays covered by its dedicated tests with short, deliberate timeouts (as
test_readability_subprocess.pyalready does for npm at 0.5 s).
Reproduce
Run npm run coverage:py -- fetch (or uv run pytest in src/fetch) while the machine is saturated, for example alongside another workspace's build and tests.
Activity
A7mdMe commentedon Nov 25, 2026
This is a very common failure mode for MCP servers that delegate extraction or transformation to external subprocesses (Node, Python, Pandoc, etc.).
Root Cause Analysis: Static Wall-Clock Budgets vs. Machine Saturation
In
readability_js.py, the timeout is currently hardcoded as a static wall-clock budget:When a test suite runs under parallel CI load or machine CPU contention (e.g. load average 11), process spawning, V8 initialization in Node, and IPC pipe flushing take substantially longer. Because the timeout is static and wall-clock based,
subprocess.runkills a process that is actively making progress, returningNoneand causing assertion failures for tests that only care about content simplification and stderr routing.Architectural Solution: Two-Tier Remediation
1. Environment-Aware Timeout Scaling for Tests & CI
To fix CI/gate flakiness without breaking
verify:no-test-retries, the simplest immediate fix inreadability_js.pyis respecting an optional scaling factor:In the test gate or test harness, setting
MCP_SUBPROCESS_TIMEOUT_SCALE=3.0(or monkeypatching the constants for non-timeout tests) prevents load-induced premature kills while dedicated timeout tests (liketest_npm_install_output_goes_to_stderr_not_stdout) pass an explicit, small timeout directly.2. Activity-Aware Heartbeat Extensions (Production Pattern)
For long-running tools under high load, a static wall-clock timer is fundamentally brittle. A more resilient production pattern splits the timeout into:
3. Graceful Extractor Degradation
In production agent loops, if the heavy Node/Readability subprocess is killed under extreme CPU load, falling back to a lightweight regex or basic DOM cleaner ensures the agent's fetch tool call still succeeds with usable text rather than crashing with an empty response.
(Disclosure: As maintainers of the open-source MCP Production Kit, we have integrated load-aware heartbeat extensions and graceful extractor fallbacks into our production hardening test suite).