From 974ba6437d2d0cb318dbf9fbf3215d66ae74634c Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 22:17:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(test):=20release=20self-held=20handles?= =?UTF-8?q?=20before=20temp-tree=20teardown=20=E2=80=94=20Windows=20EBUSY/?= =?UTF-8?q?EPERM=20(#2799)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two distinct self-held-handle mechanisms made Windows CI teardown reds non-deterministic (R1: unlink index.db EBUSY in storage-engine-proofs; R2: rmrf golden-chat-* EPERM). Backoff cannot absorb a lock held by the deleting process itself, so release such handles up front: - src/logger.ts: _releaseLogFileForTest() seam — synchronously close the tee-log fd and clear its path (closeLogger() keeps the path → next log() reopens into the deleted tree; configureLogger("off") leaks the fd). - tests/tmp-rm.ts: rmrf() releases the handle when the current log path lies under the deletion target — systemic guard for every importer. - tests/golden-storage-drive.ts: the producer fix — driveWire pointed XDG_STATE_HOME at its disposable tmp BEFORE startServer, landing the default log file INSIDE the tree finally deletes; set logFile:"off" and close the drive store explicitly in finally instead of relying on flushAllSessions()'s shadow-close side effect. - tests/mode-upgrade-proof.test.ts: switch its local bare-rmSync rmrf to the shared retried helper. - tests/tmp-rm.test.ts: deterministic regression pin for the guard (release + path-clear + no resurrection of the deleted tree). --- src/logger.ts | 31 ++++++++++++++++++++++++++++++- tests/golden-storage-drive.ts | 13 +++++++++++++ tests/mode-upgrade-proof.test.ts | 5 +---- tests/tmp-rm.test.ts | 24 ++++++++++++++++++++++++ tests/tmp-rm.ts | 23 +++++++++++++++++++++++ 5 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index e3ff473808..30aa696f4e 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -35,7 +35,7 @@ * masking in log-mask.ts. The programmatic capture hook receives the raw * message: it is an in-process test/diagnostic seam, not a log surface. */ -import { createWriteStream, fstatSync, mkdirSync, statSync, renameSync, unlinkSync, type WriteStream } from "node:fs"; +import { closeSync, createWriteStream, fstatSync, mkdirSync, statSync, renameSync, unlinkSync, type WriteStream } from "node:fs"; import path from "node:path"; import { AsyncLocalStorage } from "node:async_hooks"; import { VERSION } from "./version.js"; @@ -322,3 +322,32 @@ export function closeLogger(): Promise { export function getLogPath(): string | undefined { return logPath; } + +/** Test seam (#2799): synchronously release the held log-file handle and + * forget its path so test teardown can delete the tree containing it. On + * Windows a process cannot unlink a file it holds open, and neither existing + * exit is safe for that: closeLogger() keeps logPath set (the next log() + * reopens into the deleted tree) and configureLogger("off") drops the stream + * without ever closing it (fd lingers until GC). The fd is closed + * synchronously — an async end()/destroy() cannot complete while a teardown + * retry sleeps via Atomics.wait (blocked main thread ⇒ libuv never services + * the close within the retry window). Buffered lines are lost; acceptable on + * a teardown path where the record under test has already been read. */ +export function _releaseLogFileForTest(): void { + const s = stream; + const fd = streamFd; + stream = undefined; + streamFd = undefined; + bytesWritten = 0; + logPath = undefined; + reopenWarned = false; + if (!s) return; + // Swallow late errors (e.g. EBADF once the fd below is gone) instead of + // letting an unlistened 'error' event crash the test process. + s.on("error", () => {}); + if (typeof fd === "number") { + try { closeSync(fd); } catch { /* already closed/invalid */ } + } + // fd still undefined ⇒ open not delivered yet: nothing is held, and the + // pending open dies harmlessly against the deleted path. +} diff --git a/tests/golden-storage-drive.ts b/tests/golden-storage-drive.ts index 991b18d8f8..5675dcb64d 100644 --- a/tests/golden-storage-drive.ts +++ b/tests/golden-storage-drive.ts @@ -353,6 +353,13 @@ function goldenProxyOptions(upstreamPort: number, ctx: number): ProxyOptions { promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, + // #2799: XDG_STATE_HOME is pointed at the disposable drive tmp below + // BEFORE startServer, so the default log file would land INSIDE the + // tree that finally's rmrf(tmp) deletes — with the tee logger holding + // it open for process life. On Windows the self-held handle makes the + // deletion fail EPERM (the R2 red). No file stream at all then; the + // stderr side of the tee is untouched. + logFile: "off", debug: false, passthrough: false, passthroughSource: null, @@ -552,6 +559,12 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: await closeServer(proxy); await closeServer(upstream); _resetSessionsForTest(); + // #2799: don't rely on flushAllSessions()'s shadow-close side effect — + // an early exit (assertion failure mid-drive) leaves this store's + // index.db open, pinning the tree on Windows and masking the real + // failure with a cleanup EBUSY/EPERM. Mirrors the reload path above. + store?.cancelAll(); + store?.closeQuietly(); if (prevXdg === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = prevXdg; if (prevUnified === undefined) delete process.env.BILI_STORAGE_UNIFIED; diff --git a/tests/mode-upgrade-proof.test.ts b/tests/mode-upgrade-proof.test.ts index ffbc309989..c0e1842a96 100644 --- a/tests/mode-upgrade-proof.test.ts +++ b/tests/mode-upgrade-proof.test.ts @@ -41,6 +41,7 @@ import { getSession } from "../src/session.ts"; import { conflictEventsOf } from "../src/conflict-watch.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; import { defaultConfig } from "acp-kernel"; +import { rmrf } from "./tmp-rm.ts"; type Item = Record; @@ -75,10 +76,6 @@ function replyLabel(body: string): string { return last ? last[1]! : "0"; } -function rmrf(p: string): void { - fs.rmSync(p, { recursive: true, force: true }); -} - function bodyInjectsCompress(body: string): boolean { const parsed = JSON.parse(body) as { tools?: Array<{ function?: { name?: string } }> }; return (parsed.tools ?? []).some((t) => t.function?.name === "compress"); diff --git a/tests/tmp-rm.test.ts b/tests/tmp-rm.test.ts index 1faa4ea9cc..efa24d7b0f 100644 --- a/tests/tmp-rm.test.ts +++ b/tests/tmp-rm.test.ts @@ -4,6 +4,7 @@ import os from "node:os"; import path from "node:path"; import { test } from "node:test"; import { _setRmImplForTest, rmrf } from "./tmp-rm.ts"; +import { configureLogger, getLogPath, log } from "../src/logger.js"; function tmpRoot(): string { return fs.mkdtempSync(path.join(os.tmpdir(), "bili-tmp-rm-")); @@ -94,3 +95,26 @@ test("rmrf: non-transient errors throw at once, no backoff", () => { test("rmrf: invalid input still throws TypeError (default impl)", () => { assert.throws(() => rmrf(42 as unknown as string), TypeError); }); + +// #2799: backoff cannot absorb a lock held BY THIS PROCESS. Suites that point +// XDG_STATE_HOME at their scratch tree before startServer make defaultLogFile() +// land inside that tree, and the tee logger holds it open for process life — +// on Windows every unlink of the tree then fails EPERM forever. The guard in +// rmrf() releases the self-held handle up front; this pin drives that path +// deterministically (the Windows EPERM itself is not reproducible off-Windows). +test("rmrf: releases a self-held tee-log handle under the target before deleting (#2799)", () => { + const dir = tmpRoot(); + const logFile = path.join(dir, "billion-context", "bili.log"); + // configureLogger opens the stream eagerly (mkdir + createWriteStream): + // from here the process holds — or is about to hold — an fd inside `dir`, + // exactly what startServer does when XDG_STATE_HOME points at the tree. + configureLogger(logFile); + assert.equal(getLogPath(), logFile); + rmrf(dir); + assert.equal(fs.existsSync(dir), false); + assert.equal(getLogPath(), undefined, "guard released the handle AND cleared the path"); + // Resurrection pin: with logPath cleared, later logging must NOT recreate + // the deleted tree (a stale path would mkdir + reopen on the next line). + log("info", "post-teardown write"); + assert.equal(fs.existsSync(logFile), false, "no resurrection of the deleted tree"); +}); diff --git a/tests/tmp-rm.ts b/tests/tmp-rm.ts index fa79772147..25692757ec 100644 --- a/tests/tmp-rm.ts +++ b/tests/tmp-rm.ts @@ -1,4 +1,7 @@ import { rmSync } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { _releaseLogFileForTest, getLogPath } from "../src/logger.js"; // #1646: bare recursive rmSync throws ENOTEMPTY on loaded CI runners when a // spawned child recreates entries between readdir and rmdir. The delete may @@ -13,6 +16,25 @@ import { rmSync } from "node:fs"; // ~3.2s and is only paid when a race actually happens. const RETRY_DELAYS_MS = [25, 50, 100, 200, 400, 800, 1600] as const; +// #2799: backoff cannot absorb a lock HELD BY THIS PROCESS. Suites that point +// XDG_STATE_HOME at their scratch tree before startServer make +// defaultLogFile() land INSIDE that tree (src/paths.ts resolves the env at +// call time), and the tee logger holds that file open for process life — on +// Windows every unlink of the tree then fails EPERM forever, no matter how +// long the budget below runs (the R2 red in #2799). Release the self-held +// handle up front; the retry loop stays the defense against EXTERNAL transient +// locks (AV/indexer), which are the ones backoff can outwait. +function releaseSelfHeldLogHandle(target: string | URL): void { + const logFile = getLogPath(); + if (!logFile) return; + const t = path.resolve(typeof target === "string" ? target : fileURLToPath(target)); + const root = t.endsWith(path.sep) ? t.slice(0, -1) : t; + const lf = path.resolve(logFile); + if (lf !== root && !lf.startsWith(root + path.sep)) return; + _releaseLogFileForTest(); + process.stderr.write(`[tmp-rm] released self-held log handle ${logFile} before deleting ${root}\n`); +} + /** Sync sleep that is legal on any thread in Node (blocks the event loop — * exactly what a teardown-path retry wants; no pending I/O ordering to keep). */ function sleepSync(ms: number): void { @@ -32,6 +54,7 @@ export function _setRmImplForTest(impl?: typeof defaultRm): void { } export function rmrf(target: string | URL): void { + releaseSelfHeldLogHandle(target); let attempt = 0; for (;;) { try { From 1388294263fb6f569533efe3b86223dd7ec0492a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 22:52:43 +0800 Subject: [PATCH 2/2] test(logger-rotation): re-arm logger after teardown clobber so reopen warn still fires (#2808) The shared rmrf() guard added in #2808 releases the self-held tee-log handle AND clears the log path before deleting (#2799). The reopen-failure rotation test removes its own live-log tree mid-test (via rmrf) to force an ENOTDIR reopen; after the guard clears logPath, the post-clobber log() hit the !logPath early-return instead of attempting the reopen, so the degradation [warn] never fired (0 !== 1) and the test went red on every platform. Re-point the logger at the now-invalid path after the clobber so the reopen attempt still happens and degrades with exactly one [warn]. No production code or behavior changes; the rmrf guard and its no-resurrection pin are unchanged. --- tests/logger-rotation.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/logger-rotation.test.ts b/tests/logger-rotation.test.ts index 0c8b4ee546..55733a1a59 100644 --- a/tests/logger-rotation.test.ts +++ b/tests/logger-rotation.test.ts @@ -79,7 +79,9 @@ test("reopen failure: degrades to stderr-only with one [warn], no crash", async // dir was, so mkdirSync(dirname) throws ENOTDIR on every reopen. rmrf(dir); fs.writeFileSync(dir, "blocker"); - + // rmrf() already released our self-held handle + cleared the log path + // (#2799); re-point at the invalid path so the reopen fails once. + configureLogger(file); log("info", "after-clobber"); // must not throw log("info", "still-alive"); // must not throw, must not re-warn