Skip to content

Commit c467732

Browse files
authored
fix(sandbox): keep UnixLocal workspace-root removal off the event loop (#4941)
1 parent 549fcee commit c467732

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎src/agents/sandbox/sandboxes/unix_local.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ async def delete(self, session: SandboxSession) -> SandboxSession:
12901290
if unmount_failed:
12911291
return session
12921292
try:
1293-
shutil.rmtree(Path(inner.state.manifest.root), ignore_errors=False)
1293+
await run_blocking_workspace_io(shutil.rmtree, Path(inner.state.manifest.root))
12941294
except FileNotFoundError:
12951295
pass
12961296
except Exception:

‎tests/sandbox/test_unix_local.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import contextlib
45
import io
6+
import shutil
57
import signal
68
import tarfile
79
import threading
@@ -610,3 +612,65 @@ def _slow_extract(tar: object, **kwargs: object) -> None:
610612
# the workspace root are only released once nothing is still writing to them.
611613
assert events == ["extract-start", "extract-end"]
612614
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

Comments
 (0)