Skip to content

perf(python): reuse a Tokio runtime within SandboxFactory #453

Description

@sd2k

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions