SandboxNotFoundExpiredError
e2b.errors.SandboxNotFoundExpiredError
Stack trace
Traceback (most recent call last):
File "main.py", line 42, in <module>
sandbox = e2b.get_sandbox(sandbox_id)
File "/usr/local/lib/python3.9/site-packages/e2b/api.py", line 88, in get_sandbox
raise SandboxNotFoundExpiredError(f"Sandbox {sandbox_id} not found or expired.")
e2b.errors.SandboxNotFoundExpiredError: Sandbox 12345 not found or expired. Why it happens
This error occurs when the requested sandbox environment either does not exist or its session has expired due to timeout or manual invalidation. The E2B platform requires an active sandbox context to run code, and if the sandbox is missing or expired, operations depending on it will fail.
Detection
Check for SandboxNotFoundExpiredError exceptions when accessing sandbox environments and log the sandbox ID and timestamps to detect expired or missing sandboxes before critical operations.
Causes & fixes
The sandbox session expired due to inactivity or timeout.
Recreate or refresh the sandbox session before running operations, ensuring the sandbox is active and valid.
The sandbox ID provided is incorrect or does not exist.
Verify the sandbox ID is correct and corresponds to an existing active sandbox in the E2B system.
The sandbox was manually deleted or invalidated by an administrator.
Request a new sandbox environment or check with the administrator to restore access.
Code: broken vs fixed
import os
import e2b
sandbox_id = "12345"
sandbox = e2b.get_sandbox(sandbox_id) # This line raises SandboxNotFoundExpiredError
sandbox.run_code("print('Hello World')") import os
import e2b
sandbox_id = "12345"
try:
sandbox = e2b.get_sandbox(sandbox_id)
except e2b.errors.SandboxNotFoundExpiredError:
sandbox = e2b.create_sandbox() # Recreate sandbox if expired or not found
sandbox.run_code("print('Hello World')") # Fixed: sandbox ensured active Workaround
Wrap sandbox access in try/except SandboxNotFoundExpiredError and automatically create a new sandbox session as a fallback to avoid crashes.
Prevention
Implement sandbox session health checks and automatic refresh or recreation logic before executing code to guarantee sandbox availability and prevent expiration errors.