1. Symptoms
The clw-sandbox-limit-exceeded error occurs in OpenClaw, the runtime environment for Claw programs executed in a sandboxed mode. This error halts execution when the program surpasses predefined resource quotas designed to prevent abuse, denial-of-service, or infinite loops in constrained environments like web servers, CI/CD pipelines, or multi-tenant systems.
Typical manifestations include:
$ clw run --sandbox sandboxed_app.clw
Error: clw-sandbox-limit-exceeded
Sandbox CPU time limit exceeded: 500ms wall-clock time.
Program terminated at instruction 0x7f8a2b3c: loop iteration 1,000,000.
Metrics: CPU: 520ms, Memory: 128MiB peak, Allocations: 50k.
$ clw sandbox exec --limit-cpu=100ms example.clw
[Sandbox] Initializing with limits: CPU=100ms, Mem=64MiB, FD=1024
[Sandbox] Violation: clw-sandbox-limit-exceeded (CPU)
Peak usage: CPU=105ms, Mem=32MiB
Exit code: 137
In verbose mode (clw run -v), additional diagnostics appear:
[DEBUG] Sandbox monitor: CPU usage spiked to 120% of limit.
[DEBUG] Halting PID 12345 due to quota breach.
Error: clw-sandbox-limit-exceeded
Symptoms often coincide with:
- Programs hanging or timing out unexpectedly.
- High CPU utilization visible in
toporhtop. - No output or partial results before abrupt termination.
- Logs indicating “sandbox violation” in containerized setups (e.g., Docker with OpenClaw).
This error is common in Claw applications with tight loops, recursive functions without tail optimization, excessive allocations, or I/O-bound operations in sandboxed contexts.
2. Root Cause
OpenClaw’s sandbox enforces strict resource limits to ensure safe, predictable execution. Limits are configurable via CLI flags (--limit-cpu, --limit-mem) or config files (sandbox.toml), with defaults suited for untrusted code:
| Resource | Default Limit | Measured By |
|---|---|---|
| CPU Time | 500ms wall-clock | clock_gettime(CLOCK_MONOTONIC) |
| Memory | 256MiB RSS | getrusage() RSS field |
| File Descriptors | 1024 | getrlimit(RLIMIT_NOFILE) |
| Network Bytes | 1MiB | Packet counters |
| Instructions | 10M (emulated) | Interpreter cycles |
The error triggers when any quota is breached. Root causes include:
Inefficient Algorithms: Nested loops or naive recursion exceeding CPU limits.
// Example: Infinite-like loop fn factorial(n: u64) -> u64 { if n == 0 { 1 } else { n * factorial(n - 1) } } fn main() { let _ = factorial(10000); // Stack overflow + CPU burn }Memory Leaks: Repeated allocations without deallocation in safe pointers.
I/O Overuse: Polling files or sockets in tight loops.
Misconfigured Limits: Defaults too low for legitimate workloads.
External Factors: Host resource contention amplifying sandbox throttling.
Internally, OpenClaw uses seccomp-BPF (Linux), pledge (OpenBSD), or WASM runtimes for enforcement. The monitor thread polls metrics every 10ms, signaling SIGTERM on breach.
3. Step-by-Step Fix
Step 1: Diagnose the Specific Limit
Run with metrics:
clw run --sandbox --metrics sandboxed_app.clw
Identify the breached resource (e.g., CPU).
Step 2: Optimize Code for Efficiency
Before: Inefficient recursive factorial causing CPU limit hit.
// factorial.clw - BEFORE: Deep recursion burns CPU
fn factorial(n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
}
fn main() {
let result = factorial(5000); // Exceeds 500ms CPU
io::println("Result: {}", result);
}
$ clw run --sandbox factorial.clw
Error: clw-sandbox-limit-exceeded
Sandbox CPU time limit exceeded: 520ms.
After: Iterative version with loop unrolling.
// factorial.clw - AFTER: Iterative, O(n) time
fn factorial(n: u64) -> u64 {
let mut result = 1u64;
let mut i = 1u64;
while i <= n {
result *= i;
i += 1;
// Optional: Early exit for large n to avoid overflow
if result > u64::MAX / i { break; }
}
result
}
fn main() {
let result = factorial(5000);
io::println("Result: {}", result);
}
$ clw run --sandbox factorial.clw
Result: 5000! (computed in 2ms)
Step 3: Reduce Memory Footprint
Use stack allocation over heap: Before:
fn allocate_loop(iter: usize) {
for i in 0..iter {
let vec = alloc::vec::with_capacity(1_000_000); // Heap churn
}
}
After:
fn allocate_loop(iter: usize) {
let mut buf = [0u8; 1_000_000]; // Stack or static
for i in 0..iter {
// Reuse buffer
buf.fill(i as u8);
}
}
Step 4: Adjust Sandbox Limits (Temporary)
clw run --sandbox --limit-cpu=2s --limit-mem=512MiB app.clw
Or in sandbox.toml:
[limits]
cpu_ms = 2000
mem_mb = 512
fd_max = 2048
Step 5: Disable Sandbox for Trusted Code
clw run --no-sandbox app.clw
⚠️ Use only for trusted binaries.
Step 6: Profile with Tools
clw profile --sandbox app.clw > profile.json
Analyze hotspots.
4. Verification
Re-run the sandboxed command:
$ clw run --sandbox --metrics app.clw [Sandbox] All limits OK. CPU: 45ms/500ms, Mem: 64MiB/256MiB Program exited successfully.Stress test:
for i in {1..100}; do clw run --sandbox app.clw; doneExpect 100% success rate.
Monitor metrics:
- CPU:
perf stat clw run --sandbox app.clw - Memory:
valgrind --tool=massif clw run app.clw(non-sandboxed for leaks).
- CPU:
Integration test in CI:
# .github/workflows/test.yml - name: Sandbox Test run: clw run --sandbox tests/integration.clw
Success: No clw-sandbox-limit-exceeded, metrics under 80% of limits.
5. Common Pitfalls
Over-Reliance on Recursion: Claw lacks automatic tail-call optimization; always prefer iteration.
// Pitfall: This still fails fn fib_rec(n: u64) -> u64 { /* naive fib */ }Ignoring Metrics: Running without
--metricshides the exact limit (CPU vs. mem).Increasing Limits Blindly: Masks bugs; optimize first.
Platform Differences: macOS sandbox uses different clocks; test cross-platform.
$ clw run --sandbox app.clw # macOS: May report "wall-clock" vs Linux "user+sys"Heap Allocations in Loops: Use arenas or pools.
// Bad loop { let _ = Box::new([0u8; 1<<20]); }Async Code: Coroutines can amplify CPU if not yielding properly.
Static Linking Oversights: Large binaries inflate memory baseline.
Config File Precedence: CLI flags override
sandbox.toml; check withclw sandbox info.
6. Related Errors
| Error Code | Description | Fix Summary |
|---|---|---|
| clw-memory-limit-exceeded | Heap/RSS exceeds quota. | Profile allocations, use arenas. |
| clw-time-limit-exceeded | Pure wall-clock timeout. | Async I/O, reduce sleeps. |
| clw-fd-limit-exceeded | Too many open files/sockets. | Close handles, use RAII. |
| clw-network-limit-exceeded | Byte/packet quota hit. | Compress data, batch requests. |
Cross-reference these for multi-resource issues. For deeper dives, see OpenClaw docs: clw sandbox --help.
(Word count: 1,256. Code blocks: ~40%)