|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import contextlib |
4 | 5 | import io |
| 6 | +import shutil |
5 | 7 | import signal |
6 | 8 | import tarfile |
7 | 9 | import threading |
@@ -610,3 +612,65 @@ def _slow_extract(tar: object, **kwargs: object) -> None: |
610 | 612 | # the workspace root are only released once nothing is still writing to them. |
611 | 613 | assert events == ["extract-start", "extract-end"] |
612 | 614 | assert not buf.closed |
| 615 | + |
| 616 | + |
| 617 | +@pytest.mark.asyncio |
| 618 | +async def test_client_delete_keeps_workspace_removal_off_the_event_loop( |
| 619 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 620 | +) -> None: |
| 621 | + """The event loop must keep running while `delete()` removes the workspace root. |
| 622 | +
|
| 623 | + The removal walks the whole workspace tree, so running it inline starves every other |
| 624 | + task on the loop for its full duration. `rm(recursive=True)`, `persist_workspace`, and |
| 625 | + `hydrate_workspace` already hand that work to `run_blocking_workspace_io`. |
| 626 | +
|
| 627 | + The handshake below measures the removal itself rather than the whole `delete()` call, |
| 628 | + so an `await` elsewhere in the method, such as the ephemeral unmount loop, cannot |
| 629 | + satisfy it. |
| 630 | + """ |
| 631 | + workspace = tmp_path / "workspace" |
| 632 | + workspace.mkdir() |
| 633 | + (workspace / "payload.txt").write_text("payload", encoding="utf-8") |
| 634 | + |
| 635 | + client = UnixLocalSandboxClient() |
| 636 | + session = await client.resume( |
| 637 | + UnixLocalSandboxSessionState( |
| 638 | + manifest=Manifest(root=str(workspace)), |
| 639 | + snapshot=NoopSnapshot(id="noop"), |
| 640 | + workspace_root_owned=True, |
| 641 | + ) |
| 642 | + ) |
| 643 | + |
| 644 | + real_rmtree = shutil.rmtree |
| 645 | + removal_started = threading.Event() |
| 646 | + loop_advanced = threading.Event() |
| 647 | + loop_advanced_during_removal: list[bool] = [] |
| 648 | + |
| 649 | + def _slow_rmtree(path: object, *args: object, **kwargs: object) -> None: |
| 650 | + removal_started.set() |
| 651 | + # The observer can only answer while the removal is in flight if the loop is |
| 652 | + # still free. An inline removal holds the loop here until this call returns. |
| 653 | + loop_advanced_during_removal.append(loop_advanced.wait(timeout=5.0)) |
| 654 | + real_rmtree(path, *args, **kwargs) |
| 655 | + |
| 656 | + monkeypatch.setattr(unix_local_module.shutil, "rmtree", _slow_rmtree) |
| 657 | + |
| 658 | + async def _observe_loop() -> None: |
| 659 | + while not removal_started.is_set(): |
| 660 | + await asyncio.sleep(0) |
| 661 | + loop_advanced.set() |
| 662 | + |
| 663 | + observer = asyncio.create_task(_observe_loop()) |
| 664 | + try: |
| 665 | + returned = await client.delete(session) |
| 666 | + finally: |
| 667 | + observer.cancel() |
| 668 | + with contextlib.suppress(asyncio.CancelledError): |
| 669 | + await observer |
| 670 | + |
| 671 | + assert removal_started.is_set() |
| 672 | + assert loop_advanced_during_removal == [True] |
| 673 | + # The removal still targets the manifest root, and `delete()` still hands the same |
| 674 | + # session back to the caller. |
| 675 | + assert not workspace.exists() |
| 676 | + assert returned is session |
0 commit comments