Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-donkeys-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-python': patch
---

Abort Jupyter requests at the deadline they were given instead of twice it. The SDK's pyqwest-backed transport derives a single whole-request deadline by summing httpx's `read` and `write` phases, and every request set both to the requested timeout — so `run_code(timeout=1)` ran for up to 2 seconds, and the context requests (`request_timeout`) likewise bought twice their value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Optional

import httpx


def _jupyter_timeout(
timeout: Optional[float], *, connect: Optional[float] = None
) -> httpx.Timeout:
"""Build the `httpx.Timeout` every Jupyter request derives its deadline from.

The SDK's pyqwest-backed transport has no per-phase timeouts: it collapses
them into a single whole-request deadline, derived as the sum of the
`read` and `write` phases (`connect` and `pool` are ignored). The whole
budget therefore goes on `read` and `write` stays at zero, which makes the
deadline `timeout` and not `2 * timeout`. This matches the JS SDK, which
aborts the execution on a `timeout`-long timer. A `None` timeout disables
the deadline entirely and sets no phase at all — the transport still bounds
connect on its own.
"""
if timeout is None:
return httpx.Timeout(None)
return httpx.Timeout(timeout, write=0, connect=connect)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from e2b.api.client_async import get_transport

from e2b_code_interpreter._jupyter_timeout import _jupyter_timeout
from e2b_code_interpreter.constants import (
DEFAULT_TEMPLATE,
JUPYTER_PORT,
Expand Down Expand Up @@ -217,21 +218,8 @@ async def run_code(
},
headers=headers,
# `timeout` bounds the execution, `request_timeout` only the
# connect. Every non-connect phase must carry `timeout`: the
# SDK's pyqwest-backed transport collapses the per-phase
# timeouts into a single whole-request deadline and takes the
# longest of them, so leaving `request_timeout` on the write
# and pool phases would raise the floor to
# `max(timeout, request_timeout)` and silently ignore any
# `timeout` shorter than it. This matches the JS SDK, which
# aborts the execution on a `timeout`-long timer.
# `timeout=0` disables the deadline entirely; the transport
# still bounds connect on its own.
timeout=(
httpx.Timeout(timeout, connect=request_timeout)
if timeout is not None
else httpx.Timeout(None)
),
# connect; `_jupyter_timeout` holds the transport details.
timeout=_jupyter_timeout(timeout, connect=request_timeout),
) as response:
err = await aextract_exception(response)
if err:
Expand Down Expand Up @@ -294,7 +282,9 @@ async def create_code_context(
f"{self._jupyter_url}/contexts",
headers=headers,
json=data,
timeout=request_timeout or self.connection_config.request_timeout,
timeout=_jupyter_timeout(
self.connection_config.get_request_timeout(request_timeout)
),
)

err = await aextract_exception(response)
Expand Down Expand Up @@ -334,7 +324,7 @@ async def remove_code_context(
response = await self._client.delete(
f"{self._jupyter_url}/contexts/{context_id}",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = await aextract_exception(response)
Expand Down Expand Up @@ -364,7 +354,7 @@ async def list_code_contexts(self) -> List[Context]:
response = await self._client.get(
f"{self._jupyter_url}/contexts",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = await aextract_exception(response)
Expand Down Expand Up @@ -403,7 +393,7 @@ async def restart_code_context(
response = await self._client.post(
f"{self._jupyter_url}/contexts/{context_id}/restart",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = await aextract_exception(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from e2b import Sandbox as BaseSandbox, InvalidArgumentException
from e2b.api.client_sync import get_transport

from e2b_code_interpreter._jupyter_timeout import _jupyter_timeout
from e2b_code_interpreter.constants import (
DEFAULT_TEMPLATE,
JUPYTER_PORT,
Expand Down Expand Up @@ -214,21 +215,8 @@ def run_code(
},
headers=headers,
# `timeout` bounds the execution, `request_timeout` only the
# connect. Every non-connect phase must carry `timeout`: the
# SDK's pyqwest-backed transport collapses the per-phase
# timeouts into a single whole-request deadline and takes the
# longest of them, so leaving `request_timeout` on the write
# and pool phases would raise the floor to
# `max(timeout, request_timeout)` and silently ignore any
# `timeout` shorter than it. This matches the JS SDK, which
# aborts the execution on a `timeout`-long timer.
# `timeout=0` disables the deadline entirely; the transport
# still bounds connect on its own.
timeout=(
httpx.Timeout(timeout, connect=request_timeout)
if timeout is not None
else httpx.Timeout(None)
),
# connect; `_jupyter_timeout` holds the transport details.
timeout=_jupyter_timeout(timeout, connect=request_timeout),
) as response:
err = extract_exception(response)
if err:
Expand Down Expand Up @@ -291,7 +279,9 @@ def create_code_context(
f"{self._jupyter_url}/contexts",
json=data,
headers=headers,
timeout=request_timeout or self.connection_config.request_timeout,
timeout=_jupyter_timeout(
self.connection_config.get_request_timeout(request_timeout)
),
)

err = extract_exception(response)
Expand Down Expand Up @@ -331,7 +321,7 @@ def remove_code_context(
response = self._client.delete(
f"{self._jupyter_url}/contexts/{context_id}",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = extract_exception(response)
Expand Down Expand Up @@ -361,7 +351,7 @@ def list_code_contexts(self) -> List[Context]:
response = self._client.get(
f"{self._jupyter_url}/contexts",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = extract_exception(response)
Expand Down Expand Up @@ -401,7 +391,7 @@ def restart_code_context(
response = self._client.post(
f"{self._jupyter_url}/contexts/{context_id}/restart",
headers=headers,
timeout=self.connection_config.request_timeout,
timeout=_jupyter_timeout(self.connection_config.request_timeout),
)

err = extract_exception(response)
Expand Down
Loading