Skip to content

Commit 68be276

Browse files
Theodor N. EngøyTheodor N. Engøy
authored andcommitted
test: reduce Windows flake in child process cleanup
1 parent 27ad99d commit 68be276

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/client/test_stdio.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ class TestChildProcessCleanup:
245245
This is a fundamental difference between Windows and Unix process termination.
246246
"""
247247

248+
async def _wait_for_file_growth(self, file_path: str, initial_size: int, timeout_seconds: float = 2.0) -> int:
249+
"""Wait until a file grows beyond initial_size.
250+
251+
These tests can be a bit timing-sensitive on Windows runners under load,
252+
so prefer polling with a short timeout over a single fixed sleep.
253+
"""
254+
deadline = time.monotonic() + timeout_seconds
255+
size = initial_size
256+
while time.monotonic() < deadline:
257+
await anyio.sleep(0.1)
258+
size = os.path.getsize(file_path)
259+
if size > initial_size:
260+
break
261+
return size
262+
248263
@pytest.mark.anyio
249264
@pytest.mark.filterwarnings("ignore::ResourceWarning" if sys.platform == "win32" else "default")
250265
async def test_basic_child_process_cleanup(self):
@@ -305,8 +320,7 @@ async def test_basic_child_process_cleanup(self):
305320
# Verify child is writing
306321
if os.path.exists(marker_file): # pragma: no branch
307322
initial_size = os.path.getsize(marker_file)
308-
await anyio.sleep(0.3)
309-
size_after_wait = os.path.getsize(marker_file)
323+
size_after_wait = await self._wait_for_file_growth(marker_file, initial_size)
310324
assert size_after_wait > initial_size, "Child process should be writing"
311325
print(f"Child is writing (file grew from {initial_size} to {size_after_wait} bytes)")
312326

@@ -405,8 +419,7 @@ async def test_nested_process_tree(self):
405419
for file_path, name in [(parent_file, "parent"), (child_file, "child"), (grandchild_file, "grandchild")]:
406420
if os.path.exists(file_path): # pragma: no branch
407421
initial_size = os.path.getsize(file_path)
408-
await anyio.sleep(0.3)
409-
new_size = os.path.getsize(file_path)
422+
new_size = await self._wait_for_file_growth(file_path, initial_size)
410423
assert new_size > initial_size, f"{name} process should be writing"
411424

412425
# Terminate the whole tree
@@ -483,8 +496,7 @@ def handle_term(sig, frame):
483496
# Verify child is writing
484497
if os.path.exists(marker_file): # pragma: no branch
485498
size1 = os.path.getsize(marker_file)
486-
await anyio.sleep(0.3)
487-
size2 = os.path.getsize(marker_file)
499+
size2 = await self._wait_for_file_growth(marker_file, size1)
488500
assert size2 > size1, "Child should be writing"
489501

490502
# Terminate - this will kill the process group even if parent exits first

0 commit comments

Comments
 (0)