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
scout/forge:scout/11980-memory-held-not-hit-slow-or-refuse-new-p → main 2 commits fork
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"; | ||
| 26 | 26 | import { notFoundHint, shellCommand } from "./run_command.ts"; |
| 27 | 27 | import { floorRefusal } from "../spawn/disk_floor.ts"; |
| 28 | 28 | import { usedRefusal } from "./disk_guard.ts"; |
| 29 | import { memoryRefusal } from "../spawn/memory_guard.ts"; | |
| 29 | 30 | import { cellInstall, meteredInstall, prefillInstallCache, withLock } from "../checks/install.ts"; |
| 30 | 31 | import { |
| 31 | 32 | commandPid, |
| @@ -349,6 +350,14 @@ export async function runStep( | ||
| 349 | 350 | return err(`${name} ${used}`); |
| 350 | 351 | } |
| 351 | 352 | } |
| 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 | } | |
| 352 | 361 | options.log(`$ ${command}`); |
| 353 | 362 | // A step is background work: the forge's own pages outrank it (#3319). |
| 354 | 363 | const cell: Cell | undefined = |
src/checks/service.ts +22
| @@ -46,6 +46,7 @@ import { | ||
| 46 | 46 | INFRA_SUMMARY, |
| 47 | 47 | CPU_STOP_SUMMARY, |
| 48 | 48 | DISK_USED_SUMMARY, |
| 49 | MEMORY_HELD_SUMMARY, | |
| 49 | 50 | requeueCheck, |
| 50 | 51 | REQUEUED_SUMMARY, |
| 51 | 52 | sweepAbandonedChecks, |
| @@ -102,6 +103,7 @@ import { accountActive } from "../core/accounts.ts"; | ||
| 102 | 103 | import { childEnv, killedStep, runStep } from "../apps/process.ts"; |
| 103 | 104 | import { requeueCpuStop } from "./cpu_stop.ts"; |
| 104 | 105 | import { isUsedRefusal, usedRefusal } from "../apps/disk_guard.ts"; |
| 106 | import { isMemoryRefusal, memoryRefusal } from "../spawn/memory_guard.ts"; | |
| 105 | 107 | import { noteReached } from "../apps/egress_report.ts"; |
| 106 | 108 | import { |
| 107 | 109 | capacityCount, |
| @@ -642,6 +644,16 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 642 | 644 | } |
| 643 | 645 | return undefined; |
| 644 | 646 | } |
| 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 | } | |
| 645 | 657 | let rest = queued; |
| 646 | 658 | const isApproved = approvedOnce(app.db); |
| 647 | 659 | let next = pickNext(rest, Date.now(), isApproved); |
| @@ -917,6 +929,16 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 917 | 929 | return; |
| 918 | 930 | } |
| 919 | 931 | } |
| 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 | } | |
| 920 | 942 | // Stopped for CPU by the cell's watch: the machine's, not the pull's. |
| 921 | 943 | // Queued once more on its own, and no builder hears check_failed for |
| 922 | 944 | // 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 | ||
| 536 | 536 | export const CPU_STOP_SUMMARY = "failed: infrastructure — stopped for CPU by the cell's watch; queued again"; |
| 537 | 537 | |
| 538 | 538 | /** 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"; | |
| 539 | 540 | export const DISK_USED_SUMMARY = "waiting: the forge's disk is 80% used; queued again when there is room"; |
| 540 | 541 | |
| 541 | 542 | /** |
src/crew/dispatch.ts +12
| @@ -46,6 +46,7 @@ import { resolveOpen } from "../core/decisions.ts"; | ||
| 46 | 46 | import { accountActive } from "../core/accounts.ts"; |
| 47 | 47 | import { runningLimitSentence, runningRuns } from "../settings/quotas.ts"; |
| 48 | 48 | import { appendRunLog } from "../runs/log.ts"; |
| 49 | import { memoryRefusal } from "../spawn/memory_guard.ts"; | |
| 49 | 50 | import { noteHeld, type HoldCause } from "../runs/held.ts"; |
| 50 | 51 | import { decideAndRecord } from "./ledger.ts"; |
| 51 | 52 | import { bugsFirst } from "../flow/bugs.ts"; |
| @@ -1702,6 +1703,17 @@ export function pump( | ||
| 1702 | 1703 | } |
| 1703 | 1704 | continue; |
| 1704 | 1705 | } |
| 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 | } | |
| 1705 | 1717 | // Out of slots: nothing more starts. The runs past this point are still |
| 1706 | 1718 | // read, so those a policy above holds are named for vitals, and only the |
| 1707 | 1719 | // rest count as waiting for a slot (#11518). |
src/runs/held.ts +2 −1
| @@ -17,7 +17,7 @@ import type { Database } from "bun:sqlite"; | ||
| 17 | 17 | export const HELD_FRESH_MS = 2 * 60_000; |
| 18 | 18 | |
| 19 | 19 | /** 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"; | |
| 21 | 21 | |
| 22 | 22 | /** How vitals names each cause: "2 by a shut valve". */ |
| 23 | 23 | export const HOLD_WORDS: Readonly<Record<HoldCause, string>> = { |
| @@ -26,6 +26,7 @@ export const HOLD_WORDS: Readonly<Record<HoldCause, string>> = { | ||
| 26 | 26 | limit: "an account limit", |
| 27 | 27 | maintenance: "planned maintenance", |
| 28 | 28 | issue: "its own issue's run still going", |
| 29 | memory: "the forge being near its memory limit", | |
| 29 | 30 | }; |
| 30 | 31 | |
| 31 | 32 | 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 | }); |
new above
offline