Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(sdks): transport proxy caching, request timeout, and option handling (#1421)

Mish Ushakov1d5259ca1d65
diff --git a/.changeset/sdk-review-fixes.md b/.changeset/sdk-review-fixes.md
new file mode 100644
index 000000000..1aacea00a
--- /dev/null
+++ b/.changeset/sdk-review-fixes.md
@@ -0,0 +1,12 @@
+---
+"@e2b/python-sdk": patch
+"e2b": patch
+---
+
+Fix a batch of connection-handling bugs in the JS and Python SDKs:
+
+- **Python**: HTTP transport caches now key on the configured proxy, so clients created with different (or no) proxy settings no longer silently reuse a transport built for the first proxy seen.
+- **Python**: `request_timeout` is now applied to control-plane (E2B API) requests; previously the underlying httpx client was built with no timeout at all.
+- **Python**: the server-stream parser no longer stalls (or drops the final envelope) when the remaining payload of an envelope is shorter than the 5-byte envelope header.
+- **JS + Python**: passing `debug: false` explicitly now overrides the `E2B_DEBUG=true` environment variable instead of being ignored.
+- **JS**: the RPC logger no longer crashes requests with a `TypeError` when a response contains protobuf int64 (`bigint`) fields; they are now logged as strings.
diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.ts
index 9571dda83..35ee59e77 100644
--- a/packages/js-sdk/src/connectionConfig.ts
+++ b/packages/js-sdk/src/connectionConfig.ts
@@ -206,7 +206,7 @@ export class ConnectionConfig {
constructor(opts?: ConnectionOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
- this.debug = opts?.debug || ConnectionConfig.debug
+ this.debug = opts?.debug ?? ConnectionConfig.debug
this.domain = opts?.domain || ConnectionConfig.domain
this.accessToken = opts?.accessToken || ConnectionConfig.accessToken
this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS
diff --git a/packages/js-sdk/src/logs.ts b/packages/js-sdk/src/logs.ts
index a483389a7..d3ed59f21 100644
--- a/packages/js-sdk/src/logs.ts
+++ b/packages/js-sdk/src/logs.ts
@@ -24,7 +24,13 @@ export interface Logger {
}
function formatLog(log: any) {
- return JSON.parse(JSON.stringify(log))
+ // Protobuf int64 fields are represented as bigint, which JSON.stringify
+ // can't serialize without a replacer.
+ return JSON.parse(
+ JSON.stringify(log, (_, value) =>
+ typeof value === 'bigint' ? value.toString() : value
+ )
+ )
}
export function createRpcLogger(logger: Logger): Interceptor {
diff --git a/packages/js-sdk/tests/connectionConfig.test.ts b/packages/js-sdk/tests/connectionConfig.test.ts
index 207255d28..dbb0aabff 100644
--- a/packages/js-sdk/tests/connectionConfig.test.ts
+++ b/packages/js-sdk/tests/connectionConfig.test.ts
@@ -146,6 +146,20 @@ test('sandbox_url stays localhost in debug mode', () => {
)
})
+test('debug false in args overrides E2B_DEBUG env var', () => {
+ process.env.E2B_DEBUG = 'true'
+
+ const config = new ConnectionConfig({ debug: false })
+ assert.equal(config.debug, false)
+})
+
+test('debug defaults to E2B_DEBUG env var', () => {
+ process.env.E2B_DEBUG = 'true'
+
+ const config = new ConnectionConfig()
+ assert.equal(config.debug, true)
+})
+
test('getSignal returns user signal when no timeout is set', () => {
const config = new ConnectionConfig({ requestTimeoutMs: 0 })
const controller = new AbortController()
diff --git a/packages/js-sdk/tests/logs.test.ts b/packages/js-sdk/tests/logs.test.ts
new file mode 100644
index 000000000..e22dc3dac
--- /dev/null
+++ b/packages/js-sdk/tests/logs.test.ts
@@ -0,0 +1,39 @@
+import { assert, describe, test } from 'vitest'
+import { createRpcLogger } from '../src/logs'
+
+const req = { url: 'http://localhost:49983/process.Process/Start' } as any
+
+describe('createRpcLogger', () => {
+ test('logs unary responses containing bigint fields without throwing', async () => {
+ const logs: any[][] = []
+ const interceptor = createRpcLogger({ info: (...args) => logs.push(args) })
+
+ const res = { stream: false, message: { size: 42n, name: 'file' } } as any
+ const result = await interceptor(async () => res)(req)
+
+ assert.equal(result, res)
+ assert.deepEqual(logs[1], ['Response:', { size: '42', name: 'file' }])
+ })
+
+ test('logs streamed messages containing bigint fields without throwing', async () => {
+ const logs: any[][] = []
+ const interceptor = createRpcLogger({ debug: (...args) => logs.push(args) })
+
+ async function* stream() {
+ yield { offset: 9007199254740993n }
+ }
+ const res = { stream: true, message: stream() } as any
+ const result = (await interceptor(async () => res)(req)) as any
+
+ const received = []
+ for await (const m of result.message) {
+ received.push(m)
+ }
+
+ assert.deepEqual(received, [{ offset: 9007199254740993n }])
+ assert.deepEqual(logs[0], [
+ 'Response stream:',
+ { offset: '9007199254740993' },
+ ])
+ })
+})
diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.py
index 07830bc12..b48bf3b7c 100644
--- a/packages/python-sdk/e2b/api/__init__.py
+++ b/packages/python-sdk/e2b/api/__init__.py
@@ -6,7 +6,7 @@ from dataclasses import dataclass
from types import TracebackType
from typing import Optional, Protocol, Union
-from httpx import AsyncBaseTransport, BaseTransport, Limits
+from httpx import AsyncBaseTransport, BaseTransport, Limits, Timeout
from e2b.api.client.client import AuthenticatedClient
from e2b.api.client.types import Response
@@ -162,6 +162,10 @@ class ApiClient(AuthenticatedClient):
if transport is None:
httpx_args["proxy"] = config.proxy
+ # config.request_timeout is None when the timeout is explicitly
+ # disabled (request_timeout=0), which httpx.Timeout(None) preserves.
+ kwargs.setdefault("timeout", Timeout(config.request_timeout))
+
super().__init__(
base_url=config.api_url,
httpx_args=httpx_args,
diff --git a/packages/python-sdk/e2b/api/client_async/__init__.py b/packages/python-sdk/e2b/api/client_async/__init__.py
index 85300f590..842034dce 100644
--- a/packages/python-sdk/e2b/api/client_async/__init__.py
+++ b/packages/python-sdk/e2b/api/client_async/__init__.py
@@ -1,14 +1,18 @@
import asyncio
import logging
-from typing import Dict, Tuple
+from typing import Dict, Optional, Tuple
import httpx
+from httpx._types import ProxyTypes
+
from e2b.api import AsyncApiClient, limits
from e2b.connection_config import ConnectionConfig
logger = logging.getLogger(__name__)
+TransportKey = Tuple[int, bool, Optional[ProxyTypes]]
+
def get_api_client(config: ConnectionConfig, **kwargs) -> AsyncApiClient:
return AsyncApiClient(
@@ -19,7 +23,7 @@ def get_api_client(config: ConnectionConfig, **kwargs) -> AsyncApiClient:
class AsyncTransportWithLogger(httpx.AsyncHTTPTransport):
- _instances: Dict[Tuple[int, bool], "AsyncTransportWithLogger"] = {}
+ _instances: Dict[TransportKey, "AsyncTransportWithLogger"] = {}
async def handle_async_request(self, request):
url = f"{request.url.scheme}://{request.url.host}{request.url.path}"
@@ -39,10 +43,10 @@ class AsyncTransportWithLogger(httpx.AsyncHTTPTransport):
def get_transport(
config: ConnectionConfig, http2: bool = True
) -> AsyncTransportWithLogger:
- loop_id = (id(asyncio.get_running_loop()), http2)
+ key: TransportKey = (id(asyncio.get_running_loop()), http2, config.proxy)
- if loop_id in AsyncTransportWithLogger._instances:
- return AsyncTransportWithLogger._instances[loop_id]
+ if key in AsyncTransportWithLogger._instances:
+ return AsyncTransportWithLogger._instances[key]
transport = AsyncTransportWithLogger(
limits=limits,
@@ -50,21 +54,21 @@ def get_transport(
http2=http2,
)
- AsyncTransportWithLogger._instances[loop_id] = transport
+ AsyncTransportWithLogger._instances[key] = transport
return transport
class AsyncEnvdTransportWithLogger(AsyncTransportWithLogger):
- _instances: Dict[Tuple[int, bool], "AsyncEnvdTransportWithLogger"] = {}
+ _instances: Dict[TransportKey, "AsyncEnvdTransportWithLogger"] = {}
def get_envd_transport(
config: ConnectionConfig, http2: bool = True
) -> AsyncEnvdTransportWithLogger:
- loop_id = (id(asyncio.get_running_loop()), http2)
+ key: TransportKey = (id(asyncio.get_running_loop()), http2, config.proxy)
- if loop_id in AsyncEnvdTransportWithLogger._instances:
- return AsyncEnvdTransportWithLogger._instances[loop_id]
+ if key in AsyncEnvdTransportWithLogger._instances:
+ return AsyncEnvdTransportWithLogger._instances[key]
transport = AsyncEnvdTransportWithLogger(
limits=limits,
@@ -72,5 +76,5 @@ def get_envd_transport(
http2=http2,
)
- AsyncEnvdTransportWithLogger._instances[loop_id] = transport
+ AsyncEnvdTransportWithLogger._instances[key] = transport
return transport
diff --git a/packages/python-sdk/e2b/api/client_sync/__init__.py b/packages/python-sdk/e2b/api/client_sync/__init__.py
index e44c43707..1a14fe3f2 100644
--- a/packages/python-sdk/e2b/api/client_sync/__init__.py
+++ b/packages/python-sdk/e2b/api/client_sync/__init__.py
@@ -1,14 +1,18 @@
-from typing import Dict
+from typing import Dict, Optional, Tuple
import httpx
import logging
import threading
+from httpx._types import ProxyTypes
+
from e2b.api import ApiClient, limits
from e2b.connection_config import ConnectionConfig
logger = logging.getLogger(__name__)
+TransportKey = Tuple[bool, Optional[ProxyTypes]]
+
def get_api_client(config: ConnectionConfig, **kwargs) -> ApiClient:
return ApiClient(
@@ -37,10 +41,11 @@ class TransportWithLogger(httpx.HTTPTransport):
def get_transport(config: ConnectionConfig, http2: bool = True) -> TransportWithLogger:
- instances: Dict[bool, TransportWithLogger] = getattr(
+ instances: Dict[TransportKey, TransportWithLogger] = getattr(
TransportWithLogger._thread_local, "instances", {}
)
- cached = instances.get(http2)
+ key: TransportKey = (http2, config.proxy)
+ cached = instances.get(key)
if cached is not None:
return cached
@@ -49,7 +54,7 @@ def get_transport(config: ConnectionConfig, http2: bool = True) -> TransportWith
proxy=config.proxy,
http2=http2,
)
- instances[http2] = transport
+ instances[key] = transport
TransportWithLogger._thread_local.instances = instances
return transport
@@ -61,10 +66,11 @@ class EnvdTransportWithLogger(TransportWithLogger):
def get_envd_transport(
config: ConnectionConfig, http2: bool = True
) -> EnvdTransportWithLogger:
- instances: Dict[bool, EnvdTransportWithLogger] = getattr(
+ instances: Dict[TransportKey, EnvdTransportWithLogger] = getattr(
EnvdTransportWithLogger._thread_local, "instances", {}
)
- cached = instances.get(http2)
+ key: TransportKey = (http2, config.proxy)
+ cached = instances.get(key)
if cached is not None:
return cached
@@ -73,6 +79,6 @@ def get_envd_transport(
proxy=config.proxy,
http2=http2,
)
- instances[http2] = transport
+ instances[key] = transport
EnvdTransportWithLogger._thread_local.instances = instances
return transport
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index 47b13f101..d1b80ca0f 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -95,7 +95,7 @@ class ConnectionConfig:
proxy: Optional[ProxyTypes] = None,
):
self.domain = domain or ConnectionConfig._domain()
- self.debug = debug or ConnectionConfig._debug()
+ self.debug = debug if debug is not None else ConnectionConfig._debug()
self.api_key = api_key or ConnectionConfig._api_key()
self.access_token = access_token or ConnectionConfig._access_token()
self.headers = {**(headers or {}), **(api_headers or {})}
diff --git a/packages/python-sdk/e2b_connect/client.py b/packages/python-sdk/e2b_connect/client.py
index b41fedb03..7e1ced394 100644
--- a/packages/python-sdk/e2b_connect/client.py
+++ b/packages/python-sdk/e2b_connect/client.py
@@ -361,7 +361,6 @@ class Client:
},
}
- @_retry(RemoteProtocolError, 3)
async def acall_server_stream(
self,
req,
@@ -395,7 +394,6 @@ class Client:
for parsed in parser.parse(chunk):
yield parsed
- @_retry(RemoteProtocolError, 3)
def call_server_stream(
self,
req,
@@ -479,7 +477,10 @@ class ServerStreamParser:
def parse(self, chunk: bytes) -> Generator[Any, None, None]:
self.buffer += chunk
- while len(self.buffer) >= envelope_header_length:
+ # Once the header is consumed, the remaining payload can be shorter
+ # than the header length, so only require a full header when we still
+ # need to read one.
+ while self._header is not None or len(self.buffer) >= envelope_header_length:
flags, data_len = self.header
if data_len > len(self.buffer):
diff --git a/packages/python-sdk/tests/e2b_connect/test_client.py b/packages/python-sdk/tests/e2b_connect/test_client.py
index ea1f27260..58004a318 100644
--- a/packages/python-sdk/tests/e2b_connect/test_client.py
+++ b/packages/python-sdk/tests/e2b_connect/test_client.py
@@ -2,7 +2,12 @@ import asyncio
import pytest
-from e2b_connect.client import _retry
+from e2b_connect.client import (
+ EnvelopeFlags,
+ ServerStreamParser,
+ _retry,
+ encode_envelope,
+)
class GoodError(Exception):
@@ -132,3 +137,35 @@ async def test_async_with_multiple_await_calls():
result = await f()
assert result is True
assert total == 2
+
+
+def make_parser():
+ return ServerStreamParser(
+ decode=lambda data, msg_type: data,
+ response_type=None,
+ )
+
+
+def test_parser_yields_messages_from_single_chunk():
+ parser = make_parser()
+ envelope = encode_envelope(flags=EnvelopeFlags(0), data=b"abc")
+
+ assert list(parser.parse(envelope)) == [b"abc"]
+
+
+def test_parser_handles_payload_split_after_header():
+ parser = make_parser()
+ envelope = encode_envelope(flags=EnvelopeFlags(0), data=b"abc")
+
+ # The header plus one payload byte arrive first; the remaining payload
+ # (shorter than a header) arrives in a later chunk.
+ assert list(parser.parse(envelope[:6])) == []
+ assert list(parser.parse(envelope[6:])) == [b"abc"]
+
+
+def test_parser_handles_end_stream_payload_shorter_than_header():
+ parser = make_parser()
+ envelope = encode_envelope(flags=EnvelopeFlags.end_stream, data=b"{}")
+
+ assert list(parser.parse(envelope[:5])) == []
+ assert list(parser.parse(envelope[5:])) == []
diff --git a/packages/python-sdk/tests/test_api_client_transport.py b/packages/python-sdk/tests/test_api_client_transport.py
index 9b96491f5..981b08c4e 100644
--- a/packages/python-sdk/tests/test_api_client_transport.py
+++ b/packages/python-sdk/tests/test_api_client_transport.py
@@ -1,6 +1,7 @@
import asyncio
from concurrent.futures import ThreadPoolExecutor
+import httpx
import pytest
from e2b.api.client_async import AsyncEnvdTransportWithLogger, AsyncTransportWithLogger
@@ -65,6 +66,61 @@ def test_sync_get_transport_http2_opt_out_returns_distinct_instance(test_api_key
reset_sync_api_transports()
+def test_sync_get_transport_keyed_by_proxy(test_api_key):
+ reset_sync_api_transports()
+ proxied_config = ConnectionConfig(
+ api_key=test_api_key,
+ proxy="http://127.0.0.1:9999",
+ )
+ direct_config = ConnectionConfig(api_key=test_api_key)
+ other_proxy_config = ConnectionConfig(
+ api_key=test_api_key,
+ proxy="http://127.0.0.1:8888",
+ )
+
+ try:
+ proxied_transport = get_sync_transport(proxied_config)
+ direct_transport = get_sync_transport(direct_config)
+ other_proxy_transport = get_sync_transport(other_proxy_config)
+
+ assert proxied_transport is not direct_transport
+ assert proxied_transport is not other_proxy_transport
+ assert direct_transport is not other_proxy_transport
+ # The same proxy still reuses the cached instance.
+ assert get_sync_transport(proxied_config) is proxied_transport
+ assert get_sync_transport(direct_config) is direct_transport
+ finally:
+ reset_sync_api_transports()
+
+
+def test_sync_api_client_applies_request_timeout(test_api_key):
+ reset_sync_api_transports()
+ config = ConnectionConfig(api_key=test_api_key, request_timeout=1.5)
+
+ api_client = get_sync_api_client(config)
+ httpx_client = api_client.get_httpx_client()
+
+ try:
+ assert httpx_client.timeout == httpx.Timeout(1.5)
+ finally:
+ httpx_client.close()
+ reset_sync_api_transports()
+
+
+def test_sync_api_client_request_timeout_zero_disables_timeout(test_api_key):
+ reset_sync_api_transports()
+ config = ConnectionConfig(api_key=test_api_key, request_timeout=0)
+
+ api_client = get_sync_api_client(config)
+ httpx_client = api_client.get_httpx_client()
+
+ try:
+ assert httpx_client.timeout == httpx.Timeout(None)
+ finally:
+ httpx_client.close()
+ reset_sync_api_transports()
+
+
def test_sync_envd_transport_uses_separate_cache(test_api_key):
reset_sync_api_transports()
reset_sync_envd_transports()
@@ -129,7 +185,7 @@ async def test_async_api_client_proxy_uses_explicit_transport(test_api_key):
api_client = get_async_api_client(config)
httpx_client = api_client.get_async_httpx_client()
transport = AsyncTransportWithLogger._instances[
- (id(asyncio.get_running_loop()), True)
+ (id(asyncio.get_running_loop()), True, "http://127.0.0.1:9999")
]
try:
@@ -163,6 +219,42 @@ async def test_async_get_transport_http2_opt_out_returns_distinct_instance(
AsyncTransportWithLogger._instances.clear()
+async def test_async_get_transport_keyed_by_proxy(test_api_key):
+ AsyncTransportWithLogger._instances.clear()
+ proxied_config = ConnectionConfig(
+ api_key=test_api_key,
+ proxy="http://127.0.0.1:9999",
+ )
+ direct_config = ConnectionConfig(api_key=test_api_key)
+
+ try:
+ proxied_transport = get_async_transport(proxied_config)
+ direct_transport = get_async_transport(direct_config)
+
+ assert proxied_transport is not direct_transport
+ # The same proxy still reuses the cached instance.
+ assert get_async_transport(proxied_config) is proxied_transport
+ assert get_async_transport(direct_config) is direct_transport
+ finally:
+ AsyncTransportWithLogger._instances.clear()
+
+
+async def test_async_api_client_applies_request_timeout(test_api_key):
+ AsyncTransportWithLogger._instances.clear()
+ config = ConnectionConfig(api_key=test_api_key, request_timeout=1.5)
+
+ api_client = get_async_api_client(config)
+ httpx_client = api_client.get_async_httpx_client()
+
+ try:
+ assert httpx_client.timeout == httpx.Timeout(1.5)
+ finally:
+ await httpx_client.aclose()
+ AsyncTransportWithLogger._instances.clear()
+
+
@pytest.mark.asyncio
async def test_async_envd_transport_uses_separate_cache(test_api_key):
AsyncTransportWithLogger._instances.clear()
diff --git a/packages/python-sdk/tests/test_connection_config.py b/packages/python-sdk/tests/test_connection_config.py
index c866518b5..67daad871 100644
--- a/packages/python-sdk/tests/test_connection_config.py
+++ b/packages/python-sdk/tests/test_connection_config.py
@@ -96,6 +96,20 @@ def test_sandbox_direct_url_uses_explicit_url_first():
)
+def test_debug_false_overrides_env_var(monkeypatch):
+ monkeypatch.setenv("E2B_DEBUG", "true")
+
+ config = ConnectionConfig(debug=False)
+ assert config.debug is False
+
+
+def test_debug_defaults_to_env_var(monkeypatch):
+ monkeypatch.setenv("E2B_DEBUG", "true")
+
+ config = ConnectionConfig()
+ assert config.debug is True
+
+
def test_request_timeout_zero_means_no_timeout():
config = ConnectionConfig(request_timeout=0)
assert config.request_timeout is None