From 13d91f3d97a21b05d70b18a2a914b9c435d605b3 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 12:41:55 +0800 Subject: [PATCH] fix: raise SQLite engine self-download timeout to match the updater (#3338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engine self-bootstrap capped its ~11 MB better-sqlite3 fetch at 10 s while the self-updater budgets 60 s for its own comparable tarball pull. On a non-trivial network (corporate proxy / slow link) that forced >1.1 MB/s sustained before TLS/CONNECT overhead, so a single slow boot on Node<=21 (no builtin node:sqlite) aborted the download and armed the 6 h cooldown — locking the machine into legacy-only storage for hours even though a working engine was obtainable. Align BOOTSTRAP_TIMEOUT_MS with the updater's 60 s budget. Only behavioral change: the startup wait ceiling in the narrow path where unified storage is requested AND no engine exists AND auto-download is allowed AND cache misses; the 6 h cooldown bounds it to at most once per window per machine. Adds a test seam + three regression tests: a floor guard on the production budget, an abort path (budget < download time), and a success path (budget > download time). --- src/storage/engine-bootstrap.ts | 15 ++++- tests/storage-engine-bootstrap.test.ts | 78 ++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/storage/engine-bootstrap.ts b/src/storage/engine-bootstrap.ts index 80afec8336..28e9d15c66 100644 --- a/src/storage/engine-bootstrap.ts +++ b/src/storage/engine-bootstrap.ts @@ -58,7 +58,11 @@ const SUPPORTED_TARGETS = [ "win32-x64", "win32-arm64", ]; -const BOOTSTRAP_TIMEOUT_MS = 10_000; +// Whole-download budget for the ~11 MB engine tarball. Must cover a multi-MB +// registry fetch over a non-trivial network (corporate proxy / slow link); +// 10 s locked Node<=21 machines into a 6 h legacy-only cooldown after one slow +// boot (#3338). Aligned with the updater's own tarball budget (src/update.ts). +export const BOOTSTRAP_TIMEOUT_MS = 60_000; export const COOLDOWN_MS = 6 * 60 * 60 * 1000; type BootstrapStatus = @@ -94,6 +98,13 @@ export function resetEngineBootstrapForTests(): void { inFlight = null; } +let bootstrapTimeoutOverrideMs: number | null = null; + +/** Test seam: override the whole-download timeout (null = production value). */ +export function setBootstrapTimeoutMsForTests(ms: number | null): void { + bootstrapTimeoutOverrideMs = ms; +} + function activeSpec(): EngineBootstrapSpec { return specOverride ?? DEFAULT_ENGINE_SPEC; } @@ -288,7 +299,7 @@ async function downloadAndInstall(spec: EngineBootstrapSpec, target: string, res // @types/node types RequestInit.dispatcher as its internal Dispatcher; // cast once here (no `as any`), same pattern as src/update.ts fetchWithEgress. const res = await fetch(url, { - signal: AbortSignal.timeout(BOOTSTRAP_TIMEOUT_MS), + signal: AbortSignal.timeout(bootstrapTimeoutOverrideMs ?? BOOTSTRAP_TIMEOUT_MS), ...(dispatcher ? { dispatcher } : {}), } as RequestInit); if (!res.ok || !res.body) throw new Error(`registry returned HTTP ${res.status} for ${url}`); diff --git a/tests/storage-engine-bootstrap.test.ts b/tests/storage-engine-bootstrap.test.ts index 6105f7e34d..0af9555c9d 100644 --- a/tests/storage-engine-bootstrap.test.ts +++ b/tests/storage-engine-bootstrap.test.ts @@ -41,10 +41,12 @@ import { setSqliteProbeOverridesForTests, } from "../src/storage/driver.ts"; import { + BOOTSTRAP_TIMEOUT_MS, ensureSqliteEngine, extractTgzToDir, platformTargetKey, resetEngineBootstrapForTests, + setBootstrapTimeoutMsForTests, setEngineBootstrapSpecForTests, setPlatformKeyForTests, type BootstrapOutcome, @@ -142,7 +144,7 @@ interface MockRegistry { close(): Promise; } -async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3"): Promise { +async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3", delayMs = 0): Promise { let count = 0; const server = http.createServer((req, res) => { count++; @@ -151,15 +153,27 @@ async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3") res.end(); return; } - res.writeHead(status, { "content-type": "application/octet-stream" }); - res.end(body); + const send = (): void => { + if (res.writableEnded || res.destroyed) return; + try { + res.writeHead(status, { "content-type": "application/octet-stream" }); + res.end(body); + } catch { + // client aborted the download before we got here — nothing to serve + } + }; + if (delayMs > 0) setTimeout(send, delayMs); + else send(); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const addr = server.address() as AddressInfo; return { base: `http://127.0.0.1:${addr.port}`, requests: () => count, - close: () => new Promise((r) => server.close(() => r())), + close: () => new Promise((r) => { + (server as unknown as { closeAllConnections?: () => void }).closeAllConnections?.(); + server.close(() => r()); + }), }; } @@ -188,6 +202,7 @@ function resetAllSeams(): void { clearSelfDownloadedEngineForTests(); setEngineBootstrapSpecForTests(null); setPlatformKeyForTests(null); + setBootstrapTimeoutMsForTests(null); resetEngineBootstrapForTests(); } @@ -431,6 +446,61 @@ test("bootstrap: stale unusable cache dir is replaced instead of looping the coo } }); +test("bootstrap: production download budget comfortably covers an ~11 MB registry fetch (#3338)", () => { + assert.ok(BOOTSTRAP_TIMEOUT_MS >= 30_000, + `BOOTSTRAP_TIMEOUT_MS=${BOOTSTRAP_TIMEOUT_MS} too tight for an ~11 MB download over a non-trivial network (was 10 s, see #3338)`); +}); + +test("bootstrap: a download that outlasts its timeout budget aborts fail-safe and arms the cooldown (#3338)", async () => { + const cachePrev = isolateCache(); + let reg: MockRegistry | null = null; + try { + resetAllSeams(); + const tgz = fakePackageTgz(); + setEngineBootstrapSpecForTests({ version: "13.0.3", sha512Base64: sha512B64(tgz) }); + setSqliteProbeOverridesForTests({ builtin: false, host: false }); + const delayMs = 800; + setBootstrapTimeoutMsForTests(Math.floor(delayMs / 5)); + reg = await startMockRegistry(tgz, 200, "13.0.3", delayMs); + await withRegistry(reg.base, async () => { + const out = await ensureSqliteEngine({ env: { ...UNIFIED_ENV } }); + assert.equal(out.status, "failed", `expected timeout failure, got ${JSON.stringify(out)}`); + assert.match(out.detail ?? "", /abort|time/i, "failure reason names the abort/timeout"); + assert.ok(!fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", "better-sqlite3-13.0.3")), "aborted download must not publish a cache dir"); + assert.ok(fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", ".bootstrap-failed.json")), "aborted download arms the cooldown"); + }); + } finally { + await reg?.close(); + resetAllSeams(); + restoreCache(cachePrev); + rmrf(cachePrev); + } +}); + +test("bootstrap: a slow-but-completing download succeeds once the budget covers it (#3338)", async () => { + const cachePrev = isolateCache(); + let reg: MockRegistry | null = null; + try { + resetAllSeams(); + const tgz = fakePackageTgz(); + setEngineBootstrapSpecForTests({ version: "13.0.3", sha512Base64: sha512B64(tgz) }); + setSqliteProbeOverridesForTests({ builtin: false, host: false }); + const delayMs = 400; + setBootstrapTimeoutMsForTests(delayMs * 10); + reg = await startMockRegistry(tgz, 200, "13.0.3", delayMs); + await withRegistry(reg.base, async () => { + const out = await ensureSqliteEngine({ env: { ...UNIFIED_ENV } }); + assert.equal(out.status, "downloaded", `expected slow download to succeed, got ${JSON.stringify(out)}`); + assert.ok(fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", "better-sqlite3-13.0.3", "package.json")), "slow download materialized"); + }); + } finally { + await reg?.close(); + resetAllSeams(); + restoreCache(cachePrev); + rmrf(cachePrev); + } +}); + test("bootstrap: offline (connection refused) degrades fail-safe with cooldown, never throws", async () => { const cachePrev = isolateCache(); try {