Diagnostics

Sandbox slowness handoff (issue.md). Fetch on the host with:
curl -fsSLO https://diagnostics.theo.infinitymachine.app/issue.md

Download issue.md
# Handoff: per-user sandbox is very slow (2026-11-25)

For: an agent with root access on the host running the per-user gVisor containers.
From: Claude Code session running *inside* the sandbox as user `lab` (no host visibility).

## What the user asked

> "you are running inside a virtual machine (docker + gvis or something like that). it's painfully slow it feels.
> the tetris app took 19 minutes to make. I'd expect it from you in 2-3 mins. Also launching claude took 20 sec
> and even typing is slow. There's a .5 sec delay between typing and the letter appearing. Can you please
> diagnose why? I think we had strict CPU/RAM limits earlier but we lifted them on the virtual containers for
> each user."

Then: "paste your output tests and diagnostics along with what I asked you to do etc into a file … I will ask
an agent with root access to continue where you left off outside of a container."

Task for you: confirm the root cause on the host and fix it, so that the sandbox no longer has periodic
~100 ms freezes. Get the user's confirmation before taking disruptive actions like recreating containers.

## Context: what was being done when the slowness was noticed

- A static PWA game "Stackfall" was built in `/srv/lab/stackfall/app`, published to
  https://stackfall.theo.infinitymachine.app, and `labctl verify stackfall` passed.
- `labcheck stackfall` (10 Playwright/Chromium tests) took **~170–175 s** per run; one-off scripts and process
  launches were very slow. Total wall time was ~19 min for work that should take 2–3 min.

## Sandbox identity

- Hostname / container short ID: `5f4815b86cf1`
- Full container ID (from /proc/self/cgroup):
  `5f4815b86cf171232b7cd7627b635de6a619b4fc62e16a68357d8cf34ee55aea`
- Kernel string: `Linux 5f4815b86cf1 4.19.0-gvisor` (gVisor / runsc)
- Sandbox uptime at time of test: **14:02** (started ~14 h before 01:54 local). If the CPU/RAM limits were lifted
  after this container started, it may still be running under the old ones.

## Raw diagnostics (inside the sandbox)

### Resources / limits as seen from inside

```
nproc: 8
model name: unknown        (cpuinfo flags include "hypervisor": host is itself a VM)
/dev/kvm: not present
cgroup v1 (gVisor internal view):
  cpu.cfs_quota_us  = 800000
  cpu.cfs_period_us = 100000     -> 8 CPUs
  memory.limit_in_bytes = 21474836480   -> 20 GiB
free -m: total 20480, used 949, free 19530, swap 0
loadavg: 0.00 0.00 0.00 1/45
TracerPid: 0, Seccomp: 0
```

Mounts:
```
none / 9p ro,...,cache=fscache,...,directfs
none /tmp tmpfs rw,nosuid,noexec,size=262144k
none /srv/lab 9p rw,...,cache=remote_revalidating,...,directfs
none /srv/home 9p rw,...,cache=remote_revalidating,...,directfs
```

### Wall-clock benchmarks

```
CPU: python 20M-iteration loop                3.288 s   (fine, roughly 1.5-2x native)
spawn /bin/true x200                         31.484 s   (~157 ms each; normal ~1-3 ms under gVisor)
python3 startup x10                           3.180 s
node startup x5                               4.204 s
200k stat() syscalls                         26.501 s   (~132 us avg)
2000 small files /srv/lab (9p)               18.429 s
2000 small files /tmp (tmpfs)                18.234 s   (same as 9p -> NOT a filesystem problem)
500MB write+fsync /srv/lab                    8.692 s
stat 5000 files in .venv (walk)               3.049 s
claude --version                              3.317 s
HTTPS GET api.anthropic.com                   3.003 s
```

### Per-syscall averages

```
python no-op call:       0.56 us
getpid() raw syscall:  129.6 us
stat('/'):             251.1 us
read 1B /dev/zero:     215.1 us
fork+wait:              70.2 ms
```

