Skip to content

Commit 5e124bc

Browse files
do not await a lock to put the tail back
the bytes have already left the deque when the window decodes, so the local pending is the only copy of them. awaiting output_lock to put it back is a cancellation point, and a caller cancelled while a producer holds the lock loses the character while the session carries on to read its continuation appendleft is one synchronous call and deque is documented safe for it. every holder of that lock is either a single append or a drain loop with no await inside, so there is nothing for this to interleave with. the lock is there for the multi step drain, not for one append doing it without the lock removes the cancellation point rather than recovering from it, so there is no window left to get wrong test follows the reported repro. it drains the lead byte, holds the lock across the deadline, cancels, and checks the byte is still there and the next window still gives e acute
1 parent f479af8 commit 5e124bc

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎src/agents/sandbox/session/pty_output.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ async def collect_pty_output(
127127
# decoder once the provider is done replaces a tail that no later window will finish.
128128
text, pending = decode_pty_window(output, is_final=is_done())
129129
if pending:
130-
async with output_lock:
131-
output_chunks.appendleft(pending)
130+
# Deliberately not under ``output_lock``. Those bytes have already left the deque, so
131+
# this is the only copy, and awaiting the lock is a cancellation point: a caller
132+
# cancelled here would drop the character while the session lives on to read its
133+
# continuation. ``appendleft`` is one synchronous call on a deque, so nothing can
134+
# interleave with the drain loops the lock exists to protect.
135+
output_chunks.appendleft(pending)
132136

133137
truncated, original_token_count = truncate_text_by_tokens(text, max_output_tokens)
134138
return truncated.encode("utf-8", errors="replace"), original_token_count

‎tests/sandbox/test_pty_output.py‎

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

33
import asyncio
4+
import contextlib
45
from collections import deque
56

67
import pytest
@@ -250,3 +251,44 @@ async def test_flush_pty_tail_drains_what_the_session_still_holds() -> None:
250251

251252
assert flushed.decode("utf-8") == "hi \ufffd"
252253
assert not chunks
254+
255+
256+
@pytest.mark.asyncio
257+
async def test_collect_pty_output_keeps_the_tail_when_a_window_is_cancelled() -> None:
258+
# the lead byte has already left the deque by the time the window decodes, so this is the
259+
# only copy of it. a producer holding the lock must not be able to turn a cancelled call
260+
# into a lost character for the session that carries on
261+
raw = "\u00e9".encode()
262+
chunks: deque[bytes] = deque([raw[:1]])
263+
lock = asyncio.Lock()
264+
notify = asyncio.Event()
265+
done = {"value": False}
266+
267+
async def window(yield_time_ms: int) -> bytes:
268+
notify.set()
269+
collected, _ = await collect_pty_output(
270+
output_chunks=chunks,
271+
output_lock=lock,
272+
output_notify=notify,
273+
is_done=lambda: done["value"],
274+
yield_time_ms=yield_time_ms,
275+
max_output_tokens=None,
276+
)
277+
return collected
278+
279+
task = asyncio.create_task(window(120))
280+
await asyncio.sleep(0.02)
281+
assert not chunks
282+
283+
await lock.acquire()
284+
await asyncio.sleep(0.2)
285+
task.cancel()
286+
with contextlib.suppress(asyncio.CancelledError):
287+
await task
288+
lock.release()
289+
290+
assert list(chunks) == [raw[:1]]
291+
292+
chunks.append(raw[1:])
293+
done["value"] = True
294+
assert (await window(60)).decode("utf-8") == "\u00e9"

0 commit comments

Comments
 (0)