Skip to content

The queue can give out more memory than the machine has, and the machine froze #55

Closed

Description

@sourcehawk

Problem

A 28 GB laptop froze while every heavy command of every session ran through the wrapper of bounding-heavy-commands. The wrapper is there to prevent this.

The queue gives out a fixed pool of memory and trusts that the rest of the machine stays inside a fixed reserve. On this laptop the numbers are: total 27703 MiB, reserve max(2048, total / 4) = 6925 MiB, and 10 slots of 2048 MiB = 20480 MiB for bounded commands. Three things in the wrapper let the bounded commands and the rest of the machine together pass the physical memory:

  1. The reserve is fixed, and the load outside the queue is not. The desktop, the browser, the editor, containers, and the agent sessions themselves are not in the queue. On this laptop they use more than 6925 MiB. At the last sar sample before the freeze, 4 GB of swap was already in use. The queue can still give out the whole 20480 MiB.
  2. The wait for free memory ignores reservations that are not used yet. wait_for_memory starts a command when the free memory now is at least the new budget. A running command with a budget of 6 GB that uses 1 GB now leaves 5 GB that looks free, and the next command takes it. When both grow to their budgets, the sum is more than the free memory was.
  3. The wait for free memory ends by starting anyway. After BOUNDED_RUN_MEMORY_WAIT_SECONDS (300 s) the wrapper logs a warning and starts the command in the memory that it just found was not free.

A fourth gap is in the skill, not the script. A test suite that talks to a server outside the queue moves its load to that server. Playwright's reuseExistingServer reuses a dev server that already listens on the port. When an agent started that server without the wrapper, every page that the suite compiles grows a process that no cap or budget covers.

Evidence

From the journal and sar of the boot that ended in the freeze (2026-09-29):

  • 23:50: 13.2 GB available, 13.6 GB used, 4.2 GB swap used, commit 172%.
  • 23:51:32: a full e2e suite started (E2E_BASE_URL=http://localhost:3111 npm run test:e2e with no file argument). A go test -p "$BOUNDED_RUN_CPUS" and a go run from another session were also running.
  • 23:51:39 and 23:52:43: two more wrappers probed the cap and then waited. They never started, so the queue did hold them back.
  • 23:58 to 00:02: gnome-shell input lag ("your system is too slow"), then journald and resolved "Under memory pressure" every few seconds, with no OOM kill. The power key was pressed at 00:01.
  • The oom-kill lines earlier that day are all CONSTRAINT_MEMCG inside bounded-run-*.scope units: the cap killed commands that passed their budget, as designed. The freeze was machine-wide pressure, not a cap.

sar samples every 10 minutes, so the evidence cannot say which process grew between 23:51 and 23:58. Gaps 1 to 3 are in the code in any case. Gap 4 is the most likely source of the growth, but it is not proven.

Approach

In the wrapper, with no new state file (the kernel stays the source of truth):

  • Count the reserved but unused memory. On Linux with the cap, each running bounded command is a bounded-run-*.scope cgroup with memory.max and memory.current. Before starting, sum memory.max - memory.current over those scopes, and start only when the free memory minus that sum minus a headroom is at least the budget. Where there is no cap, keep the current check and say so.
  • Keep a headroom that follows the machine, for the desktop and the agent sessions, instead of trusting a reserve fixed at the start. For example, never start a command that would leave less than a set part of the memory free.
  • Do not start when the memory wait times out. Keep waiting, and log at intervals that the machine is full, as the slot wait does. A timeout that starts the command turns a slow queue into a freeze.

In the skill:

  • A server that a heavy command uses is heavy too. A dev server, a database, or a container that a test suite talks to runs through the wrapper, or the test runner starts it inside the same bounded command. Do not let the suite reuse a server that an agent started plain.

Acceptance criteria

  • A test: a bounded command holds a large budget and uses little of it. A second command whose budget fits in the free memory now, but not in the free memory minus the unused budget, waits.
  • A test: when the memory wait passes its limit, the command still does not start while the memory is not free.
  • The lock files stay empty, and the wrapper adds no state file.
  • The skill tells agents to start a server that a heavy command uses through the wrapper, and a Red Flags row covers "the dev server is already running, the tests can reuse it".
  • python3 -m unittest discover -s skills/bounding-heavy-commands/scripts/tests passes on Linux and macOS.

Context

Related: #54 (order among waiters). Waiting instead of starting on a timeout makes the progress guarantee of #54 more important.

Activity

  1. sourcehawk commented on Sep 30, 2026

    @sourcehawk
    OwnerAuthor

    Fixed by #58 (merged in e897794). The closing keyword did not fire on merge, so this is closed by hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions