Skip to content

fix(mcp): isolate orphan-reap sleep so test patches don't catch other threads - #27656

Closed
bbernstein616 wants to merge 2 commits into
NousResearch:mainfrom
bbernstein616:fix/mcp-orphan-reap-sleep-isolation
Closed

fix(mcp): isolate orphan-reap sleep so test patches don't catch other threads#27656
bbernstein616 wants to merge 2 commits into
NousResearch:mainfrom
bbernstein616:fix/mcp-orphan-reap-sleep-isolation

Conversation

@bbernstein616

Copy link
Copy Markdown

Summary

  • Patching tools.mcp_tool.time.sleep is identical to patching the global time.sleeptools.mcp_tool.time is the time module, so the previous "fix" (04b4f76) did not isolate anything.
  • On CI under pytest-xdist, worker / heartbeat threads kept calling time.sleep(1) while the mock was active, so test_kill_orphaned_uses_sigkill_when_available saw 207,243 calls and assert_called_once_with(2) failed (see fix(security): validate inference_base_url against host allowlist to prevent bearer token exfiltration #27612 CI job 76430224275).
  • Introduces a private _orphan_reap_sleep(seconds) indirection in tools.mcp_tool and routes _kill_orphaned_mcp_children's 2-second SIGTERM→SIGKILL gap through it. Tests patch the new symbol instead; it's exclusive to mcp_tool, so unrelated background sleepers can't pollute the mock.
  • Strengthens the SIGKILL-absent test from a permissive assert mock_sleep.called to mock_sleep.assert_called_once_with(2) so both branches lock the same property.

Verification

  • Stress harness with a background thread spamming time.sleep(1) while _kill_orphaned_mcp_children runs: mock now captures only call(2) (vs. thousands before).
  • tests/tools/test_mcp_stability.py — 16/16 pass locally.
  • tests/tools/ -k mcp — 399/399 pass locally.

Test plan

  • CI test job green on Linux pytest-xdist (the surface where this previously failed)
  • tests/tools/test_mcp_stability.py::TestStdioPidTracking all pass
  • No new flakes in adjacent MCP tests

memosr and others added 2 commits May 18, 2026 00:29
… threads

Patching ``tools.mcp_tool.time.sleep`` (the previous "fix") is identical to
patching the global ``time.sleep`` — ``tools.mcp_tool.time`` *is* the time
module, and ``sleep`` is a module attribute. On CI under pytest-xdist the
test_kill_orphaned_uses_sigkill_when_available mock captured 207k calls
because worker/heartbeat threads kept calling ``time.sleep(1)`` while the
mock was active, blowing up ``assert_called_once_with(2)``.

Introduce a private ``_orphan_reap_sleep`` indirection in tools.mcp_tool and
have the test patch that symbol instead. The wrapper is unique to mcp_tool
so unrelated background sleepers can't pollute the mock's call list.

Verified with a stress harness (background thread spamming time.sleep(1))
that previously caused thousands of phantom call(1)s — mock now sees only
the intended call(2).

Tests: tests/tools/test_mcp_stability.py — 16/16 pass.
       tests/tools/ -k mcp — 399/399 pass.
@alt-glitch alt-glitch added type/test Test coverage or test infrastructure P3 Low — cosmetic, nice to have tool/mcp MCP client and OAuth labels May 17, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Follow-up to merged #27589 (same MCP orphan-reap sleep mock CI flake). #27589 narrowed the mock target but didn't fully isolate — this PR adds the _orphan_reap_sleep indirection to complete the fix.

Note: This PR also bundles an unrelated security hardening change in hermes_cli/proxy/adapters/nous_portal.py (URL allowlist for Nous Portal inference endpoint). Consider splitting.

@bbernstein616

Copy link
Copy Markdown
Author

Superseded by clean replacement PR #27676. This branch includes an unrelated nous_portal security commit from the original base; because force-push is forbidden for this watchdog, I opened #27676 from a clean branch rebased onto current origin/main instead.

@bbernstein616

Copy link
Copy Markdown
Author

Closing in favor of clean replacement #27676 (same MCP fix, no unrelated nous_portal commit).

@bbernstein616
bbernstein616 deleted the fix/mcp-orphan-reap-sleep-isolation branch May 20, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low — cosmetic, nice to have tool/mcp MCP client and OAuth type/test Test coverage or test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants