SandboxFactory.create_sandbox() reuses a precompiled artifact but constructs a new multithreaded Tokio runtime for each child sandbox. Reuse a runtime within the factory to avoid repeated worker-thread creation and teardown while preserving independently configured child sandboxes.
The public implementation shows:
Proposed approach:
- Share an
Arc<tokio::runtime::Runtime> among a factory's children, covering both new and loaded factories.
- Keep child-specific callbacks, output handlers, secrets and resource limits separate.
- Preserve fresh guest Store/instance and default VFS creation for each stateless
Sandbox.execute() call; this does not require reusing persistent sessions between tenants.
- Define lazy initialization, prefork/process ownership and shutdown behavior. Children must remain usable after their factory is dropped.
Validate concurrent execution, callback routing, destruction order, Python GIL interactions, error/timeout recovery and thread-count stability. Benchmark total process CPU and latency for repeated child creation/execution/teardown to quantify the benefit.
Related: #411 optimizes Wasmtime allocation and preparation of fresh instances. Sharing the Python bindings' Tokio runtime is a separate optimization.
SandboxFactory.create_sandbox()reuses a precompiled artifact but constructs a new multithreaded Tokio runtime for each child sandbox. Reuse a runtime within the factory to avoid repeated worker-thread creation and teardown while preserving independently configured child sandboxes.The public implementation shows:
create_sandbox()callsSandbox::from_inner().Sandbox::from_inner()builds a new multithreaded Tokio runtime on every call.create_session()also constructs a runtime per child.Proposed approach:
Arc<tokio::runtime::Runtime>among a factory's children, covering both new and loaded factories.Sandbox.execute()call; this does not require reusing persistent sessions between tenants.Validate concurrent execution, callback routing, destruction order, Python GIL interactions, error/timeout recovery and thread-count stability. Benchmark total process CPU and latency for repeated child creation/execution/teardown to quantify the benefit.
Related: #411 optimizes Wasmtime allocation and preparation of fresh instances. Sharing the Python bindings' Tokio runtime is a separate optimization.