Commit 68c6e0b
committed
Fix flaky TestChildProcessCleanup: poll for file growth, clean up proc in finally
The three TestChildProcessCleanup tests were flaky on Windows/macOS CI
because they used fixed anyio.sleep() durations (0.5s + 0.3s = 800ms)
to wait for nested Python interpreter chains to start writing to marker
files. On loaded CI runners, two Python startups + subprocess.Popen +
open() + first write() can exceed 800ms, causing assert 0 > 0.
Secondary bug: when the assertion failed, proc was never terminated
(finally only cleaned up tempfiles). The leaked subprocess was GC'd
during a later test, triggering PytestUnraisableExceptionWarning and
causing knock-on failures in unrelated tests.
Changes:
- Added _wait_for_file_size() helper: polls until getsize(path) exceeds
a threshold, bounded by anyio.fail_after(10). Replaces all startup
sleep+assert chains. Raises TimeoutError with a clear message instead
of a confusing assert 0 > 0.
- Added proc cleanup to each finally block. Uses proc=None tracking
after successful in-test termination to avoid redundant double-calls,
and anyio.move_on_after() so cleanup timeout never masks the real
failure.
- Removed the parent_marker check from test_basic_child_process_cleanup.
NamedTemporaryFile(delete=False) already creates the file, so the
os.path.exists() assertion was a no-op that never verified anything.
Child writing proves parent started.
- Simplified shutdown verification: terminate_posix_process_tree already
polls for process-group death, so the first post-termination sleep(0.5)
was redundant. Reduced to a single 0.3s stability check (3x child
write interval).
Result: 3 tests drop from ~7s to ~1.9s locally, 30/30 pass under
4-worker parallel load (flake-finder).
Closes #1775
Github-Issue: #17751 parent 62eb08e commit 68c6e0b
1 file changed
+180
-188
lines changed
0 commit comments