Skip to content

Commit 9fc79d7

Browse files
committed
addressed comments
1 parent dfc32d2 commit 9fc79d7

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

.changeset/clever-bears-accept.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@e2b/code-interpreter-template': patch
33
---
44

5-
release lock before stream completion
5+
interrupt kernel execution on client disconnect

js/tests/interrupt.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sandboxTest(
99
// This simulates a client disconnect: the SDK aborts the connection,
1010
// which should trigger the server to interrupt the kernel (#213).
1111
await expect(
12-
sandbox.runCode('import time; time.sleep(30)', { timeoutMs: 3_000 })
12+
sandbox.runCode('import time; time.sleep(300)', { timeoutMs: 3_000 })
1313
).rejects.toThrow()
1414

1515
// Wait for the server to detect the disconnect (via keepalive write

python/tests/async/test_async_interrupt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def test_subsequent_execution_works_after_client_timeout(
1313
# This simulates a client disconnect: the SDK closes the connection,
1414
# which should trigger the server to interrupt the kernel (#213).
1515
with pytest.raises(TimeoutException):
16-
await async_sandbox.run_code("import time; time.sleep(30)", timeout=3)
16+
await async_sandbox.run_code("import time; time.sleep(300)", timeout=3)
1717

1818
# Wait for the server to detect the disconnect (via keepalive write
1919
# failure) and interrupt the kernel.

python/tests/sync/test_interrupt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_subsequent_execution_works_after_client_timeout(sandbox: Sandbox):
1111
# This simulates a client disconnect: the SDK closes the connection,
1212
# which should trigger the server to interrupt the kernel (#213).
1313
with pytest.raises(TimeoutException):
14-
sandbox.run_code("import time; time.sleep(30)", timeout=3)
14+
sandbox.run_code("import time; time.sleep(300)", timeout=3)
1515

1616
# Wait for the server to detect the disconnect (via keepalive write
1717
# failure) and interrupt the kernel.

template/server/messaging.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ async def _cleanup_env_vars(self, env_vars: Dict[StrictStr, str]):
258258
async def _wait_for_result(self, message_id: str):
259259
queue = self._executions[message_id].queue
260260

261+
# Use a timeout on queue.get() to periodically send keepalives.
262+
# Without keepalives, the generator blocks indefinitely waiting for
263+
# kernel output. If the client silently disappears (e.g. network
264+
# failure), uvicorn can only detect the broken connection when it
265+
# tries to write — so we force a write every KEEPALIVE_INTERVAL
266+
# seconds. This ensures timely disconnect detection and kernel
267+
# interrupt for abandoned executions (see #213).
261268
while True:
262269
try:
263270
output = await asyncio.wait_for(queue.get(), timeout=KEEPALIVE_INTERVAL)

0 commit comments

Comments
 (0)