# Fork bombs, full disks and OOM kills: handling resource exhaustion in agent sandboxes

> Model-written code will eventually loop forever, fill the disk, fork without limit or print gigabytes. How each failure shows up, which limits contain it, and what to tell the model so it recovers instead of thrashing.

Model-written code will eventually loop forever, fill the disk, fork without limit or print gigabytes. How each failure shows up, which limits contain it, and what to tell the model so it recovers instead of thrashing.

November 26, 2026 · 4 min read · The Sandscale team

Give a model a shell for long enough and it will write `while True:` without a `break`, load a 4 GB CSV into a 2 GB box, or spawn a process per file in `node_modules`. None of this needs an attacker. The sandbox’s job is to keep the damage inside the box. Your job is to make sure the agent learns what happened and does something sensible next.

This post goes through the four common failures, the limits that contain each, and how to report each to the model.

## 1. Runaway time: loops and hangs

**Looks like:** a command that never returns. Often a loop, a network call with no timeout, or a process waiting for input that never comes (`npm init` without `-y`, a `git` command opening an editor).

**Contain it with:** a per-command timeout and a sandbox lifetime. The command timeout should be per command type; the lifetime is the backstop if your orchestrator dies. Our post on [three timeouts](https://sandscale.dev/blog/timeouts-for-agent-sandboxes) covers both. Also close stdin for commands that should not be interactive, and set `CI=true` and `GIT_TERMINAL_PROMPT=0` so tools do not wait for a person.

**Tell the model:** “The command was stopped after 120 seconds without finishing. The last output was: …”. With the tail of the output, the model can usually see where it hung.

## 2. Memory: the OOM kill

**Looks like:** a process that dies with `Killed`, exit code 137, or a stack trace from a language runtime that could not allocate.

**Contain it with:** the box’s memory limit. In a VM, the guest kernel’s OOM killer picks a process inside the guest; the host and other runs are unaffected. Inside the box, you can make it fail earlier and more clearly with a lower limit on the agent’s command than on the whole box, for example `ulimit -v` or `prlimit --as` for a single process, or a cgroup memory limit if the template sets one up.

**Tell the model:** be explicit. “The process was killed because it ran out of memory (the box has 4 GiB).” A model told that will often stream the file, use chunks or drop columns. A model shown only `Killed` tends to rewrite unrelated code.

## 3. Process count: the fork bomb

**Looks like:** the box becoming unresponsive, commands failing with `fork: Resource temporarily unavailable`, or a timeout on every subsequent command.

**Contain it with:** a process limit. `ulimit -u` caps processes for a user; a cgroup `pids.max` caps them for a group. Set it in the template or wrap the agent’s commands:

```sh
prlimit --nproc=256 --as=3000000000 -- python3 main.py
```

Fork bombs are rarer from models than loops, but test suites with unbounded worker pools and recursive scripts that call themselves produce the same effect. If a box has stopped responding after one, stop it and start a fresh one rather than trying to clean up.

**Tell the model:** “The command created too many processes and the sandbox stopped responding. It has been replaced with a fresh sandbox; files from before were not kept.” Then make sure that is true.

## 4. Disk and output

**Disk.** A log written in a loop, a dataset downloaded twice, a build cache that grows. **Looks like:** `No space left on device`, which then breaks unrelated commands. **Contain it with:** the box’s disk size, and cleaning temporary directories between steps. Check `df -h /workspace` before large downloads.

**Output.** A command that prints a million lines does not hurt the box, but it hurts everything downstream: your memory, your logs and the model’s context window. **Contain it with:** a byte cap on what you keep and send to the model, with the head and tail kept and the middle cut. See [truncating tool output for LLMs](https://sandscale.dev/blog/truncating-tool-output-for-llms).

## Keep the agent from thrashing

The worst outcome is not one failure; it is the agent retrying the same failing command twenty times. Two rules help:

- **Report the cause, not just the output.** Distinguish timeout, memory, process limit, disk and normal non-zero exit in the tool result. The model can only adapt to what it is told.
- **Count retries.** After the same class of failure two or three times in a row, stop and escalate to a person, or switch to a larger box for that workload if the failure is memory and the data is legitimate. See [spend caps for AI agents](https://sandscale.dev/blog/spend-caps-for-ai-agents).

## What Sandscale reports

On Sandscale, `sandboxes.run()` returns a result rather than throwing when a command ends badly:

```ts
const result = await sandscale.sandboxes.run(box.id, ['python3', 'main.py'], {
  region: box.region,
  timeoutSeconds: 120,
});

const cause = result.timedOut ? 'timeout'
  : result.oomKilled ? 'out_of_memory'
  : result.exitCode !== 0 ? 'error'
  : 'ok';
```

`timedOut` and `oomKilled` are set by the server, so you do not have to guess from exit codes. Each run is its own Firecracker microVM with its own kernel, and every box gets exactly the vCPUs and memory you picked (plus any burst you set), never more, so one run’s exhaustion stays in its box. Set `timeoutSeconds` on create as the lifetime backstop. Process and per-command memory limits inside the box are yours to set in the template or the command, as above.

**On this page**

1. 1. Runaway time: loops and hangs
2. 2. Memory: the OOM kill
3. 3. Process count: the fork bomb
4. 4. Disk and output
5. Keep the agent from thrashing
6. What Sandscale reports

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [November 26, 2026 · **30 questions to ask any agent sandbox provider before you sign up** · A vendor-neutral checklist for choosing where your agents run code: isolation, network, secrets, lifecycle, billing for waiting time, governance, data location, reliability and leaving. With where to find each answer for Sandscale.](https://sandscale.dev/blog/questions-to-ask-an-agent-sandbox-provider)
- [November 26, 2026 · **Agent sandbox glossary: 41 terms in plain English** · Plain definitions for the words that come up when you run AI agent code in isolation: microVM, VMM, KVM, jailer, gVisor, seccomp, namespaces, egress allowlist, lease, snapshot, warm pool, awake vCPU-hour and more.](https://sandscale.dev/blog/agent-sandbox-glossary)

---
Source: https://sandscale.dev/blog/resource-exhaustion-in-agent-sandboxes
