Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 30 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -322,3 +322,32 @@ export function closeLogger(): Promise<void> {
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.
}
13 changes: 13 additions & 0 deletions tests/golden-storage-drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion tests/logger-rotation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions tests/mode-upgrade-proof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;

Expand Down Expand Up @@ -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");
Expand Down
24 changes: 24 additions & 0 deletions tests/tmp-rm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-"));
Expand Down Expand Up @@ -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");
});
23 changes: 23 additions & 0 deletions tests/tmp-rm.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Loading