fix(cli): a command waiting on the project lock says what it waits for (#7184)

* fix(cli): a command waiting on the project lock says what it waits for

* fix(cli): the project lock timeout says to run the command again
This commit is contained in:
Miguel Ángel
2026-11-04 21:01:53 +00:00
committed by GitHub
parent 73e65e2c78
commit 00ea86570a
2 changed files with 36 additions and 2 deletions
+19 -1
View File
@@ -94,10 +94,28 @@ describe("withProjectLock", () => {
);
expect(Date.now() - started).toBeGreaterThanOrEqual(300);
await expect(withProjectLock(dir, "snapshot", async () => "ran", locked(0))).rejects.toThrow(
`hyperframes check (pid ${process.pid}) is still using this project`,
`hyperframes check (pid ${process.pid}) is still using this project after 0s. Run this command again when it finishes.`,
);
});
it("says who it is waiting for after a few seconds, so a queued command does not read as hung", async () => {
const dir = project();
hold(dir, process.pid);
const said = vi.spyOn(console, "error").mockImplementation(() => undefined);
const base = Date.now();
const clock = vi
.spyOn(Date, "now")
.mockReturnValueOnce(base)
.mockReturnValue(base + 6_000);
await expect(withProjectLock(dir, "check", async () => "ran", locked(300))).rejects.toThrow(
ProjectLockTimeout,
);
clock.mockRestore();
expect(said).toHaveBeenCalledTimes(1);
expect(said.mock.calls[0]?.[0]).toContain(`waiting for check (pid ${process.pid}, running `);
said.mockRestore();
});
it("takes over a lock whose process has exited", async () => {
const dir = project();
hold(dir, 2 ** 22 + 12345);
+17 -1
View File
@@ -71,6 +71,17 @@ function tryAcquire(path: string, holder: Holder): boolean {
return false;
}
const FIRST_NOTICE_MS = 5_000;
const NOTICE_EVERY_MS = 30_000;
function sayWaiting(current: Holder | null, waitMs: number): void {
if (!current) return;
const running = Math.round((Date.now() - current.startedAt) / 1000);
console.error(
`[hyperframes] waiting for ${current.command} (pid ${current.pid}, running ${running}s) to finish with this project; waiting up to ${Math.round(waitMs / 1000)}s`,
);
}
export async function withProjectLock<T>(
projectDir: string,
command: string,
@@ -82,7 +93,12 @@ export async function withProjectLock<T>(
mkdirSync(dirname(path), { recursive: true });
const holder: Holder = { pid: process.pid, command, startedAt: Date.now() };
const deadline = holder.startedAt + context.projectLock.waitMs;
let nextNoticeAt = holder.startedAt + FIRST_NOTICE_MS;
while (!tryAcquire(path, holder)) {
if (Date.now() >= nextNoticeAt) {
nextNoticeAt = Date.now() + NOTICE_EVERY_MS;
sayWaiting(readHolder(path), context.projectLock.waitMs);
}
const remaining = deadline - Date.now();
if (remaining <= 0) {
const current = readHolder(path);
@@ -95,7 +111,7 @@ export async function withProjectLock<T>(
}
throw new ProjectLockTimeout(
`hyperframes ${current?.command ?? "another command"} (pid ${current?.pid ?? "unknown"}) ` +
`is still using this project after ${Math.round(context.projectLock.waitMs / 1000)}s`,
`is still using this project after ${Math.round(context.projectLock.waitMs / 1000)}s. Run this command again when it finishes.`,
);
}
await sleep(Math.min(POLL_MS, remaining));