### Latency distribution and CPU stall probe (the key result)

```
getpid latency us: min 4.7  p50 7.1  p90 345.8  p99 1982.4  max 128743
stalls >5ms: 55 (out of 20000 calls)
pure-CPU 2s loop (no syscalls): 24 pauses >2ms, total 324 ms, max 101 ms
```

Probe used (busy-loop reading the clock and recording gaps > 2 ms):
```python
import time; pc=time.perf_counter_ns
gaps=[]; t0=pc(); prev=t0
while pc()-t0<2e9:
    n=pc()
    if n-prev>2e6: gaps.append((n-prev)/1e6)
    prev=n
print(len(gaps), sum(gaps), max(gaps or [0]))
```

## Interpretation

- The best-case syscall cost (4.7 µs min, 7 µs median) is normal for gVisor (systrap-like). The **platform is
  not the main problem**.
- The **tail** is the problem: p90 is 50× the median, and there are stalls up to 129 ms.
- **A loop that makes no syscalls is also frozen**: 16% of wall time, with pauses up to **~101 ms**. Something
  outside the sandbox is pausing the whole sandbox.
- Pauses of about 100 ms match the default CFS period (100 ms). This is the usual sign of **host-side
  cgroup CPU quota throttling** of the runsc sandbox. The Sentry, gofer and Go GC threads use up the quota,
  then everything stops until the next period.
- Alternative or contributing cause: **host VM CPU oversubscription or steal** (many busy sandboxes per vCPU,
  or a noisy hypervisor neighbour).
- The internal cgroup (8 CPUs) is gVisor's own view and does **not** prove the host-side cgroup quota was lifted.
- The symptoms fit: Claude Code (Node/Ink) re-renders on every keystroke, so ~100 ms freezes turn into the
  ~0.5 s typing lag and the ~20 s startup.

## Next steps on the host (not yet done)

```sh
# 1. Docker's configured limits for this container
docker inspect 5f4815b86cf1 --format 'nano={{.HostConfig.NanoCpus}} quota={{.HostConfig.CpuQuota}} period={{.HostConfig.CpuPeriod}} cpuset={{.HostConfig.CpusetCpus}} shares={{.HostConfig.CpuShares}} mem={{.HostConfig.Memory}} runtime={{.HostConfig.Runtime}}'

# 2. Actual throttling. Run twice a few seconds apart; if nr_throttled/throttled_usec keep growing, that confirms it
cat /sys/fs/cgroup/system.slice/docker-5f4815b86cf1*.scope/cpu.stat 2>/dev/null \
  || cat /sys/fs/cgroup/cpu,cpuacct/docker/5f4815b86cf1*/cpu.stat
#    also find any parent slice limits (e.g. a per-user or per-tenant slice):
systemd-cgls --no-pager | head -50; find /sys/fs/cgroup -name cpu.max -exec grep -H -v '^max' {} \; 2>/dev/null

# 3. Host VM contention: watch st (steal) and r (run queue) against the vCPU count
nproc; vmstat 1 5; top -b -n1 | head -30

# 4. gVisor runtime config (platform should be systrap; ptrace would be much slower)
docker info --format '{{json .Runtimes}}'; cat /etc/docker/daemon.json
```

### Likely fixes
- **If throttling is confirmed:** `docker update --cpus=0 <id>` (or raise the quota) on this and the other
  per-user containers. Check for quotas on parent slices too. If the change does not take effect under runsc,
  recreate the container. Recreating also covers the case where the container predates the limit change.
- **If steal or run-queue is high:** the host is oversubscribed. Add vCPUs or reduce the number of active
  sandboxes per host. KVM platform is not an option (no nested virt / `/dev/kvm`), so keep systrap.
- **Re-test afterwards** from inside the sandbox with the stall probe above. Target: 0 pauses > 2 ms in a 2 s loop,
  `getpid` p99 < 50 µs, `/bin/true` x200 < 2 s.

Keep Diagnostics handy