diff --git a/packages/cli/src/utils/projectLock.test.ts b/packages/cli/src/utils/projectLock.test.ts index 5ae18eebc9..dcbc5c0b43 100644 --- a/packages/cli/src/utils/projectLock.test.ts +++ b/packages/cli/src/utils/projectLock.test.ts @@ -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); diff --git a/packages/cli/src/utils/projectLock.ts b/packages/cli/src/utils/projectLock.ts index 7067e74f89..6057fdcc02 100644 --- a/packages/cli/src/utils/projectLock.ts +++ b/packages/cli/src/utils/projectLock.ts @@ -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( projectDir: string, command: string, @@ -82,7 +93,12 @@ export async function withProjectLock( 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( } 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));