Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
fix(js-sdk): stop CommandHandle.disconnect() leaking the output subscription (#1474)
Mish Ushakov2a98cce8c782
diff --git a/.changeset/command-handle-disconnect-leak.md b/.changeset/command-handle-disconnect-leak.mdnew file mode 100644index 000000000..96ec83444--- /dev/null+++ b/.changeset/command-handle-disconnect-leak.md@@ -0,0 +1,5 @@+---+"e2b": patch+---++Fix `CommandHandle.disconnect()` leaking the output subscription in the JS SDK. `disconnect()` now cooperatively gates event handling with a flag that is checked before every dispatch, so `onStdout`/`onStderr`/`onPty` are guaranteed not to fire for output that arrives after the call (previously they could keep firing when the underlying HTTP/2 abort did not promptly tear down the stream). The call returns promptly and never blocks on an idle command's stream.diff --git a/.changeset/python-command-result-ordering.md b/.changeset/python-command-result-ordering.mdnew file mode 100644index 000000000..146608ec3--- /dev/null+++ b/.changeset/python-command-result-ordering.md@@ -0,0 +1,5 @@+---+"@e2b/python-sdk": patch+---++Fix `CommandHandle` (sync and async) recording the command result after yielding the flushed end-event chunks. The decoders are now flushed and the `CommandResult` is recorded before the trailing chunks are yielded, so a consumer that stops iterating on the first flushed chunk still observes the exit code.diff --git a/packages/js-sdk/src/sandbox/commands/commandHandle.ts b/packages/js-sdk/src/sandbox/commands/commandHandle.tsindex ef57b21c1..759d17fe5 100644--- a/packages/js-sdk/src/sandbox/commands/commandHandle.ts+++ b/packages/js-sdk/src/sandbox/commands/commandHandle.ts@@ -97,6 +97,8 @@ export class CommandHandleprivate result?: CommandResultprivate iterationError?: Error+ private disconnected = false+private readonly _wait: Promise<void>/**@@ -184,8 +186,14 @@ export class CommandHandle** The command is not killed, but SDK stops receiving events from the command.* You can reconnect to the command using {@link Commands.connect}.+ *+ * Once it returns, the `onStdout`/`onStderr`/`onPty` callbacks are guaranteed+ * not to fire for output produced after this call. It does not wait for the+ * event handler to drain, so it returns promptly even for an idle command+ * whose stream produces no further output.*/async disconnect() {+ this.disconnected = truethis.handleDisconnect()}@@ -294,13 +302,22 @@ export class CommandHandle}breakcase 'end': {- yield* this.flushDecoders()+ // Flush trailing decoder bytes into the accumulators and record the+ // result *before* yielding the flushed chunks. A disconnected+ // consumer breaks out of `handleEvents` on the first yielded chunk,+ // which would otherwise abort this generator before `this.result`+ // is assigned and make `wait()` fail as if the process never+ // produced a result.+ const flushed = [...this.flushDecoders()]this.result = {exitCode: e.value.exitCode,error: e.value.error,stdout: this.stdout,stderr: this.stderr,}+ for (const chunk of flushed) {+ yield chunk+ }break}}@@ -327,6 +344,17 @@ export class CommandHandleprivate async handleEvents() {try {for await (const [stdout, stderr, pty] of this.iterateEvents()) {+ // The handle was disconnected — stop dispatching to the callbacks. The+ // flag is checked before every dispatch, so no callback fires for+ // output that arrives (or was buffered) after disconnect() was called,+ // even if the underlying abort hasn't torn the stream down yet. There+ // is no synchronous suspension point between this check and the+ // dispatch below, so disconnect() cannot interleave to let a late event+ // slip through.+ if (this.disconnected) {+ break+ }+if (stdout !== null) {await this.onStdout?.(stdout)} else if (stderr !== null) {diff --git a/packages/js-sdk/tests/sandbox/commands/commandHandle.test.ts b/packages/js-sdk/tests/sandbox/commands/commandHandle.test.tsindex 65b289b60..eec91b7b5 100644--- a/packages/js-sdk/tests/sandbox/commands/commandHandle.test.ts+++ b/packages/js-sdk/tests/sandbox/commands/commandHandle.test.ts@@ -82,6 +82,54 @@ function endEvent(exitCode = 0) {}}+// An async iterable whose events are delivered on demand. Lets a test hold the+// handle's event loop blocked on `next()` (idle between bursts) and then push a+// late event to simulate stdout arriving after `disconnect()` — the transport+// condition that triggers the production leak. `return()` (called when the+// handle's `for await` breaks) unblocks any pending read, mirroring the stream+// being torn down from the client side.+function createControllableEvents() {+ const queue: any[] = []+ let pending: ((result: IteratorResult<any>) => void) | undefined+ let closed = false++ const iterator: AsyncIterator<any> = {+ next() {+ if (queue.length > 0) {+ return Promise.resolve({ value: queue.shift(), done: false })+ }+ if (closed) {+ return Promise.resolve({ value: undefined, done: true })+ }+ return new Promise((resolve) => {+ pending = resolve+ })+ },+ return() {+ closed = true+ if (pending) {+ const resolve = pending+ pending = undefined+ resolve({ value: undefined, done: true })+ }+ return Promise.resolve({ value: undefined, done: true })+ },+ }++ return {+ events: { [Symbol.asyncIterator]: () => iterator } as AsyncIterable<any>,+ push(event: any) {+ if (pending) {+ const resolve = pending+ pending = undefined+ resolve({ value: event, done: false })+ } else {+ queue.push(event)+ }+ },+ }+}+describe('CommandHandle', () => {it.each<EventKind>(['stdout', 'stderr', 'pty'])('wait awaits async %s callbacks',@@ -257,4 +305,173 @@ describe('CommandHandle', () => {expect(stdoutChunks.join('')).toBe('a�')})++ it('onStdout stops firing after await disconnect()', async () => {+ const controllable = createControllableEvents()+ const chunks: string[] = []++ const handle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ controllable.events,+ (out) => {+ chunks.push(out)+ }+ )++ // First burst is delivered to the live subscriber.+ controllable.push(dataEvent('stdout', new TextEncoder().encode('a')))+ await vi.waitFor(() => {+ expect(chunks).toEqual(['a'])+ })++ // Disconnect while the event loop is idle (blocked on the next read), then+ // push a late event — as if stdout arrived after the abort but before the+ // stream was torn down. The late event must never reach the callback.+ await handle.disconnect()+ controllable.push(dataEvent('stdout', new TextEncoder().encode('b')))+ await new Promise((r) => setTimeout(r, 0))++ expect(chunks).toEqual(['a'])++ // Any further output is ignored too.+ controllable.push(dataEvent('stdout', new TextEncoder().encode('c')))+ await new Promise((r) => setTimeout(r, 0))+ expect(chunks).toEqual(['a'])+ })++ it('disconnect() resolves promptly when a stream is idle', async () => {+ // An idle long-running command (e.g. `sleep`) produces no further output,+ // so the event handler stays blocked on the next read. disconnect() must+ // not wait for that read and must resolve right away.+ const controllable = createControllableEvents()+ const handle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ controllable.events,+ () => {}+ )++ // No event is ever pushed; this would hang if disconnect() awaited the+ // event handler.+ await handle.disconnect()+ })++ it('disconnect() does not block on an in-flight callback', async () => {+ const controllable = createControllableEvents()+ let callbackStarted = false+ let releaseCallback: (() => void) | undefined+ const callbackBlocked = new Promise<void>((resolve) => {+ releaseCallback = resolve+ })++ const handle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ controllable.events,+ async () => {+ callbackStarted = true+ await callbackBlocked+ }+ )++ controllable.push(dataEvent('stdout', new TextEncoder().encode('a')))+ await vi.waitFor(() => {+ expect(callbackStarted).toBe(true)+ })++ // The callback is still in flight; disconnect() must resolve without+ // waiting for it to settle.+ await handle.disconnect()++ // Releasing the callback afterwards is harmless.+ releaseCallback?.()+ })++ it('disconnect() resolves when called from inside a callback', async () => {+ const controllable = createControllableEvents()+ let disconnectReturned = false++ const handle: CommandHandle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ controllable.events,+ async () => {+ await handle.disconnect()+ disconnectReturned = true+ }+ )++ controllable.push(dataEvent('stdout', new TextEncoder().encode('a')))+ await vi.waitFor(() => {+ expect(disconnectReturned).toBe(true)+ })+ })++ it('records the exit code when disconnected before an end event with trailing bytes', async () => {+ const controllable = createControllableEvents()+ const emojiBytes = new TextEncoder().encode('😀')+ const chunks: string[] = []++ const handle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ controllable.events,+ (out) => {+ chunks.push(out)+ }+ )++ // First chunk leaves an incomplete multibyte sequence buffered in the+ // decoder, so the end event will flush a trailing replacement character.+ controllable.push(+ dataEvent(+ 'stdout',+ new Uint8Array([+ ...new TextEncoder().encode('a'),+ ...emojiBytes.slice(0, 2),+ ])+ )+ )+ await vi.waitFor(() => {+ expect(chunks).toEqual(['a'])+ })++ // Disconnect, then the process exits. The end event carries a flushed+ // chunk; wait() must still surface the exit code instead of failing as if+ // the process never produced a result.+ await handle.disconnect()+ controllable.push(endEvent(0))++ const result = await handle.wait()+ expect(result.exitCode).toBe(0)+ expect(result.stdout).toBe('a�')+ // The flushed chunk was produced after disconnect, so it must not reach+ // the live callback.+ expect(chunks).toEqual(['a'])+ })++ it('surfaces an async callback error through wait()', async () => {+ async function* events() {+ yield dataEvent('stdout', new TextEncoder().encode('a'))+ yield endEvent()+ }++ const handle = new CommandHandle(+ 1,+ () => {},+ async () => true,+ events(),+ async () => {+ throw new Error('callback failed')+ }+ )++ await expect(handle.wait()).rejects.toThrow('callback failed')+ })})diff --git a/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py b/packages/python-sdk/e2b/sandbox_async/commands/command_handle.pyindex faf6e7f43..a8436beb1 100644--- a/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py+++ b/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py@@ -162,14 +162,19 @@ class AsyncCommandHandle:if event.event.data.pty:yield None, None, event.event.data.ptyif event.event.HasField("end"):- for flushed in self._flush_decoders():- yield flushed+ # Flush trailing decoder bytes into the accumulators and+ # record the result before yielding the flushed chunks, so a+ # consumer that stops iterating on the first flushed chunk+ # still observes the exit code.+ flushed = list(self._flush_decoders())self._result = CommandResult(stdout="".join(self._stdout_chunks),stderr="".join(self._stderr_chunks),exit_code=event.event.end.exit_code,error=event.event.end.error,)+ for f in flushed:+ yield fexcept Exception:# The stream raised before an end event (e.g. disconnect or RPC# failure). Flush any bytes still buffered in the decoders sodiff --git a/packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py b/packages/python-sdk/e2b/sandbox_sync/commands/command_handle.pyindex b02e78581..a9cf5fc79 100644--- a/packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py+++ b/packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py@@ -112,13 +112,18 @@ class CommandHandle:if event.event.data.pty:yield None, None, event.event.data.ptyif event.event.HasField("end"):- yield from self._flush_decoders()+ # Flush trailing decoder bytes into the accumulators and+ # record the result before yielding the flushed chunks, so a+ # consumer that stops iterating on the first flushed chunk+ # still observes the exit code.+ flushed = list(self._flush_decoders())self._result = CommandResult(stdout="".join(self._stdout_chunks),stderr="".join(self._stderr_chunks),exit_code=event.event.end.exit_code,error=event.event.end.error,)+ yield from flushed# If the stream closed without an end event (e.g. disconnect or a# dropped connection), flush any bytes still buffered in thediff --git a/packages/python-sdk/tests/test_command_handle.py b/packages/python-sdk/tests/test_command_handle.pyindex 7b3237c00..c8cf957c4 100644--- a/packages/python-sdk/tests/test_command_handle.py+++ b/packages/python-sdk/tests/test_command_handle.py@@ -1,3 +1,6 @@+import asyncio+from typing import Any, cast+import pytestfrom e2b.envd.process import process_pb2@@ -38,6 +41,114 @@ async def _kill() -> bool:return True+class _AsyncControllableEvents:+ """Async event source that delivers items on demand.++ Lets a test hold the handle's event-handling task blocked waiting for the+ next event (idle between bursts), then push a late event after+ ``disconnect()`` to confirm it never reaches the callback — the transport+ condition that triggers the JS SDK leak.+ """++ def __init__(self):+ self._queue: asyncio.Queue = asyncio.Queue()+ self._closed = False++ def __aiter__(self):+ return self++ async def __anext__(self):+ if self._closed and self._queue.empty():+ raise StopAsyncIteration+ item = await self._queue.get()+ if item is None:+ raise StopAsyncIteration+ return item++ def push(self, event):+ self._queue.put_nowait(event)++ async def aclose(self):+ # Intentionally does NOT unblock a read that is already suspended on+ # the queue: this models the JS-leak transport condition where the+ # stream stays open past the close request. The only thing that stops+ # the handle's event loop here is task cancellation in disconnect().+ self._closed = True+++async def test_async_disconnect_stops_callbacks():+ events = _AsyncControllableEvents()+ chunks = []+ handle = AsyncCommandHandle(+ pid=1,+ handle_kill=_kill,+ # The handle only async-iterates and aclose()s the stream; this stand-in+ # satisfies both without being a real async generator.+ events=cast(Any, events),+ on_stdout=chunks.append,+ )++ # First burst is delivered to the live subscriber.+ events.push(_stdout_event(b"a"))+ for _ in range(200):+ if chunks == ["a"]:+ break+ await asyncio.sleep(0.005)+ assert chunks == ["a"]++ # disconnect() cancels the event-handling task and closes the stream, so+ # once it returns the callback must not fire again.+ await handle.disconnect()++ # A late event (stdout arriving after disconnect) must never reach the+ # callback.+ events.push(_stdout_event(b"b"))+ await asyncio.sleep(0.05)+ assert chunks == ["a"]+++def test_sync_has_no_background_subscription():+ # The sync handle has no detached event-handling task: events are consumed+ # only while the caller iterates (e.g. inside wait()), so there is no+ # subscription that could keep firing after disconnect().+ consumed = []++ def events():+ consumed.append("started")+ yield _stdout_event(b"a")+ yield _end_event()++ handle = CommandHandle(pid=1, handle_kill=lambda: True, events=events())++ # Nothing is consumed until the caller iterates.+ assert consumed == []++ # disconnect() just closes the (un-started) stream — still nothing consumed.+ handle.disconnect()+ assert consumed == []+++def test_sync_records_result_before_yielding_flushed_chunk():+ # A consumer that stops iterating right after the end event's flushed chunk+ # must still observe the exit code: the result is recorded before the+ # flushed chunk is yielded.+ def events():+ yield _stdout_event(b"a" + EMOJI_BYTES[:2])+ yield _end_event(0)++ handle = CommandHandle(pid=1, handle_kill=lambda: True, events=events())+ iterator = iter(handle)+ assert next(iterator) == ("a", None, None)+ # The end event flushes a trailing replacement character; pull just that+ # chunk and then stop iterating.+ next(iterator)+ iterator.close()++ assert handle._result is not None+ assert handle._result.exit_code == 0+ assert handle._result.stdout == "a�"++def test_sync_decodes_multibyte_chars_split_across_chunks():def events():yield _stdout_event(b"a" + EMOJI_BYTES[:2])