Public: anyone can read this; the crew can change it.
A child forge's Bun processes start with a smaller heap, with the resident memory before and after in the pull #15273
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15256-a-child-forge-s-bun-processes-start-with → main 2 commits fork
4 files
changed +72
−2
docs/features/memory-held.md +3 −1
| @@ -11,4 +11,6 @@ A child forge in a cell has one memory limit for itself, its crew run, its check | ||
| 11 | 11 | |
| 12 | 12 | With nothing running a start is always allowed, so the hold never lasts. A forge that is not a cell child reads no pressure and is never held. The rule is `src/runs/memory_hold.ts`; the cgroup reading is reused for a second; tests in `test/memory-hold.test.ts` drive the dispatcher, the check queue and vitals. |
| 13 | 13 | |
| 14 | Not done here: a smaller heap for the child's Bun processes, reclaiming idle app cells, stopping the largest process instead of the forge. | |
| 14 | A child forge's own Bun process starts with `--smol`, Bun's smaller-heap mode (#15256): the command is built in `src/settings/child_heap.ts` and nowhere else, and `FORGE_CHILD_SMOL=off` on the parent turns it off. Measured on one idle forge (Bun 1.4.4, empty data, 90 s after boot): 282 MiB resident without, 262 MiB with; after 60 page loads 305 MiB against 289 MiB; `GET /` median 3.4 ms against 4.1 ms. Test: `test/child-heap-smol.test.ts`. | |
| 15 | ||
| 16 | Not done here: a smaller heap for the processes a child starts in turn (its checks, crew runs and apps), reclaiming idle app cells, stopping the largest process instead of the forge. |
src/settings/child_heap.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | /** | |
| 2 | * The child forge's heap, one switch (#15256): a child forge in a cell starts its Bun process with | |
| 3 | * `--smol`, Bun's documented smaller-heap mode (it collects garbage more often, so the process | |
| 4 | * holds less resident memory at some CPU cost). A forge grew to 4.7 GiB in #15210; a cell's memory | |
| 5 | * is shared by the child, its crew runs, its checks and its apps (`docs/features/memory-held.md`). | |
| 6 | * | |
| 7 | * `FORGE_CHILD_SMOL=off` on the parent turns it off; anything else, or nothing, is on. The command | |
| 8 | * the cell runs is built here and nowhere else (`spawn/cell.ts`). | |
| 9 | */ | |
| 10 | ||
| 11 | export const CHILD_SMOL_ENV = "FORGE_CHILD_SMOL"; | |
| 12 | ||
| 13 | /** Whether children start with the smaller heap: on unless the parent says `off`. */ | |
| 14 | export const childSmol = (env: Record<string, string | undefined> = process.env): boolean => env[CHILD_SMOL_ENV] !== "off"; | |
| 15 | ||
| 16 | /** | |
| 17 | * The arguments a child forge's Bun process starts with: the runtime, `--smol` when on, its entry. | |
| 18 | */ | |
| 19 | export function childArgs(execPath: string, main: string, env: Record<string, string | undefined> = process.env): string[] { | |
| 20 | return childSmol(env) ? [execPath, "--smol", main] : [execPath, main]; | |
| 21 | } | |
| 22 | ||
| 23 | /** The shell line the cell runs: `exec` of `childArgs`, each one quoted. */ | |
| 24 | export const childCommand = (execPath: string, main: string, env: Record<string, string | undefined> = process.env): string => | |
| 25 | `exec ${childArgs(execPath, main, env).map((arg) => JSON.stringify(arg)).join(" ")}`; |
src/spawn/cell.ts +3 −1
| @@ -16,6 +16,7 @@ import type { App } from "../app.ts"; | ||
| 16 | 16 | import { connectUpstream, offeredProtocols, type AppSocket } from "../apps/proxy.ts"; |
| 17 | 17 | import { cellsAvailable, commandPid, killTree, openCell, type Cell, type CellSpec, type Limits } from "../apps/contain.ts"; |
| 18 | 18 | import { childEnv, freePort, lineCutter } from "../apps/process.ts"; |
| 19 | import { childCommand } from "../settings/child_heap.ts"; | |
| 19 | 20 | import { childDirOf } from "../actions/spawn.ts"; |
| 20 | 21 | import { openBeatDoor, parentLocal, type BeatDoor } from "./beat-door.ts"; |
| 21 | 22 | import { hostedPlacement, parentOrigin, childPublicUrl, type BodyStore, type Placement } from "./placement.ts"; |
| @@ -540,7 +541,8 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren { | ||
| 540 | 541 | release(); |
| 541 | 542 | throw error; |
| 542 | 543 | } |
| 543 | const command = `exec ${JSON.stringify(process.execPath)} ${JSON.stringify(source.main)}`; | |
| 544 | // `--smol` unless the parent turns it off (`settings/child_heap.ts`, #15256). | |
| 545 | const command = childCommand(process.execPath, source.main); | |
| 544 | 546 | const { proc, unfollow } = spawnInCell(cell, command, { |
| 545 | 547 | cwd: dirname(source.main), |
| 546 | 548 | env: childEnvFor({ port, dataDir: dir, url, parentPort, size: input.size, ...(cell.cgroupDir === undefined ? {} : { cgroupDir: cell.cgroupDir }) }), |
test/child-heap-smol.test.ts added +41
| @@ -0,0 +1,41 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { readFileSync, writeFileSync, mkdtempSync } from "node:fs"; | |
| 3 | import { join } from "node:path"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { CHILD_SMOL_ENV, childArgs, childCommand, childSmol } from "../src/settings/child_heap.ts"; | |
| 6 | ||
| 7 | // #15256: a child forge's Bun process starts with Bun's smaller heap (`--smol`), one switch off. | |
| 8 | describe("child forge heap", () => { | |
| 9 | test("children start with --smol by default", () => { | |
| 10 | expect(childSmol({})).toBe(true); | |
| 11 | expect(childArgs("/bin/bun", "/code/src/main.ts", {})).toEqual(["/bin/bun", "--smol", "/code/src/main.ts"]); | |
| 12 | }); | |
| 13 | ||
| 14 | test("FORGE_CHILD_SMOL=off turns it off; any other value leaves it on", () => { | |
| 15 | expect(childArgs("/bin/bun", "/m.ts", { [CHILD_SMOL_ENV]: "off" })).toEqual(["/bin/bun", "/m.ts"]); | |
| 16 | expect(childSmol({ [CHILD_SMOL_ENV]: "on" })).toBe(true); | |
| 17 | expect(childSmol({ [CHILD_SMOL_ENV]: "0" })).toBe(true); | |
| 18 | }); | |
| 19 | ||
| 20 | test("the shell line quotes each argument", () => { | |
| 21 | expect(childCommand("/b un", "/a\"b.ts", {})).toBe('exec "/b un" "--smol" "/a\\"b.ts"'); | |
| 22 | }); | |
| 23 | ||
| 24 | test("the process the shell line starts runs in smol mode, and not when off", async () => { | |
| 25 | const dir = mkdtempSync(join(tmpdir(), "smol-")); | |
| 26 | const main = join(dir, "main.ts"); | |
| 27 | writeFileSync(main, "console.log(JSON.stringify(process.execArgv));"); | |
| 28 | const run = async (env: Record<string, string>) => { | |
| 29 | const proc = Bun.spawn(["sh", "-c", childCommand(process.execPath, main, env)], { stdout: "pipe" }); | |
| 30 | return JSON.parse((await new Response(proc.stdout).text()).trim()) as string[]; | |
| 31 | }; | |
| 32 | expect(await run({})).toContain("--smol"); | |
| 33 | expect(await run({ [CHILD_SMOL_ENV]: "off" })).not.toContain("--smol"); | |
| 34 | }); | |
| 35 | ||
| 36 | test("the cell start builds its command here, nowhere else", () => { | |
| 37 | const cell = readFileSync(new URL("../src/spawn/cell.ts", import.meta.url), "utf8"); | |
| 38 | expect(cell).toContain("childCommand(process.execPath, source.main)"); | |
| 39 | expect(cell).not.toContain("`exec ${JSON.stringify(process.execPath)}"); | |
| 40 | }); | |
| 41 | }); |
new above
offline