Skip to content

Commit fa487b9

Browse files
flush the modal tail when the finalizer is the one that sees the exit
the collector polls for the exit code and the finalizer polls again after it. a process that ends between those two polls means the collector kept a partial character back for a next window, and then the finalizer drops the entry and the bytes go with it that is worse than what was there before my change. the old code replaced those bytes in the same window so you saw a question mark. this way the output just loses them quietly so the finalizer closes the tail before it removes the entry. added a test for it, which gives hi instead of hi ? on the last commit also put back the pytest.mark.asyncio i dropped off the split test last time. asyncio_mode is auto so it still ran, but every other test here carries it
1 parent 91f5fb9 commit fa487b9

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎src/agents/extensions/sandbox/modal/sandbox.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,14 @@ async def _finalize_pty_update(
11191119
exit_code = await self._peek_exit_code(entry.process)
11201120
live_process_id: int | None = process_id
11211121
if exit_code is not None:
1122+
# The collector polled before this and may have seen the process still running, so
1123+
# it kept a partial character back for a window that is never going to come. Close
1124+
# it here, otherwise those bytes go out with the entry and the output loses them.
1125+
if entry.pending_output:
1126+
tail, _ = decode_pty_window(entry.pending_output, is_final=True)
1127+
entry.pending_output = b""
1128+
output += tail.encode("utf-8", errors="replace")
1129+
11221130
async with self._pty_lock:
11231131
removed = self._pty_processes.pop(process_id, None)
11241132
self._reserved_pty_process_ids.discard(process_id)

‎tests/extensions/sandbox/test_modal.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,6 +4444,59 @@ def _exec(self, *command: object, **kwargs: object) -> object:
44444444
await session.pty_terminate_all()
44454445

44464446

4447+
@pytest.mark.asyncio
4448+
async def test_modal_pty_finalize_flushes_a_partial_character_when_the_process_has_exited(
4449+
monkeypatch: pytest.MonkeyPatch,
4450+
) -> None:
4451+
modal_module, _create_calls, _registry_tags = _load_modal_module(monkeypatch)
4452+
4453+
class _FakeProcess:
4454+
def __init__(self) -> None:
4455+
self.poll = _with_aio(lambda: 0)
4456+
self.terminate = _with_aio(lambda: None)
4457+
4458+
class _FakeSandbox:
4459+
object_id = "sb-finalize"
4460+
4461+
def __init__(self) -> None:
4462+
self.process = _FakeProcess()
4463+
self.exec = _with_aio(self._exec)
4464+
4465+
def _exec(self, *command: object, **kwargs: object) -> object:
4466+
_ = (command, kwargs)
4467+
return self.process
4468+
4469+
sandbox = _FakeSandbox()
4470+
state = modal_module.ModalSandboxSessionState(
4471+
manifest=Manifest(root="/workspace"),
4472+
snapshot=modal_module.resolve_snapshot(None, "snapshot"),
4473+
app_name="sandbox-tests",
4474+
sandbox_id=sandbox.object_id,
4475+
)
4476+
session = modal_module.ModalSandboxSession.from_state(state, sandbox=sandbox)
4477+
4478+
# the collector polled while the process was still running, so it held the first byte of a
4479+
# two byte character back. by the time the finalizer polls, the process has gone and the
4480+
# entry is about to be dropped
4481+
entry = modal_module._ModalPtyProcessEntry(process=sandbox.process, tty=True)
4482+
entry.pending_output = "\u00e9".encode()[:1]
4483+
session._pty_processes[7] = entry
4484+
4485+
update = await session._finalize_pty_update(
4486+
process_id=7,
4487+
entry=entry,
4488+
output=b"hi ",
4489+
original_token_count=None,
4490+
)
4491+
4492+
assert update.exit_code == 0
4493+
assert update.process_id is None
4494+
# replaced rather than vanishing with the entry
4495+
assert update.output.decode("utf-8") == "hi \ufffd"
4496+
assert entry.pending_output == b""
4497+
4498+
4499+
@pytest.mark.asyncio
44474500
async def test_modal_pty_output_keeps_a_character_split_across_windows(
44484501
monkeypatch: pytest.MonkeyPatch,
44494502
) -> None:

0 commit comments

Comments
 (0)