TimeoutError
e2b.sandbox.TimeoutError
Stack trace
Traceback (most recent call last):
File "main.py", line 42, in <module>
result = sandbox.run(code)
File "e2b/sandbox.py", line 128, in run
raise TimeoutError("Sandbox execution time exceeded the limit")
e2b.sandbox.TimeoutError: Sandbox execution time exceeded the limit Why it happens
The E2B sandbox enforces strict execution time limits to prevent runaway or infinite loops. When user code or tasks run longer than the configured timeout, the sandbox forcibly terminates execution and raises this TimeoutError. This often happens with inefficient code, infinite loops, or insufficient timeout settings.
Detection
Monitor sandbox execution durations and catch e2b.sandbox.TimeoutError exceptions to log and alert on timeout events before they crash the application.
Causes & fixes
User code contains an infinite loop or very long-running operation exceeding the sandbox timeout.
Optimize the user code to avoid infinite loops and reduce execution time below the sandbox timeout threshold.
Sandbox timeout configuration is set too low for the expected workload or task complexity.
Increase the sandbox timeout setting in the e2b configuration to allow sufficient time for legitimate code execution.
External dependencies or network calls within the sandboxed code cause delays exceeding the timeout.
Refactor code to minimize blocking external calls or increase timeout; consider asynchronous or timeout-handling logic.
Code: broken vs fixed
from e2b import Sandbox
sandbox = Sandbox(timeout=2) # 2 seconds timeout
result = sandbox.run(user_code) # This line raises TimeoutError
print(result) import os
from e2b import Sandbox
sandbox = Sandbox(timeout=10) # Increased timeout to 10 seconds
result = sandbox.run(user_code) # Now runs without timeout error
print(result) # Shows execution result Workaround
Wrap sandbox.run calls in try/except TimeoutError blocks and implement retry logic or fallback behavior to handle timeouts gracefully.
Prevention
Design sandboxed code to be efficient and predictable in execution time, and configure sandbox timeouts based on realistic workload profiling to avoid unexpected terminations.