Skip to content

Commit 1f82269

Browse files
committed
fix(studio): a busy project history never makes writes wait for its lock again
1 parent 09b81ae commit 1f82269

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎packages/cli/src/server/studioServer.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ describe("createStudioServer project history (D-491)", () => {
185185
},
186186
);
187187

188+
it("never makes a request wait on another process's history lock again once it was busy", async () => {
189+
const waits: unknown[] = [];
190+
historyState.open = async (options) => {
191+
waits.push((options as { ownerWaitMs?: number }).ownerWaitMs);
192+
throw new HistoryBusyError(1);
193+
};
194+
const projectDir = tmpProject();
195+
server = createStudioServer({ projectDir, historyRoot: tmpProject() });
196+
const historyUrl = `/api/projects/${encodeURIComponent(basename(projectDir))}/history`;
197+
await server.app.request(historyUrl);
198+
await server.app.request(historyUrl);
199+
historyState.open = null;
200+
expect(waits).toEqual([undefined, 0]);
201+
await server.shutdown();
202+
});
203+
188204
it("opens a new project's own history once it takes the folder's path", async () => {
189205
const projectDir = tmpProject();
190206
writeFileSync(join(projectDir, "index.html"), "<html>before</html>");

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,17 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
436436
};
437437

438438
// Opened on first use, so a server that never serves Studio's history never writes one. A failed open stays off
439-
// for this run; one another process was holding is tried again on the next request.
439+
// for this run; one another process was holding is tried again on the next request, without waiting for it,
440+
// since every write to the project waits on that open.
441+
let ownerWaitMs: number | undefined;
440442
const histories = historyCache(() =>
441443
openProjectHistory({
442444
projectDir,
443445
historyRoot: options.historyRoot ?? DEFAULT_HISTORY_ROOT,
446+
ownerWaitMs,
444447
}).catch((error: unknown) => {
445448
console.warn(`[studio] Project history is off: ${String(error)}`);
449+
if (error instanceof HistoryBusyError) ownerWaitMs = 0;
446450
if (error instanceof HistoryBusyError || error instanceof HistoryClosedError)
447451
histories.forget(projectDir);
448452
return null;

‎packages/studio-server/src/routes/history.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ describe("history routes", () => {
418418

419419
finishCopy();
420420
const history = (await histories.get(projectDir))!;
421-
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]));
421+
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
422+
timeout: 10_000,
423+
});
422424
await history.flush();
423425
expect(history.list().map((entry) => entry.label)).toEqual(["Color", "Undid: Color"]);
424426
});
@@ -460,7 +462,9 @@ describe("history routes", () => {
460462

461463
mediaCopy.held = null;
462464
const history = (await histories.get(projectDir))!;
463-
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]));
465+
await vi.waitFor(() => expect(history.peek(START)).toHaveProperty(["clip.mp4"]), {
466+
timeout: 10_000,
467+
});
464468
await history.flush();
465469
expect(history.list()).toEqual([]);
466470
});
@@ -475,7 +479,7 @@ describe("history routes", () => {
475479
await vi.waitFor(
476480
async () =>
477481
expect((await (await call("")).json()).entries).toMatchObject([{ label: "Dragged Title" }]),
478-
{ timeout: 2_000, interval: 50 },
482+
{ timeout: 10_000, interval: 50 },
479483
);
480484
const { entry } = await (await call(`/window/${windowId}/close`, {})).json();
481485
expect(entry).toMatchObject({ id: windowId, label: "Dragged Title" });

0 commit comments

Comments
 (0)