@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 2.33 kB
JavaScript
import { closeSync, openSync, rmSync } from "node:fs";
import { rm, stat } from "node:fs/promises";
import { safeDelay } from "../wrap.js";
/**
* Shares the existing O_EXCL file with synchronous migrators without blocking the event loop: waiting is
* asynchronous, but the lock is taken, used by `critical` and released in one synchronous step. A lock held
* across an await would let a synchronous reader in the same process spin on it with the event loop blocked,
* so the holder could never release it and every caller froze until the lock went stale (senpi#3546).
*/
export async function withLegacyLock(path, stale, critical) {
const deadline = Date.now() + stale;
for (;;) {
const file = tryOpen(path);
if (file !== undefined) {
try {
return critical();
}
finally {
try {
closeSync(file);
}
finally {
rmSync(path, { force: true });
}
}
}
await waitForHolder(path, stale, deadline);
}
}
/** Thrown when the lock itself cannot be taken; errors from the critical section propagate unchanged. */
export class LegacyLockAcquireError extends Error {
constructor(cause) {
super(cause instanceof Error ? cause.message : String(cause), { cause });
this.name = "LegacyLockAcquireError";
}
}
function tryOpen(path) {
try {
return openSync(path, "wx", 0o600);
}
catch (error) {
if (error instanceof Error && "code" in error && error.code === "EEXIST")
return undefined;
throw new LegacyLockAcquireError(error);
}
}
async function waitForHolder(path, stale, deadline) {
try {
const age = Date.now() - (await stat(path)).mtimeMs;
if (age >= stale) {
await rm(path, { force: true });
return;
}
}
catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT")
return;
throw new LegacyLockAcquireError(error);
}
if (Date.now() >= deadline) {
throw new LegacyLockAcquireError(new Error("Legacy OAuth migration lock acquisition timed out"));
}
await safeDelay(20);
}
//# sourceMappingURL=legacy-lock.js.map