commands.connect to a resumed sandbox stops delivering stdout after a large response
Hi E2B team — thanks for the platform, we really enjoy building on it! We run long-lived stdio MCP servers inside sandboxes and lean heavily on on_timeout=pause / auto_resume, and we think we've hit a streaming-reconnect bug. We put together a tiny repro to make it easy to poke at. 🙏
TL;DR: after reattaching to a backgrounded process with commands.connect(pid) on a resumed sandbox, on_stdout stops firing for output that follows a large (~8KB) response — even though the process keeps writing and send_stdin keeps working. A stream that only emits small responses is totally fine; a single ~8KB response is enough to wedge it.
Environment
e2bPython SDK 2.20.2 (also present on 2.23.1)- Python 3.12, public
basetemplate, regioneu-west-1 - Sandboxes created with
lifecycle={"on_timeout": "pause", "auto_resume": True} - Background command via
commands.run(cmd, background=True, stdin=True, timeout=0), reattached withcommands.connect(pid, on_stdout=…, timeout=0)
Repro (SDK-only, public base template)
import asyncio
from e2b import AsyncSandbox
# Reads a line; "BIG" -> ~8KB response, anything else -> echo it back.
PROC = ("python3 -u -c \"import sys\n"
"for line in sys.stdin:\n"
" sys.stdout.write(('X'*8000 if line.startswith('BIG') else line.strip())+'\\n')\n"
" sys.stdout.flush()\"")
async def main():
sbx = await AsyncSandbox.create(
template="base", timeout=20,
lifecycle={"on_timeout": "pause", "auto_resume": True},
)
got: list[str] = []
proc = await sbx.commands.run(PROC, background=True, stdin=True, timeout=0,
on_stdout=lambda s: got.append(s))
pid = proc.pid
handle = proc
for c in range(1, 13):
await handle.disconnect()
await sbx.pause() # force a pause
await asyncio.sleep(1)
handle = await sbx.commands.connect(pid, timeout=0,
on_stdout=lambda s: got.append(s))
before = len(got)
await sbx.commands.send_stdin(pid, "BIG\n") # large response
for i in range(3):
await sbx.commands.send_stdin(pid, f"s{c}-{i}\n") # small responses
await asyncio.sleep(3)
delivered = len(got) - before
print(f"cycle {c}: sent 4 (1 big + 3 small), got {delivered}"
f"{' <-- STALL' if delivered < 4 else ''}")
asyncio.run(main())
Observed (SDK 2.20.2, base)
cycle 1: sent 4 (1 big + 3 small), got 4
cycle 2: sent 4 (1 big + 3 small), got 4
cycle 3: sent 4 (1 big + 3 small), got 4
cycle 4: sent 4 (1 big + 3 small), got 1 <-- STALL (only the big came back)
cycle 5: sent 4 (1 big + 3 small), got 0 <-- STALL (stream stays wedged)
...
DONE: 9/12 stalled
The all-small control (replace "BIG\n" with another small line) gives 0/12 stalled, which is what points at the large response as the trigger.
What we'd expect
After commands.connect to a running pid on a resumed sandbox, on_stdout should keep delivering all subsequent process output for the life of the connection, regardless of an earlier large response.
A couple of related things we noticed
- Sometimes
commands.connectitself hangs ~60s and then fails withTimeoutException: The sandbox is running but port is not open;request_timeoutdoesn't seem to bound it (feels related to #1128). - A fresh
commands.run(new process) on the same resumed sandbox always streams fine — so it looks specific to reconnecting to the existing pid.
Our workaround (for context)
We cap the connect establish with an external asyncio.wait_for, and when a reattached session goes quiet we drop it and commands.run a fresh process. It works, but we lose the warm process on each flaky reattach.
Possibly related: #1128, #857, e2b-dev/E2B#1031. Happy to share raw event traces or test a patch — thanks so much for taking a look!
The issue was fixed in PR #1346 in Python SDK 2.23.1.
The reproducer expects each line as a separate on_stdout=lambda s: got.append(s) call, but stdout is a stream and the data can be coalesced, which means that the reproducer can false-positively report the STALL, even if all data were received correctly, which might be the case why you reported this issue present also in 2.23.1.
Thanks for the quick reply, and for catching the counting issue — that was a fair flag, I should have counted content from the start. I reworked the repro to join all received chunks per cycle and check the expected response markers are actually present, so a coalesced chunk that contains all 4 responses is correctly counted as delivered.
Re-ran the corrected repro on both SDK versions on the public base template:
| SDK | result |
|---|---|
| 2.20.2 | 9/12 stalled |
| 2.23.1 | 9/12 stalled (same cycles, same pattern) |
Output on 2.23.1 (pip install e2b==2.23.1):
cycle 1: chunks=4 bytes=8016 big=Y smalls=3/3
cycle 2: chunks=4 bytes=8016 big=Y smalls=3/3
cycle 3: chunks=4 bytes=8016 big=Y smalls=3/3
cycle 4: chunks=1 bytes=8001 big=Y smalls=0/3 <-- STALL
cycle 5: chunks=0 bytes=0 big=N smalls=0/3 <-- STALL
cycle 6: chunks=0 bytes=0 big=N smalls=0/3 <-- STALL
... (7–12 identical to 5)
DONE: 9/12 stalled
Cycle 4 rules out coalescing as the explanation: the single on_stdout chunk is exactly 8001 bytes ("X"*8000 + "\n") — the 3 small responses weren't coalesced into it, they never arrived. Cycles 5+ are 0 chunks / 0 bytes across a 3-second wait per cycle. (2.20.2 reproduces identically.)
I read #1346 carefully — really nice fix, looks like it cleans up a real aclose() leak on disconnect() that would bite long-lived sandboxes. But the reproducer's symptom doesn't change on 2.23.1, so it looks like a separate issue here.
Corrected repro (drop-in replacement for the original — same shape, just counts content):
import asyncio
from e2b import AsyncSandbox
PROC = ("python3 -u -c \"import sys\n"
"for line in sys.stdin:\n"
" sys.stdout.write(('X'*8000 if line.startswith('BIG') else line.strip())+'\\n')\n"
" sys.stdout.flush()\"")
async def main():
sbx = await AsyncSandbox.create(
template="base", timeout=20,
lifecycle={"on_timeout": "pause", "auto_resume": True},
)
raw: list[str] = []
proc = await sbx.commands.run(PROC, background=True, stdin=True, timeout=0,
on_stdout=lambda s: raw.append(s))
pid = proc.pid
handle = proc
for c in range(1, 13):
await handle.disconnect()
await sbx.pause()
await asyncio.sleep(1)
handle = await sbx.commands.connect(pid, timeout=0,
on_stdout=lambda s: raw.append(s))
before = len(raw)
await sbx.commands.send_stdin(pid, "BIG\n")
for i in range(3):
await sbx.commands.send_stdin(pid, f"s{c}-{i}\n")
await asyncio.sleep(3)
joined = "".join(raw[before:])
big_ok = ("X" * 8000) in joined
smalls = [f"s{c}-{i}" in joined for i in range(3)]
n_chunks = len(raw) - before
print(f"cycle {c}: chunks={n_chunks} bytes={len(joined)} "
f"big={'Y' if big_ok else 'N'} smalls={sum(smalls)}/3"
f"{' <-- STALL' if not (big_ok and all(smalls)) else ''}")
asyncio.run(main())
Happy to share raw event traces or test a candidate fix — thanks again for digging in!
Triage
- type: bug
- priority: high
- complexity: medium
- affected_area: python-sdk / commands streaming (connect/reattach after auto-resume)
- actionable: yes
- needs_clarification: false
- summary: After reattaching to a backgrounded process on a resumed sandbox, on_stdout stops firing following a large (~8KB) response even though the process keeps writing; includes a minimal SDK-only repro and likely shares a root cause with #1587.