pub struct PooledSandbox { /* private fields */ }Expand description
A sandbox acquired from a pool.
This wrapper ensures the sandbox is returned to the pool when dropped.
It implements Deref and DerefMut to allow transparent access to the
underlying Sandbox.
Implementations§
Source§impl PooledSandbox
impl PooledSandbox
Sourcepub fn sandbox_mut(&mut self) -> &mut Sandbox
pub fn sandbox_mut(&mut self) -> &mut Sandbox
Get a mutable reference to the underlying sandbox.
Sourcepub fn detach(self) -> Sandbox
pub fn detach(self) -> Sandbox
Detach the sandbox from the pool, preventing it from being returned.
This consumes the PooledSandbox and returns the underlying Sandbox.
The sandbox will not be returned to the pool.
Note: This still releases the semaphore permit, so another sandbox can be created to take its place in the pool.
Sourcepub fn with_callbacks(self, callbacks: Vec<Box<dyn Callback>>) -> Self
pub fn with_callbacks(self, callbacks: Vec<Box<dyn Callback>>) -> Self
Set callbacks for this request.
These will be cleared automatically when the sandbox is returned to the pool.
Sourcepub fn with_trace_handler<H: TraceHandler + 'static>(self, handler: H) -> Self
pub fn with_trace_handler<H: TraceHandler + 'static>(self, handler: H) -> Self
Set a trace handler for this request.
This will be cleared automatically when the sandbox is returned to the pool.
Sourcepub fn with_output_handler<H: OutputHandler + 'static>(self, handler: H) -> Self
pub fn with_output_handler<H: OutputHandler + 'static>(self, handler: H) -> Self
Set an output handler for this request.
This will be cleared automatically when the sandbox is returned to the pool.
Sourcepub fn with_resource_limits(self, limits: ResourceLimits) -> Self
pub fn with_resource_limits(self, limits: ResourceLimits) -> Self
Set resource limits for this request.
These will be reset to defaults when the sandbox is returned to the pool.
Sourcepub fn with_vfs_storage(self, storage: Arc<dyn VfsStorage>) -> Self
Available on crate feature vfs only.
pub fn with_vfs_storage(self, storage: Arc<dyn VfsStorage>) -> Self
vfs only.Set VFS storage for this request.
This will be cleared automatically when the sandbox is returned to the pool. The provided storage replaces the default scrubbing storage created during execution, allowing pre-populated files to be available to Python code.
Methods from Deref<Target = Sandbox>§
Sourcepub async fn execute(&self, code: &str) -> Result<ExecuteResult, Error>
pub async fn execute(&self, code: &str) -> Result<ExecuteResult, Error>
Execute Python code in the sandbox.
If an OutputHandler was configured, stdout is streamed to it during execution.
If a TraceHandler was configured, trace events are emitted during execution.
Returns the final result including complete stdout and collected trace events.
§Errors
Returns an error if the Python code fails to execute or a resource limit is exceeded.
Sourcepub async fn execute_with_journal(&self, code: &str) -> ReplayOutcome
pub async fn execute_with_journal(&self, code: &str) -> ReplayOutcome
Execute Python code with callback-result replay and journaling.
This behaves like execute but additionally records a
CallbackJournal of every callback invocation, and — if a previous
journal was configured via
SandboxBuilder::with_replay_journal — replays matching callbacks from
that journal instead of invoking them live. See the
replay module for the full model.
The returned ReplayOutcome always carries the freshly-recorded
journal, even when execution fails, so a later resubmission can replay
everything that completed.
Each call uses fresh replay state, so a sandbox may be executed repeatedly without the journal cursor leaking between runs.
§Security
Replayed journal entries are returned to Python verbatim — the callback
is not re-executed. A crafted journal can therefore inject arbitrary
values into the script. Only replay journals produced by a trusted
source (e.g. a previous run of the same sandbox, or a journal verified
via HMAC signature). See the replay module docs for
details.
Sourcepub fn type_stubs(&self) -> &str
pub fn type_stubs(&self) -> &str
Get combined type stubs for all loaded libraries. Useful for including in LLM context windows.
Sourcepub fn callbacks(&self) -> &HashMap<String, Arc<dyn Callback>>
pub fn callbacks(&self) -> &HashMap<String, Arc<dyn Callback>>
Get a reference to the registered callbacks.
Sourcepub fn trace_handler(&self) -> &Option<Arc<dyn TraceHandler>>
pub fn trace_handler(&self) -> &Option<Arc<dyn TraceHandler>>
Get a reference to the trace handler.
Sourcepub fn output_handler(&self) -> &Option<Arc<dyn OutputHandler>>
pub fn output_handler(&self) -> &Option<Arc<dyn OutputHandler>>
Get a reference to the output handler.
Sourcepub fn resource_limits(&self) -> &ResourceLimits
pub fn resource_limits(&self) -> &ResourceLimits
Get a reference to the resource limits.
Sourcepub fn executor(&self) -> Arc<PythonExecutor> ⓘ
pub fn executor(&self) -> Arc<PythonExecutor> ⓘ
Get a reference to the Python executor.
This allows creating a SessionExecutor from a pooled sandbox,
enabling state persistence between executions.
Sourcepub fn execute_cancellable(&self, code: &str) -> ExecutionHandle
pub fn execute_cancellable(&self, code: &str) -> ExecutionHandle
Execute Python code with cancellation support.
Returns an ExecutionHandle that can be used to cancel the execution
or wait for its completion.
§Example
let handle = sandbox.execute_cancellable("while True: pass").await?;
// Cancel after 5 seconds from another task
let cancel_handle = handle.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(5)).await;
cancel_handle.cancel();
});
// Wait for result
match handle.wait().await {
Ok(result) => println!("Completed: {}", result.stdout),
Err(Error::Cancelled) => println!("Cancelled"),
Err(e) => println!("Error: {e}"),
}§Errors
Returns an error if the execution cannot be started.
Trait Implementations§
Source§impl Debug for PooledSandbox
impl Debug for PooledSandbox
Source§impl Deref for PooledSandbox
impl Deref for PooledSandbox
Source§impl DerefMut for PooledSandbox
impl DerefMut for PooledSandbox
Source§impl Drop for PooledSandbox
impl Drop for PooledSandbox
Auto Trait Implementations§
impl !RefUnwindSafe for PooledSandbox
impl !UnwindSafe for PooledSandbox
impl Freeze for PooledSandbox
impl Send for PooledSandbox
impl Sync for PooledSandbox
impl Unpin for PooledSandbox
impl UnsafeUnpin for PooledSandbox
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
Source§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
self file descriptor.Source§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
Source§fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: Sized + AsFilelike,
fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: Sized + AsFilelike,
self file descriptor. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more