|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import contextlib |
4 | 5 | import importlib |
5 | 6 | import inspect |
6 | 7 | import json |
7 | 8 | import os |
| 9 | +import signal |
8 | 10 | import sys |
9 | 11 | from dataclasses import fields |
10 | 12 | from pathlib import Path |
@@ -558,6 +560,71 @@ async def create_output_heavy_subprocess( |
558 | 560 | await process.wait() |
559 | 561 |
|
560 | 562 |
|
| 563 | +@pytest.mark.asyncio |
| 564 | +async def test_codex_exec_run_close_is_bounded_when_descendant_holds_stdout( |
| 565 | + monkeypatch: pytest.MonkeyPatch, |
| 566 | +) -> None: |
| 567 | + original_create_subprocess_exec = asyncio.create_subprocess_exec |
| 568 | + process: asyncio.subprocess.Process | None = None |
| 569 | + helper_pid: int | None = None |
| 570 | + helper_code = "import time; time.sleep(10)" |
| 571 | + child_code = ( |
| 572 | + "import os\n" |
| 573 | + "import subprocess\n" |
| 574 | + "import sys\n" |
| 575 | + f"helper = subprocess.Popen([sys.executable, '-c', {helper_code!r}], " |
| 576 | + "stdin=subprocess.DEVNULL, stdout=sys.stdout, stderr=subprocess.DEVNULL)\n" |
| 577 | + "os.write(1, f'ready {helper.pid}\\n'.encode())\n" |
| 578 | + "chunk = b'x' * 65536\n" |
| 579 | + "while True:\n" |
| 580 | + " os.write(1, chunk)\n" |
| 581 | + ) |
| 582 | + |
| 583 | + async def create_output_heavy_subprocess( |
| 584 | + *_args: Any, **kwargs: Any |
| 585 | + ) -> asyncio.subprocess.Process: |
| 586 | + nonlocal process |
| 587 | + process = await original_create_subprocess_exec(sys.executable, "-c", child_code, **kwargs) |
| 588 | + return process |
| 589 | + |
| 590 | + monkeypatch.setattr( |
| 591 | + exec_module.asyncio, "create_subprocess_exec", create_output_heavy_subprocess |
| 592 | + ) |
| 593 | + monkeypatch.setattr(exec_module, "_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS", 0.05, raising=False) |
| 594 | + |
| 595 | + exec_client = exec_module.CodexExec( |
| 596 | + executable_path="unused", |
| 597 | + subprocess_stream_limit_bytes=exec_module._MIN_SUBPROCESS_STREAM_LIMIT_BYTES, |
| 598 | + ) |
| 599 | + stream = exec_client.run(exec_module.CodexExecArgs(input="hello")) |
| 600 | + |
| 601 | + try: |
| 602 | + ready_line = await asyncio.wait_for(anext(stream), timeout=5) |
| 603 | + helper_pid = int(ready_line.removeprefix("ready ")) |
| 604 | + assert process is not None |
| 605 | + assert process.stdout is not None |
| 606 | + stdout = cast(Any, process.stdout) |
| 607 | + for _ in range(500): |
| 608 | + if stdout._paused: |
| 609 | + break |
| 610 | + await asyncio.sleep(0.01) |
| 611 | + assert stdout._paused |
| 612 | + |
| 613 | + await asyncio.wait_for(stream.aclose(), timeout=1) |
| 614 | + assert process.returncode is not None |
| 615 | + finally: |
| 616 | + if helper_pid is not None: |
| 617 | + with contextlib.suppress(ProcessLookupError, PermissionError): |
| 618 | + os.kill(helper_pid, signal.SIGTERM) |
| 619 | + if process is not None: |
| 620 | + if process.returncode is None: |
| 621 | + process.kill() |
| 622 | + if process.stdout is not None: |
| 623 | + while await process.stdout.read(exec_module._SUBPROCESS_DRAIN_CHUNK_BYTES): |
| 624 | + pass |
| 625 | + await process.wait() |
| 626 | + |
| 627 | + |
561 | 628 | @pytest.mark.asyncio |
562 | 629 | async def test_codex_exec_run_raises_without_stdin(monkeypatch: pytest.MonkeyPatch) -> None: |
563 | 630 | process = FakeProcess(stdout_lines=[], returncode=None, stdin_present=False) |
|
0 commit comments