Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ const STALE_WARN_THROTTLE_MS = 60_000;
* with a size change is foreign and forces the full read. */
const OWN_WRITE_STAMP_EPSILON_MS = 1;

/** #5219: decoded-file-count guards for the #3199 bounded-boot parse window.
* The window's stop condition (candidates ≥ maxSessions AND bytes ≥ maxBytes)
* degenerates when neither term binds: at maxSessions=0 (the sessionWarmMax()
* default) the count term is vacuous and the loop decoded the WHOLE corpus
* pre-listen before the savedAt-desc trim popped every candidate anyway;
* with many small files the byte budget fills one file at a time and does
* the same. Decoded attempts are capped — 0 when nothing survives the trim,
* otherwise a multiple of the warm-set size, clamped so normal corpora never
* hit it (the byte budget stops first there). Files past the cap stay
* lazy-loadable through loadSync; their alias bookkeeping lands in
* scanRemainderInBackground. */
const BOOT_WINDOW_DECODES_PER_SESSION = 16;
const BOOT_WINDOW_MIN_DECODES = 64;
const BOOT_WINDOW_MAX_DECODES = 2048;

interface PersistedSession {
version: number;
savedAt: number;
Expand Down Expand Up @@ -949,11 +964,17 @@ export class SessionStore {
const parsedFiles = new Set<string>();
const parsedCands: Array<{ file: string; id: string; meta: PersistedSession["meta"]; savedAt: number; bytes: number }> = [];
let windowBytes = 0;
const decodeCap = limits.maxSessions === 0
? 0
: Math.min(BOOT_WINDOW_MAX_DECODES, Math.max(BOOT_WINDOW_MIN_DECODES, limits.maxSessions * BOOT_WINDOW_DECODES_PER_SESSION));
let decoded = 0;
for (const cand of hot) {
if (decoded >= decodeCap) break;
if (parsedCands.length >= limits.maxSessions && windowBytes >= limits.maxBytes) break;
// on-disk size lower-bounds decoded size (#2803) — cheap pre-filter
if (cand.size > limits.maxBytes) continue;
const parsed = await this.readRawFileWithBytes(cand.file);
decoded++;
parsedFiles.add(cand.file);
if (!parsed) continue;
const rec = this.parseBootPayload(parsed.value);
Expand Down
143 changes: 143 additions & 0 deletions tests/issue5219-bounded-boot-decode-cap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// #5219 regression gate: bounded boot must not decode the whole corpus pre-listen.
//
// The #3199 parse window stops when candidates >= maxSessions AND bytes >= maxBytes.
// When neither term binds — maxSessions=0 (the sessionWarmMax() default) or a corpus
// of many small files under a large byte budget — the old loop decoded every file
// synchronously before listen, then popped the candidates away in the savedAt-desc
// trim anyway. On a store that grows to thousands of files this exceeds the dsh
// launcher's 60s health budget and the spawn-kill loop follows (#5219). These gates
// pin the decoded-file-count cap: zero decodes when nothing survives the trim, a
// bounded multiple of the warm-set size otherwise, and unchanged behavior when the
// cap never binds.

import assert from "node:assert/strict";
import { test } from "node:test";
import { mkdtempSync, rmSync, utimesSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { SessionStore, _setStoreForTest, relPathFor } from "../src/persist.ts";
import { createSession, _resetSessionsForTest } from "../src/session.ts";

type DecodeProbe = {
readRawFileWithBytes(file: string): Promise<{ value: unknown; bytes: number } | null>;
};

async function fixture(run: (dir: string, store: SessionStore) => Promise<void>): Promise<void> {
const root = mkdtempSync(path.join(tmpdir(), "bili-5219-"));
const dir = path.join(root, "sessions");
const store = new SessionStore({ dir, enabled: true, debounceMs: 0 });
_resetSessionsForTest(256, 512 * 1024 * 1024);
_setStoreForTest(store);
try { await run(dir, store); }
finally {
store.closeQuietly();
_resetSessionsForTest(256, 512 * 1024 * 1024);
_setStoreForTest(new SessionStore({ enabled: false }));
rmSync(root, { recursive: true, force: true });
}
}

async function withUnified(run: () => Promise<void>): Promise<void> {
const prev = process.env.BILI_STORAGE_UNIFIED;
process.env.BILI_STORAGE_UNIFIED = "full";
try { await run(); }
finally {
if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED;
else process.env.BILI_STORAGE_UNIFIED = prev;
}
}

/** Count readRawFileWithBytes invocations on this instance. Only the boot parse
* window (and the post-listen background scan) decode through it — loadSync and
* the sweep paths do not. Reading the count immediately after boot() resolves
* measures the window total plus at most a handful of background-scan stragglers
* (boot's post-window awaits can admit a few fs I/O callbacks before returning),
* so the gates assert bounds calibrated to that: the degenerate old loop decoded
* the WHOLE corpus here, which no bound admits. */
function countDecodes(store: SessionStore): { count(): number; restore(): void } {
const probe = store as unknown as DecodeProbe;
const real = probe.readRawFileWithBytes.bind(store);
let n = 0;
probe.readRawFileWithBytes = async (file: string) => { n += 1; return real(file); };
return {
count: () => n,
restore: () => { probe.readRawFileWithBytes = real; },
};
}

test("#5219: default-config bounded boot (maxSessions=0) performs zero pre-listen decodes", async () => {
await withUnified(() => fixture(async (_dir, store) => {
for (let i = 0; i < 100; i++) {
const s = createSession(`s5219a-${i}`);
await store.writeNow(s);
}
const meter = countDecodes(store);
try {
const loaded = await store.boot({ maxSessions: 0, maxBytes: 64 * 1024 * 1024 });
// The degenerate case: the old window loop decoded all 100 files here
// (the count stop-term is vacuous at maxSessions=0), then popped them
// all in the savedAt-desc trim. Nothing may be decoded pre-listen now —
// the warmed set was going to be empty either way, so the work was pure
// waste before the fix and the background scan owns bookkeeping now.
// Bound (not equality): boot's post-window awaits can admit a handful
// of background-scan stragglers; the old code's 100 never fits.
assert.ok(meter.count() < 10, `pre-listen decodes=${meter.count()}`);
assert.equal(loaded.size, 0);
} finally { meter.restore(); }
// Files stay lazily loadable — availability over performance.
assert.ok(store.loadSync("s5219a-0"));
assert.ok(store.loadSync("s5219a-99"));
}));
});

test("#5219: bounded boot caps pre-listen decodes at a warm-set multiple", async () => {
await withUnified(() => fixture(async (dir, store) => {
const ids: string[] = [];
for (let i = 0; i < 70; i++) {
const s = createSession(`s5219b-${i}`);
await store.writeNow(s);
ids.push(s.id);
}
// Deterministic mtime-desc order: index 69 newest … 0 oldest (readdir
// order is arbitrary on ties, so stagger explicitly).
const base = Date.now() - 70_000;
for (let i = 0; i < ids.length; i++) {
const t = new Date(base + i * 1000);
utimesSync(path.join(dir, relPathFor(ids[i])), t, t);
}
const meter = countDecodes(store);
try {
const loaded = await store.boot({ maxSessions: 4, maxBytes: 64 * 1024 * 1024 });
// cap = max(64, 4*16) = 64: at most 64 of the 70 files may be decoded
// in the window (plus a handful of background stragglers); the old
// loop decoded all 70 in the window, which the upper bound rejects.
const n = meter.count();
assert.ok(n >= 64 && n < 70, `pre-listen decodes=${n}`);
assert.ok(loaded.size > 0);
assert.ok(loaded.size <= 4);
} finally { meter.restore(); }
// The files past the cap stay lazily loadable.
assert.ok(store.loadSync(ids[0]));
assert.ok(store.loadSync(ids[1]));
}));
});

test("#5219: behavior unchanged when the cap never binds (small corpus)", async () => {
await withUnified(() => fixture(async (_dir, store) => {
for (let i = 0; i < 10; i++) {
const s = createSession(`s5219c-${i}`);
await store.writeNow(s);
}
const meter = countDecodes(store);
try {
const loaded = await store.boot({ maxSessions: 3, maxBytes: 64 * 1024 * 1024 });
// cap = max(64, 3*16) = 64 — far above the 10-file corpus, so every
// file is still decoded exactly as before the fix.
assert.equal(meter.count(), 10);
assert.ok(loaded.size > 0);
assert.ok(loaded.size <= 3);
} finally { meter.restore(); }
assert.ok(store.loadSync("s5219c-0"));
assert.ok(store.loadSync("s5219c-9"));
}));
});
16 changes: 13 additions & 3 deletions tests/wire-exit-matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ const SLOW_MS = 1500;
// Raised to 10s after 6s still flaked three consecutive runs on windows-latest
// node22 shard 2 (master c78ecb4b + ci-fixes c49bf8b1/12b988ff, all closed=0,
// same cell, sibling shards green — load latency, not a teardown regression;
// recorded under #4599). If it flakes again at 10s, investigate a real teardown
// hang instead of raising further.
const ABORT_TEARDOWN_BUDGET_MS = 10000;
// recorded under #4599). Flaked once more at 10s (master 9dc4c945, run
// 41686972484, 2026-11-03: openai-sse + anthropic-sse closed=0 after the full
// poll, ~11s each; same code green on the ubuntu22/24 and windows24 shard-2
// siblings, pass durations 23–352ms). The mandated "investigate a real
// teardown hang" audit found none: relay.ts arms res.on("close") ->
// clientAbort.abort() synchronously with no linger/backstop on this path, and
// no commit since d244ffff touched destroy/abort wiring — the residual latency
// is runner-side event-loop starvation on loaded Windows runners. Hence a
// platform-scoped budget instead of another flat raise: win32 doubles to 20s
// (every recorded starvation instance was windows-latest — node22+24, multiple
// shards, #3665/#4347 era — ubuntu never showed the signature), keeping the
// tight 10s gate everywhere else so a genuine teardown hang still fails fast.
const ABORT_TEARDOWN_BUDGET_MS = process.platform === "win32" ? 20000 : 10000;

async function awaitUpstreamTeardown(socketsClosed: () => number): Promise<void> {
const deadline = Date.now() + ABORT_TEARDOWN_BUDGET_MS;
Expand Down
Loading