Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(python-sdk): map httpcore timeouts to TimeoutException to fix flaky test (#1444)

Mish Ushakov5de9bc2354c1
diff --git a/.changeset/sharp-rules-shine.md b/.changeset/sharp-rules-shine.md
new file mode 100644
index 000000000..b7b9a3132
--- /dev/null
+++ b/.changeset/sharp-rules-shine.md
@@ -0,0 +1,5 @@
+---
+"@e2b/python-sdk": patch
+---
+
+Map transport-level timeouts from `httpcore` (e.g. a stream read timeout) to `TimeoutException`. When iterating over a long-running command's output, the underlying HTTP read timeout (set to the command `timeout`) races with the server's own `deadline_exceeded` response; whichever fires first won, so the client could leak a raw `httpcore.ReadTimeout` instead of a `TimeoutException`. Both cases now surface a consistent, actionable `TimeoutException`.
diff --git a/packages/js-sdk/tests/sandbox/commands/run.test.ts b/packages/js-sdk/tests/sandbox/commands/run.test.ts
index e532d88bb..9f2e10ccd 100644
--- a/packages/js-sdk/tests/sandbox/commands/run.test.ts
+++ b/packages/js-sdk/tests/sandbox/commands/run.test.ts
@@ -1,6 +1,7 @@
import { expect, assert } from 'vitest'
import { sandboxTest } from '../../setup.js'
+import { TimeoutError } from '../../../src/index.js'
sandboxTest('run', async ({ sandbox }) => {
const text = 'Hello, World!'
@@ -42,3 +43,12 @@ sandboxTest('run with too short timeout', async ({ sandbox }) => {
sandbox.commands.run('sleep 10', { timeoutMs: 1000 })
).rejects.toThrow()
})
+
+sandboxTest('run with too short timeout iterating', async ({ sandbox }) => {
+ const handle = await sandbox.commands.run('sleep 10', {
+ timeoutMs: 2000,
+ background: true,
+ })
+
+ await expect(handle.wait()).rejects.toThrowError(TimeoutError)
+})
diff --git a/packages/python-sdk/e2b/envd/rpc.py b/packages/python-sdk/e2b/envd/rpc.py
index 72ace254d..829fec668 100644
--- a/packages/python-sdk/e2b/envd/rpc.py
+++ b/packages/python-sdk/e2b/envd/rpc.py
@@ -74,6 +74,16 @@ def handle_rpc_exception(
if isinstance(e, httpcore.RemoteProtocolError):
return format_terminated_exception(e, sandbox_running)
+ # A transport-level timeout from httpcore (e.g. a stream read timeout) means the
+ # configured timeout was exceeded before the server responded. On a streaming call
+ # the read timeout equals the command `timeout`, so it races with the server's own
+ # `deadline_exceeded` response — whichever fires first wins. Map it to a
+ # `TimeoutException` so callers see a consistent timeout error either way.
+ if isinstance(e, httpcore.TimeoutException):
+ return TimeoutException(
+ f"{e}: This error is likely due to exceeding 'timeout' — the total time a long running request (like process or directory watch) can be active — or 'request_timeout'. You can modify these by passing 'timeout' or 'request_timeout' when making the request. Use '0' to disable the timeout."
+ )
+
return e
diff --git a/packages/python-sdk/tests/test_envd_rpc_exception.py b/packages/python-sdk/tests/test_envd_rpc_exception.py
index 71109d3da..2e2b53b33 100644
--- a/packages/python-sdk/tests/test_envd_rpc_exception.py
+++ b/packages/python-sdk/tests/test_envd_rpc_exception.py
@@ -61,6 +61,19 @@ def test_returns_raw_network_errors():
assert err is original
+def test_maps_read_timeout_to_timeout():
+ # A stream read timeout fires when the command `timeout` is exceeded before the
+ # server's own `deadline_exceeded` response arrives — it must surface as a
+ # TimeoutException rather than leaking the raw httpcore error.
+ err = handle_rpc_exception(httpcore.ReadTimeout("the read operation timed out"))
+ assert isinstance(err, TimeoutException)
+
+
+def test_maps_connect_timeout_to_timeout():
+ err = handle_rpc_exception(httpcore.ConnectTimeout("connect timed out"))
+ assert isinstance(err, TimeoutException)
+
+
def test_returns_original_when_not_connect_exception():
original = ValueError("not connect")
err = handle_rpc_exception(original)