Process-Level Isolation and Timeouts
What you'll learn: How to run untrusted LLM-generated code safely using subprocess isolation with strict time and resource limits.
The Core Idea
When your AI system generates code that must execute locally (not in a container or serverless function), you need process-level sandboxing. This means spawning the code in a separate operating system process with hard limits on execution time, memory usage, and system access. Think of it like letting someone use your kitchen, but setting a timer, limiting which cabinets they can open, and standing ready to pull the fire alarm if things go wrong.
How It Works
Use your language's subprocess execution tools with explicit constraints:
- Timeout limits: Kill processes that run too long (prevent infinite loops)
- Memory caps: Restrict RAM usage (prevent memory bombs)
- Signal handling: Gracefully terminate or forcibly kill runaway processes
- Restricted permissions: Run as low-privilege user, disable network/file access where possible
This creates a "disposable" execution environment. If the generated code misbehaves, the subprocess dies without affecting your main application.
When to Use This Approach
Process-level isolation works well for:
- Quick code execution on your existing infrastructure
- Environments where Docker/serverless adds too much latency
- Testing and development scenarios
- Cases where you control the entire stack
However, it provides weaker isolation than containers. A determined attacker might still escape through OS vulnerabilities. Combine with input validation and output filtering for defense in depth.
Key Takeaway: Process-level isolation with timeouts and resource limits provides fast, lightweight sandboxing for locally executing LLM-generated code, but should be part of a layered security strategy rather than your only defense mechanism.