Part 4 of 13 · 2 min read
Resource limits really work, and you can watch them stop a runaway process
A limit on CPU time, memory or disk is not a theoretical safeguard -- it's an observable kill switch you can trigger and time.
Think of a circuit breaker in a house. It does not predict which appliance might short out. It simply cuts power the instant any single circuit draws more current than it is rated for. It does this regardless of which appliance caused it.
a resource limit is a cap on CPU time, memory, file size, process count or wall-clock time, and rlimit is the Linux mechanism (setrlimit) that enforces several of these per process. Setting them before running model-written code means a runaway loop, a memory bomb, or an oversized file write gets killed by the operating system itself, not by anything the code was asked to check for.
A worked example. Running while True: pass under a 1-second CPU limit, the process is killed at 1.02 seconds -- almost exactly at the limit, with the small overshoot being measurement overhead rather than the limit failing to fire. Allocating a 2 GB bytearray under a 256 MB address-space limit raises a MemoryError inside the child process. Writing a 50 MB file under a 1 MB file-size limit fails with "File too large." A process sleeping for 60 seconds gets stopped at the 5-second wall-clock timeout -- a separate limit from the CPU one, since a sleeping process uses almost no CPU time at all.
The key lines. From code/mini-sandbox/sandbox.py:
# from code/mini-sandbox/sandbox.py
def run_limited(code, cpu_s=1, mem_mb=256, fsize_mb=1, wall_s=5, env=None):
"""Run Python source in a child with CPU, address-space and file-size limits, a scrubbed environment and a wall-clock timeout."""
import resource
def limits():
resource.setrlimit(resource.RLIMIT_CPU, (cpu_s, cpu_s))
resource.setrlimit(resource.RLIMIT_AS, (mem_mb << 20, mem_mb << 20))
resource.setrlimit(resource.RLIMIT_FSIZE, (fsize_mb << 20, fsize_mb << 20))
Read it as: setrlimit is called inside preexec_fn. That function runs in the child process right after it forks but before the model-written code executes. The limits are in place before a single line of the untrusted code runs. They are set by the kernel itself, not by anything watching the process from outside. That is why they fire reliably even on code that was never designed to cooperate with being limited.
Resource limit, rlimit
A resource limit is a cap on CPU time, memory, file size, process count or wall-clock time. rlimit is the Linux mechanism (setrlimit) that enforces some of these per process.
Example: the infinite loop dying at 1.02 seconds against a 1-second CPU limit is a resource limit measured, not just claimed -- the module doesn't say limits "should" stop a runaway process, it ran one and timed exactly when it stopped.