Fix agents
This commit is contained in:
@@ -45,6 +45,7 @@ const {
|
||||
mkdtempSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
realpathSync,
|
||||
existsSync,
|
||||
@@ -192,20 +193,32 @@ function isProcessAlive(pid: number): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
function removeTmpdirRedirectSinkForPid(ownerPid: number): void {
|
||||
try {
|
||||
rmSync(join(WORKER_ROOT, `redir-${ownerPid}`), { recursive: true, force: true });
|
||||
} catch {
|
||||
// Ignore stale-sink cleanup failures; global teardown still owns WORKER_ROOT.
|
||||
}
|
||||
}
|
||||
|
||||
function sweepDeadTmpdirRedirectSinks(): void {
|
||||
if (tmpdirRedirectSweepComplete) return;
|
||||
tmpdirRedirectSweepComplete = true;
|
||||
|
||||
let ownerPids: number[];
|
||||
// Registry-backed cleanup avoids scanning the OS temp root while still
|
||||
// reclaiming redirect sinks from fork-pool workers that were hard-killed.
|
||||
let ownerPids: number[] = [];
|
||||
try {
|
||||
ownerPids = Array.from(new Set(
|
||||
readFileSync(TMPDIR_REDIRECT_REGISTRY, "utf8")
|
||||
.split(/\r?\n/)
|
||||
.split(/
|
||||
?
|
||||
/)
|
||||
.map((line) => Number.parseInt(line, 10))
|
||||
.filter((pid) => Number.isInteger(pid) && pid > 0),
|
||||
));
|
||||
return;
|
||||
} catch {
|
||||
// The registry may not exist yet. The bounded WORKER_ROOT sweep below still
|
||||
// catches legacy redirect dirs created before the registry was introduced.
|
||||
}
|
||||
|
||||
@@ -215,15 +228,30 @@ function sweepDeadTmpdirRedirectSinks(): void {
|
||||
liveOwnerPids.push(ownerPid);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
rmSync(join(WORKER_ROOT, `redir-${ownerPid}`), { recursive: true, force: true });
|
||||
} catch {
|
||||
// Ignore stale-sink cleanup failures; global teardown still owns WORKER_ROOT.
|
||||
|
||||
removeTmpdirRedirectSinkForPid(ownerPid);
|
||||
}
|
||||
|
||||
// Preserve the local self-healing behavior for redirect dirs that predate the
|
||||
// registry or whose registry append was skipped. This is a single-level scan
|
||||
// of WORKER_ROOT (not the OS temp root) and only touches dead pid-owned dirs.
|
||||
try {
|
||||
for (const entry of readdirSync(WORKER_ROOT)) {
|
||||
const match = /^redir-(\d+)$/.exec(entry);
|
||||
if (!match) continue;
|
||||
const ownerPid = Number.parseInt(match[1], 10);
|
||||
if (ownerPid === process.pid || liveOwnerPids.includes(ownerPid) || isProcessAlive(ownerPid)) {
|
||||
continue;
|
||||
}
|
||||
removeTmpdirRedirectSinkForPid(ownerPid);
|
||||
}
|
||||
} catch {
|
||||
// Best-effort only; stale entries are harmless and swept by future workers.
|
||||
}
|
||||
|
||||
writeFileSync(TMPDIR_REDIRECT_REGISTRY, liveOwnerPids.length > 0 ? `${liveOwnerPids.join("\n")}\n` : "");
|
||||
try {
|
||||
writeFileSync(TMPDIR_REDIRECT_REGISTRY, liveOwnerPids.length > 0 ? `${liveOwnerPids.join("
|
||||
")}
|
||||
` : "");
|
||||
} catch {
|
||||
// Best-effort only; stale entries are harmless and swept by future workers.
|
||||
@@ -236,7 +264,8 @@ function ensureTmpdirRedirectSink(): string {
|
||||
sweepDeadTmpdirRedirectSinks();
|
||||
const sink = join(WORKER_ROOT, `redir-${process.pid}`);
|
||||
mkdirSync(sink, { recursive: true });
|
||||
appendFileSync(TMPDIR_REDIRECT_REGISTRY, `${process.pid}\n`);
|
||||
try {
|
||||
appendFileSync(TMPDIR_REDIRECT_REGISTRY, `${process.pid}
|
||||
`);
|
||||
} catch {
|
||||
// Best-effort only; the process exit hook and global teardown still clean up.
|
||||
@@ -256,6 +285,11 @@ function ensureTmpdirRedirectSink(): string {
|
||||
|
||||
return sink;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a mkdtemp prefix points straight at the OS temp root, rewrite it into a
|
||||
* swept per-process sink under WORKER_ROOT. Prefixes already nested under a
|
||||
* subdirectory pass through unchanged, as do non-string prefixes (Buffer/URL).
|
||||
*/
|
||||
function redirectTmpdirPrefix<T>(prefix: T): T {
|
||||
if (typeof prefix !== "string") return prefix;
|
||||
@@ -264,6 +298,7 @@ function redirectTmpdirPrefix<T>(prefix: T): T {
|
||||
if (parent !== tmpdir() && parent !== REAL_TMPDIR) return prefix;
|
||||
|
||||
return join(ensureTmpdirRedirectSink(), basename(prefix)) as T;
|
||||
}
|
||||
}
|
||||
|
||||
function ensureIsolatedHome(): void {
|
||||
|
||||
Reference in New Issue
Block a user