#2845·smolagents

RFC: Process-isolated execution backend for LocalPythonInterpreter (cgroups v2, monotonic watchdog, and fail-closed termination)

Author: shlederCreated Sep 25, 2026Updated Sep 27, 2026

Problem & Motivation

The default LocalPythonInterpreter in smolagents executes model-generated code within the agent's host process. While AST guards (such as the bit-length check in #2564) effectively catch specific known explosive patterns, threads inside a shared runtime cannot provide a reliable security or stability boundary against arbitrary or untrusted code execution:

  1. GIL-holding C-extensions: When native C extensions or native math routines execute long-running loops, they do not release the Python Global Interpreter Lock (GIL) or check Python signal flags. A Python thread timeout (Future.result(timeout=...) or threading.Timer) can only abandon the future, leaving the leaked background thread running and blocking future submit() worker calls.
  2. Memory Exhaustion (OOM): Computations that allocate large data structures (e.g. huge tensors, unconstrained lists, or regex backtracking) consume the memory of the main agent host process. When the Linux kernel OOM killer triggers, it frequently terminates the parent agent process rather than the offending snippet.
  3. Filesystem & Network Inadvertent Leakage: Code executed directly in-process inherits full access to host credentials (~/.ssh, ~/.aws, .env), system sockets, and local network interfaces.

Proposed Architecture: Pluggable Process-Isolated Executor

We propose designing an optional, pluggable ProcessIsolatedExecutor (or IsolatedPythonInterpreter) that moves execution from intra-process threads to a lightweight, kernel-isolated child process hierarchy:

[Agent Process] 
       │
       ▼ (stdin/pipe)
[Supervisor Watchdog] (monotonic clock)
       │
       ▼ fork() / unshare()
[Execution Child] (CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | Landlock LSM)
       │
       └── cgroups v2 scope (memory.max, pids.max)

Key Invariants

  1. Subprocess Boundary with Monotonic Deadline:
    • The interpreter spawns a dedicated child process for each tool/code execution block.
    • The supervisor monitors a monotonic wall-clock deadline (CLOCK_MONOTONIC).
    • If the deadline expires, the supervisor delivers SIGKILL directly to the child PID (or PID namespace). Because SIGKILL is handled by the kernel scheduler, native C execution is terminated instantly regardless of GIL state.
  2. Resource Ceilings via Linux Cgroups v2:
    • The child process is placed in a dedicated transient cgroup with explicit memory.max and pids.max.
    • If allocated memory exceeds the ceiling, the kernel terminates only the child worker with an out-of-memory error, leaving the parent agent intact.
  3. Ephemeral VFS & Secret Masking (Landlock / Namespaces):
    • Untrusted code is restricted to read-only access on Python libraries and isolated scratch storage in an ephemeral tmpfs.
    • Sensitive credential paths (~/.ssh, .env, ~/.config) are unmounted or masked with mode 0000.
  4. Execution Protocol & Serialization:
    • Input: Code snippet + captured local state passed via IPC/pipe (or JSON/pickle serialization).
    • Output: Standard streams (stdout, stderr), return value, execution duration, and exit status.

Discussion & Integration Path

How would the maintainers prefer this to be structured within smolagents?

  • Option A: A pluggable backend interface on LocalPythonInterpreter (e.g. executor="process" | "thread" | "docker").
  • Option B: An optional companion package or runtime adapter integrating with existing root-less sandboxing runtimes.

We have production implementations of these unprivileged kernel primitives (Landlock LSM ABI 1–6, cgroups v2 cgroup.kill, monotonic supervisor) running in Vetto with sub-4ms cold start latency, and would be glad to contribute to the design or provide reference implementations if there is interest.


Disclaimer: I am the author/maintainer of Vetto, an open-source sandbox runtime for AI coding agents.