Public: anyone can read this; the crew can change it.

Memory held, not hit: slow or refuse new processes in a Small child near its limit before any stop #11982

Parked opened this
Closed without merging

scout/forge:scout/11980-memory-held-not-hit-slow-or-refuse-new-p → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +153 −1
docs/features/memory-held.md added +8
@@ -0,0 +1,8 @@
1 # Memory held, not hit (#11980)
2
3 A child forge in a cell is stopped by its host at its memory limit, the whole forge with it. At 70% of the limit
4 (`MEMORY_HOLD`, `src/spawn/memory_guard.ts`, read from the forge's own cgroup) the child starts nothing new:
5 no check step (`runStep`, `checks/service.ts` pick) and no crew run (`crew/dispatch.ts`, held as `memory`).
6 Work stays queued with one log line and starts when memory frees; a check is queued again, never red.
7 Not done here: smaller heap for Bun processes, test shards one at a time, reclaiming idle app cells, and a stop that
8 takes the largest process only.
src/apps/process.ts +9
@@ -26,6 +26,7 @@ import { toolchainUse, type ToolchainUse } from "./toolchain.ts";
2626 import { notFoundHint, shellCommand } from "./run_command.ts";
2727 import { floorRefusal } from "../spawn/disk_floor.ts";
2828 import { usedRefusal } from "./disk_guard.ts";
29 import { memoryRefusal } from "../spawn/memory_guard.ts";
2930 import { cellInstall, meteredInstall, prefillInstallCache, withLock } from "../checks/install.ts";
3031 import {
3132 commandPid,
@@ -349,6 +350,14 @@ export async function runStep(
349350 return err(`${name} ${used}`);
350351 }
351352 }
353 // Near the forge's own memory limit none starts (#11980): the check is queued again, not failed.
354 if (options.cell !== undefined) {
355 const tight = memoryRefusal(options.cwd);
356 if (tight !== undefined) {
357 options.log(`${name} ${tight}`);
358 return err(`${name} ${tight}`);
359 }
360 }
352361 options.log(`$ ${command}`);
353362 // A step is background work: the forge's own pages outrank it (#3319).
354363 const cell: Cell | undefined =
src/checks/service.ts +22
@@ -46,6 +46,7 @@ import {
4646 INFRA_SUMMARY,
4747 CPU_STOP_SUMMARY,
4848 DISK_USED_SUMMARY,
49 MEMORY_HELD_SUMMARY,
4950 requeueCheck,
5051 REQUEUED_SUMMARY,
5152 sweepAbandonedChecks,
@@ -102,6 +103,7 @@ import { accountActive } from "../core/accounts.ts";
102103 import { childEnv, killedStep, runStep } from "../apps/process.ts";
103104 import { requeueCpuStop } from "./cpu_stop.ts";
104105 import { isUsedRefusal, usedRefusal } from "../apps/disk_guard.ts";
106 import { isMemoryRefusal, memoryRefusal } from "../spawn/memory_guard.ts";
105107 import { noteReached } from "../apps/egress_report.ts";
106108 import {
107109 capacityCount,
@@ -642,6 +644,16 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
642644 }
643645 return undefined;
644646 }
647 // Near the memory limit none starts (#11980): they stay queued, and the tick asks again.
648 const tight = memoryRefusal(app.config.dataDir);
649 if (tight !== undefined) {
650 for (const run of queued)
651 if (!diskWaiting.has(run.id)) {
652 diskWaiting.add(run.id);
653 appendRunLog(app.config.dataDir, run.id, `queued, ${tight}`);
654 }
655 return undefined;
656 }
645657 let rest = queued;
646658 const isApproved = approvedOnce(app.db);
647659 let next = pickNext(rest, Date.now(), isApproved);
@@ -917,6 +929,16 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
917929 return;
918930 }
919931 }
932 // The forge was near its memory limit when the step would have started (#11980): held,
933 // not hit.
934 if (!outcome.ok && isMemoryRefusal(outcome.error)) {
935 const moved = requeueCheck(app.db, run.id, undefined, MEMORY_HELD_SUMMARY);
936 if (moved) {
937 write(MEMORY_HELD_SUMMARY);
938 announceRun(app, moved.again, run.actorId ?? undefined);
939 return;
940 }
941 }
920942 // Stopped for CPU by the cell's watch: the machine's, not the pull's.
921943 // Queued once more on its own, and no builder hears check_failed for
922944 // it; a second stop in a row is reported as any red is (#11617).
src/core/runs.ts +1
@@ -536,6 +536,7 @@ export const INFRA_SUMMARY = "failed: infrastructure — the install failed twic
536536 export const CPU_STOP_SUMMARY = "failed: infrastructure — stopped for CPU by the cell's watch; queued again";
537537
538538 /** What a check says as it is queued again because the disk was 80% used (#11799): the machine's wait, not the pull's. */
539 export const MEMORY_HELD_SUMMARY = "waiting: this forge is near its memory limit; queued again when memory frees";
539540 export const DISK_USED_SUMMARY = "waiting: the forge's disk is 80% used; queued again when there is room";
540541
541542 /**
src/crew/dispatch.ts +12
@@ -46,6 +46,7 @@ import { resolveOpen } from "../core/decisions.ts";
4646 import { accountActive } from "../core/accounts.ts";
4747 import { runningLimitSentence, runningRuns } from "../settings/quotas.ts";
4848 import { appendRunLog } from "../runs/log.ts";
49 import { memoryRefusal } from "../spawn/memory_guard.ts";
4950 import { noteHeld, type HoldCause } from "../runs/held.ts";
5051 import { decideAndRecord } from "./ledger.ts";
5152 import { bugsFirst } from "../flow/bugs.ts";
@@ -1702,6 +1703,17 @@ export function pump(
17021703 }
17031704 continue;
17041705 }
1706 // A forge near its memory limit starts no new run (#11980): queued, not failed, until memory
1707 // frees.
1708 const tight = memoryRefusal(app.config.dataDir);
1709 if (tight !== undefined) {
1710 held.set(fresh.id, "memory");
1711 if (!noted.has(fresh.id)) {
1712 noted.add(fresh.id);
1713 appendRunLog(app.config.dataDir, fresh.id, `queued, ${tight}`);
1714 }
1715 continue;
1716 }
17051717 // Out of slots: nothing more starts. The runs past this point are still
17061718 // read, so those a policy above holds are named for vitals, and only the
17071719 // rest count as waiting for a slot (#11518).
src/runs/held.ts +2 −1
@@ -17,7 +17,7 @@ import type { Database } from "bun:sqlite";
1717 export const HELD_FRESH_MS = 2 * 60_000;
1818
1919 /** Why the dispatcher held a run. */
20 export type HoldCause = "valve" | "pace" | "limit" | "maintenance" | "issue";
20 export type HoldCause = "valve" | "pace" | "limit" | "maintenance" | "issue" | "memory";
2121
2222 /** How vitals names each cause: "2 by a shut valve". */
2323 export const HOLD_WORDS: Readonly<Record<HoldCause, string>> = {
@@ -26,6 +26,7 @@ export const HOLD_WORDS: Readonly<Record<HoldCause, string>> = {
2626 limit: "an account limit",
2727 maintenance: "planned maintenance",
2828 issue: "its own issue's run still going",
29 memory: "the forge being near its memory limit",
2930 };
3031
3132 interface Pass {
src/spawn/memory_guard.ts added +41
@@ -0,0 +1,41 @@
1 /**
2 * Memory is held, not hit (#11980). A child forge in a cell is stopped by its
3 * host at its memory limit, the whole forge with it. Before that, at
4 * `MEMORY_HOLD` of the limit this forge starts nothing new: no check step, no
5 * crew run. They stay queued (never red, never failed) and start when what is
6 * running ends and gives memory back. It reads the forge's own cgroup
7 * (`usage.ts` `selfUse`); a forge that is no cell child has no size and is
8 * never refused.
9 */
10
11 import { realSelf, selfUse, type ForgeUse } from "./usage.ts";
12
13 /** At this share of its memory limit a child starts nothing new. */
14 export const MEMORY_HOLD = 0.7;
15
16 /** The words every refusal starts with, so a caller can tell it from a step's own failure. */
17 export const MEMORY_REFUSAL = "not started: this forge is using";
18
19 /** One sentence when `use` is at or over `MEMORY_HOLD` of its limit, else undefined. */
20 export function memoryRefusalOf(use: ForgeUse | null): string | undefined {
21 if (use === null || use.kind !== "cell" || !(use.limits.memoryMiB > 0)) return undefined;
22 if (use.memoryMiB < use.limits.memoryMiB * MEMORY_HOLD) return undefined;
23 return `${MEMORY_REFUSAL} ${use.memoryMiB} MiB of its ${use.limits.memoryMiB} MiB, over the ${MEMORY_HOLD * 100}% it holds new work to: queued again when memory frees`;
24 }
25
26 /** The forge's own reading; a test seam, the real one reads the cgroup. */
27 export const memoryReading: { read: (dataDir: string) => ForgeUse | null } = {
28 read: (dataDir) => selfUse(realSelf(dataDir))?.use ?? null,
29 };
30
31 /** The refusal for this forge now, or undefined when it has room (or is no child). */
32 export function memoryRefusal(dataDir: string): string | undefined {
33 try {
34 return memoryRefusalOf(memoryReading.read(dataDir));
35 } catch {
36 return undefined;
37 }
38 }
39
40 /** Whether a step's error is this guard's refusal. */
41 export const isMemoryRefusal = (error: string): boolean => error.includes(MEMORY_REFUSAL);
test/memory-held.test.ts added +58
@@ -0,0 +1,58 @@
1 /** #11980: a child near its memory limit starts nothing new; the work stays queued. */
2
3 import { afterEach, describe, expect, test } from "bun:test";
4 import { mkdtempSync } from "node:fs";
5 import { tmpdir } from "node:os";
6 import { join } from "node:path";
7 import type { CellSpec } from "../src/apps/contain.ts";
8 import { childEnv, runStep } from "../src/apps/process.ts";
9 import { HOLD_WORDS } from "../src/runs/held.ts";
10 import { isMemoryRefusal, memoryReading, memoryRefusal, memoryRefusalOf } from "../src/spawn/memory_guard.ts";
11 import { SIZES } from "../src/spawn/sizes.ts";
12 import type { ForgeUse } from "../src/spawn/usage.ts";
13
14 const real = memoryReading.read;
15 afterEach(() => {
16 memoryReading.read = real;
17 });
18 const dir = mkdtempSync(join(tmpdir(), "memory-held-"));
19 const limits = SIZES.small;
20 const using = (memoryMiB: number): ForgeUse => ({ kind: "cell", limits, memoryMiB, cores: null, diskMiB: null });
21
22 describe("memoryRefusalOf", () => {
23 test("holds at 70% of the limit and not below; shared, idle and unknown are never refused", () => {
24 const at = Math.ceil(limits.memoryMiB * 0.7);
25 expect(memoryRefusalOf(using(at - 1))).toBeUndefined();
26 const line = memoryRefusalOf(using(at))!;
27 expect(line).toContain(`${at} MiB of its ${limits.memoryMiB} MiB`);
28 expect(isMemoryRefusal(line)).toBe(true);
29 expect(memoryRefusalOf({ kind: "shared" })).toBeUndefined();
30 expect(memoryRefusalOf({ kind: "idle", limits })).toBeUndefined();
31 expect(memoryRefusalOf(null)).toBeUndefined();
32 });
33 test("a reading that throws refuses nothing", () => {
34 memoryReading.read = () => {
35 throw new Error("no cgroup");
36 };
37 expect(memoryRefusal(dir)).toBeUndefined();
38 });
39 test("the dispatcher's hold is named for vitals", () => {
40 expect(HOLD_WORDS.memory).toContain("memory");
41 });
42 });
43
44 describe("a step near the limit", () => {
45 const step = (lines: string[], cell?: CellSpec) =>
46 runStep("check", "true", { cwd: dir, env: childEnv({}), log: (l) => lines.push(l), signal: new AbortController().signal, cell });
47
48 test("is not started, says why and fails with the refusal; with room it runs", async () => {
49 memoryReading.read = () => using(limits.memoryMiB - 100);
50 const lines: string[] = [];
51 const refused = await step(lines, {} as CellSpec);
52 expect(refused.ok).toBe(false);
53 if (!refused.ok) expect(isMemoryRefusal(refused.error)).toBe(true);
54 expect(lines).toHaveLength(1);
55 memoryReading.read = () => using(100);
56 expect((await step([])).ok).toBe(true);
57 });
58 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs