Skip to content

fix: replace fixed sleeps with polling in flaky TestChildProcessCleanup tests#2071

Open
BabyChrist666 wants to merge 2 commits intomodelcontextprotocol:mainfrom
BabyChrist666:fix/flaky-child-process-cleanup-tests-1775
Open

fix: replace fixed sleeps with polling in flaky TestChildProcessCleanup tests#2071
BabyChrist666 wants to merge 2 commits intomodelcontextprotocol:mainfrom
BabyChrist666:fix/flaky-child-process-cleanup-tests-1775

Conversation

@BabyChrist666
Copy link
Contributor

Summary

Closes #1775

The three tests in TestChildProcessCleanup (test_basic_child_process_cleanup, test_nested_process_tree, test_early_parent_exit) fail intermittently on macOS CI because fixed-duration anyio.sleep() calls (0.5s–1.0s) aren't always enough for child processes to start writing to their marker files on slow CI machines. The assertions then check assert 0 > 0 because the file is still empty.

Root cause

# Before: fixed sleep that may not be enough on slow CI
await anyio.sleep(0.5)
initial_size = os.path.getsize(marker_file)
await anyio.sleep(0.3)
size_after_wait = os.path.getsize(marker_file)
assert size_after_wait > initial_size  # fails with 0 > 0

Fix

Added a _wait_for_file_growth() helper that polls the marker file until it exists, has content, and is actively growing — with a generous 10-second timeout. This eliminates the race condition entirely:

# After: poll until file is growing, up to 10s
await _wait_for_file_growth(marker_file, "child process")

What changed

  • Added _wait_for_file_growth() async helper (polls file size at 0.2s intervals, 10s timeout)
  • Replaced all fixed sleep + assertion blocks in the 3 affected tests with the polling helper
  • Net result: 35 insertions, 32 deletions — simpler and more reliable

Test plan

  • ruff lint + format clean
  • pyright clean (0 errors)
  • Tests verified to work on CI (the original tests pass on CI already, this fix prevents the intermittent failures on macOS)

🤖 Generated with Claude Code

BabyChrist666 and others added 2 commits February 16, 2026 20:06
…delcontextprotocol#1775)

The three flaky tests in TestChildProcessCleanup fail intermittently on
macOS CI because fixed-duration sleeps (0.5s-1.0s) aren't enough for
child processes to start writing on slow CI machines.

Replace the fixed `anyio.sleep()` + size assertion pattern with a
`_wait_for_file_growth()` helper that polls until the file exists, has
content, and is actively growing, with a 10s timeout. This eliminates
the race condition where assertions fire before processes have started.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The timeout raise and file-not-yet-exists branch are safety nets that
don't execute in normal test runs. Mark them with coverage pragmas to
satisfy the 100% coverage requirement.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Fix flaky tests in TestChildProcessCleanup (test_stdio.py)

1 participant