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

Sentinel: memory-growth was degraded 32 minutes today: The forge's own memory is 4479 MiB, over twice its first-hour 1909 MiB and past the 2176 MiB more its database caches m… #17251

opened this

Merged 44h 57m ago, live in production

mason/forge:mason/17205-sentinel-memory-growth-was-degraded-32-m → main 0 commits fork 1 thread · 1 unresolved

  • Merged. mason/17205-sentinel-memory-growth-was-degraded-32-m was merged into main as 87074e5.

Review

Sign in to review this change.
4 files changed +61 −6
docs/features/memory-growth.md +2
@@ -29,6 +29,8 @@ The sentinel's memory-growth issue quotes the watch's evidence: each VM's heap,
2929
3030 **The jobs open at the trip (#17083).** Memory outside every heap that climbs for an hour and falls back (production on 2026-12-01: 1321 MiB to 4536 MiB from 12:26, back to 1442 MiB by 14:07, the main heap flat) is held by work that ended, and neither the heap tally nor the held counts see native bytes. So each minute the sampler also hands the watch the jobs begun under `during` and not yet settled (`runningOf` in `perf/jobs.ts`: the thread's and the forge's, by name, with how many are open and when the oldest began), and the trip keeps them. The finding then says `memory jobs open at the trip: begun since the growth began: snapshot since …; open before it: runner-wait ×3 since …` and `memory jobs open now: …`. "Since the growth began" counts from the last minute RSS was under 1.25x its first hour, so a job begun in the minute before the first sample over the line is named with the growth; a long poll open all day sits apart under `open before it`. A job open at the trip and gone from the `now` line, with RSS fallen back, is the one to read. Five are named; the rest are counted as `and N more`. Test: `test/sentinel-memory-evidence.test.ts`, `test/perf-jobs-per-forge.test.ts`.
3131
32 **The stream pass open across the trip (#17205).** The first finding with the jobs open at the trip named one: `snapshot-stream`, begun 20:27:01, two minutes before RSS stood at 5458 MiB with the main heap at 140 MiB just after a collection, and still open seven minutes later (a pass runs every five). Each hosted forge in a pass listed the bucket's whole `chunks/` prefix twice: once for the chunks already there, once for the check before its manifest. Each listing is a `Set` of every key the bucket holds, and the bucket gains keys all day until the nightly sweep. A pass's forges now share one view (`ChunkView` in `snapshot/snapshot.ts`): the first lists, each forge's check listing is the next forge's start, and a forge that throws leaves the view empty, so the next lists afresh. A pass of N forges lists N + 1 times, not 2N. The check before each manifest is still a fresh listing. Measured off production, one listing of 300,000 keys cost about 100 MiB of heap and was collected, so this cuts the churn but does not prove the stream is the whole of the growth; the next finding's `jobs open at the trip` says whether `snapshot-stream` is still there. Test: `test/snapshot-stream.test.ts`.
33
3234 A restart between the growth and the filing (a release restarts the forge several times a day) leaves a watch that never saw it: the sentence is the stored one of the process that grew, and its own evidence would say `trip none`. So each minute the need is open, the sampler writes the watch's evidence to `memory-kept.json` under the data directory (`perf/memory_kept.ts`, #16872); a finding filed by a later process that has not tripped quotes those lines, kept that day, under `memory kept by the process started …, at its last grown minute …; the forge restarted since`, and ends with one `memory now (process started …)` line for the process reading it.
3335
3436 The need clears by itself once RSS falls under twice the baseline. A process that slept through its first hour has no baseline and raises none.
src/snapshot/service.ts +5 −1
@@ -23,6 +23,7 @@ import {
2323 notInSnapshot,
2424 pruneSnapshots,
2525 takeSnapshot,
26 type ChunkView,
2627 type Manifest,
2728 type Progress,
2829 } from "./snapshot.ts";
@@ -376,9 +377,12 @@ export function startSnapshots(
376377 await (inFlight ?? Promise.resolve()).catch(() => {});
377378 const store = current();
378379 if (store === undefined || stopped) return undefined;
380 // One view of the bucket's chunks for the pass: each forge's check listing is the next one's
381 // start, so a pass of N forges lists the chunks N + 1 times, not 2N (#17205).
382 const chunks: ChunkView = {};
379383 return streamPass(app.config.dataDir, (name) =>
380384 takeSnapshot(
381 { db: app.db, dataDir: app.config.dataDir, reposRoot: app.reposRoot, version: options.version, passphrase: options.passphrase, stream: name },
385 { db: app.db, dataDir: app.config.dataDir, reposRoot: app.reposRoot, version: options.version, passphrase: options.passphrase, stream: name, chunks },
382386 store,
383387 ),
384388 );
src/snapshot/snapshot.ts +29 −4
@@ -157,10 +157,14 @@ export function chunksNamed(m: Manifest): string[] {
157157 /** A chunk's key as `list` gives it: hidden on a sealed lane, as given elsewhere. */
158158 export const storedChunk = async (store: Store, id: string): Promise<string> => (await store.storedKey?.(chunkKey(id))) ?? chunkKey(id);
159159
160 /** The chunks among `ids` the store does not list, by one fresh listing. */
161 export async function missingChunks(store: Store, ids: readonly string[]): Promise<string[]> {
160 /**
161 * The chunks among `ids` the store does not list, by one fresh listing. `listed` is handed that
162 * listing, for a caller that would otherwise list again.
163 */
164 export async function missingChunks(store: Store, ids: readonly string[], listed?: (there: Set<string>) => void): Promise<string[]> {
162165 if (ids.length === 0) return [];
163166 const there = new Set(await store.list(CHUNK_PREFIX));
167 listed?.(there);
164168 const missing: string[] = [];
165169 for (const id of ids) if (!there.has(await storedChunk(store, id))) missing.push(id);
166170 return missing;
@@ -337,6 +341,21 @@ export interface Snapshotter {
337341 * the snapshot's, so a forge unchanged since the last pass sends nothing.
338342 */
339343 readonly stream?: string | undefined;
344 /**
345 * The store's chunks as one stream pass last listed them, shared by its forges one after
346 * another (#17205): a forge starts from it in place of a listing of its own, and leaves it the
347 * fresh listing its check before the manifest made. A forge that throws leaves it empty, so the
348 * next one lists afresh.
349 */
350 readonly chunks?: ChunkView | undefined;
351 }
352
353 /**
354 * What a run of snapshots knows of the store's chunks between them: `keys` as `list` gave them,
355 * or undefined when the next must list.
356 */
357 export interface ChunkView {
358 keys?: Set<string> | undefined;
340359 }
341360
342361 /** Where the stream's manifests sit: one per hosted forge, always the newest pass. */
@@ -544,7 +563,11 @@ export async function takeSnapshot(
544563 listed(path, body.byteLength, chunkId(body), chunks);
545564 };
546565 try {
547 there = new Set(await store.list(CHUNK_PREFIX));
566 // A pass's view is taken, not shared while this forge adds to it: it goes back only as the
567 // fresh listing below, so a forge that throws leaves the next to list afresh.
568 const viewed = forge.chunks?.keys;
569 if (forge.chunks !== undefined) forge.chunks.keys = undefined;
570 there = viewed ?? new Set(await store.list(CHUNK_PREFIX));
548571 const heldBefore = there.size;
549572 // The database, consistently, from the live connection.
550573 if (!streaming) {
@@ -765,13 +788,15 @@ export async function takeSnapshot(
765788 if (refused !== undefined) throw new Error(`the stream of ${refused.name} was not written: ${refused.why}`);
766789 // Announced only when whole: every chunk it names, seen in the store now.
767790 const named = chunksNamed(manifest);
768 const missing = await missingChunks(store, named);
791 let fresh: Set<string> | undefined;

Works, but assigning through a void callback is indirect. Next time, let missingChunks return both the missing ids and the listing.

792 const missing = await missingChunks(store, named, (listed) => void (fresh = listed));
769793 if (missing.length > 0)
770794 throw new Error(`${missing.length} of the ${named.length} chunks this snapshot names are not in ${store.where} (first ${missing[0]}); no manifest written`);
771795 await store.put(
772796 forge.stream === undefined ? `${prefix}manifest.json` : streamManifestKey(forge.stream),
773797 JSON.stringify(manifest, null, 2),
774798 );
799 if (forge.chunks !== undefined) forge.chunks.keys = fresh;
775800 log.info("snapshot taken", {
776801 name,
777802 where: store.where,
test/snapshot-stream.test.ts +25 −1
@@ -16,7 +16,8 @@ import { commitLog } from "../src/git/read.ts";
1616 import { repoDir } from "../src/git/repos.ts";
1717 import { restoreHostedInto } from "../src/snapshot/restore.ts";
1818 import { startSnapshots } from "../src/snapshot/service.ts";
19 import { pruneSnapshots, streamManifestKey, takeSnapshot } from "../src/snapshot/snapshot.ts";
19 import { CHUNK_PREFIX } from "../src/snapshot/chunks.ts";
20 import { pruneSnapshots, streamManifestKey, takeSnapshot, type ChunkView } from "../src/snapshot/snapshot.ts";
2021 import { readStream, STREAM_LATE_MS, streamLag, streamPass } from "../src/snapshot/stream.ts";
2122 import type { Ctx } from "../src/http/context.ts";
2223 import { tool as systemTool } from "../src/http/mcp/tools/system.ts";
@@ -129,6 +130,29 @@ describe("the stream of hosted forges", () => {
129130 expect((await restoreHostedInto(store, "stream", "kid", dir, { passphrase: PASS })).repos).toBe(1);
130131 });
131132
133 test("forges of one pass share its view of the chunks: N + 1 listings, not 2N (#17205)", async () => {
134 const inner = dirStore(join(root, `store-${n++}`));
135 let listings = 0;
136 const store = { ...inner, list: (prefix: string) => (prefix === CHUNK_PREFIX && listings++, inner.list(prefix)) };
137 const chunks: ChunkView = {};
138 await takeSnapshot({ ...shape(), stream: "kid", chunks }, store);
139 expect(listings).toBe(2);
140 // The view left is the fresh listing the check made, the store as it is now.
141 expect([...chunks.keys!].sort()).toEqual((await inner.list(CHUNK_PREFIX)).sort());
142 const second = await takeSnapshot({ ...shape(), stream: "kid", chunks }, store);
143 expect(listings).toBe(3);
144 expect(second.sent?.chunks).toBeLessThan(3);
145 // A forge that throws leaves no view: the next lists afresh, and what it says is whole.
146 const refuse: NonNullable<Parameters<typeof takeSnapshot>[0]["hostedCopy"]> = async () => {
147 throw new Error("copy refused");
148 };
149 await expect(takeSnapshot({ ...shape(), stream: "kid", chunks, hostedCopy: refuse }, store)).rejects.toThrow("copy refused");
150 expect(chunks.keys).toBeUndefined();
151 await takeSnapshot({ ...shape(), stream: "kid", chunks }, store);
152 expect(listings).toBe(5);
153 expect((await restoreHostedInto(inner, "stream", "kid", join(root, "after-view"), { passphrase: PASS })).repos).toBe(1);
154 });
155
132156 test("the MCP system tool's stream read gives an admin each forge's lag and refuses anyone else", async () => {
133157 const store = dirStore(join(root, `store-${n++}`));
134158 await pass(store, new Date());

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs