Skip to content

Commit 1468e5c

Browse files
committed
fix(studio-server): a restore waits for the preview's reads to close before replacing the file
1 parent 0c8c4b2 commit 1468e5c

4 files changed

Lines changed: 17 additions & 13 deletions

File tree

‎packages/core/src/renameWithRetry.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ interface Options {
1313
copyFallback?: boolean;
1414
}
1515

16-
/** Windows refusing a file another process holds open, which a moment's wait may clear. */
1716
export function isWindowsHold(error: unknown, platform = process.platform): boolean {
1817
if (platform !== "win32") return false;
1918
return RETRYABLE.has((error as NodeJS.ErrnoException | undefined)?.code ?? "");
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { ReadStream } from "node:fs";
22
import { resolve } from "node:path";
33

4-
// Windows refuses to replace a file while any read of it is open, and a loaded <video> keeps its read open.
54
const serving = new Map<string, Set<ReadStream>>();
65
const keyOf = (path: string) =>
76
process.platform === "win32" ? resolve(path).toLowerCase() : resolve(path);
87

9-
/** Tracks a read the preview serves from `path` until it closes. */
108
export function serveWhileOpen(path: string, stream: ReadStream): ReadStream {
119
const key = keyOf(path);
1210
const reads = serving.get(key) ?? new Set();
@@ -18,7 +16,17 @@ export function serveWhileOpen(path: string, stream: ReadStream): ReadStream {
1816
return stream;
1917
}
2018

21-
/** Ends the preview's open reads of `path` so it can be replaced; the player asks again for the new bytes. */
22-
export function stopServing(path: string): void {
23-
for (const stream of serving.get(keyOf(path)) ?? []) stream.destroy();
19+
// Windows refuses to replace a file while a read of it is open, and a loaded <video> keeps its read open.
20+
export async function stopServingAndClose(path: string): Promise<void> {
21+
const reads = [...(serving.get(keyOf(path)) ?? [])];
22+
await Promise.all(
23+
reads.map(
24+
(stream) =>
25+
new Promise<void>((done) => {
26+
if (stream.closed) return done();
27+
stream.once("close", () => done());
28+
stream.destroy();
29+
}),
30+
),
31+
);
2432
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe("a restore of a file the preview is serving", () => {
138138
read.pause();
139139
let openAtReplace = true;
140140
vi.mocked(fs.renameSync).mockImplementation((from, to) => {
141-
openAtReplace = !read.destroyed;
141+
openAtReplace = !read.closed;
142142
return realRenameSync(from, to);
143143
});
144144

‎packages/studio-server/src/history/blobStore.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
renameWithRetry,
1010
renameWithRetryAsync,
1111
} from "@hyperframes/core/rename-retry";
12-
import { stopServing } from "../helpers/servedFiles.js";
12+
import { stopServingAndClose } from "../helpers/servedFiles.js";
1313

1414
/** File contents stored once by sha256, text and binary alike. */
1515
export interface BlobStore {
@@ -53,12 +53,10 @@ async function cloneOrStream(from: string, to: string): Promise<void> {
5353
}
5454
}
5555

56-
/** How a restore Windows would not rename into place went: written and checked, written wrong, or not written. */
5756
export type InPlaceOutcome = "restored" | "mismatch" | "failed";
5857
type InPlaceListener = (absPath: string, outcome: InPlaceOutcome) => void;
5958
const inPlaceListeners = new Set<InPlaceListener>();
6059

61-
/** Hears each restore that had to write a held file in place on Windows. */
6260
export function onRestoredInPlace(listener: InPlaceListener): () => void {
6361
inPlaceListeners.add(listener);
6462
return () => inPlaceListeners.delete(listener);
@@ -75,7 +73,7 @@ async function cloneOrCopy(
7573
try {
7674
await copyFile(from, temp, constants.COPYFILE_FICLONE);
7775
beforeReplace?.();
78-
stopServing(to);
76+
await stopServingAndClose(to);
7977
try {
8078
renameWithRetry(temp, to);
8179
} catch (error) {
@@ -87,8 +85,7 @@ async function cloneOrCopy(
8785
}
8886
}
8987

90-
/** Windows still refuses the rename after its retry: something outside the app holds the file. Its bytes go in place,
91-
* synced, then checked against the blob; a mismatch fails the restore and the blob stays. */
88+
// Something outside the app still holds the file: write in place, sync, and fail on any mismatch with the blob.
9289
async function writeInPlace(temp: string, to: string, hash: string): Promise<void> {
9390
let outcome: InPlaceOutcome = "failed";
9491
try {

0 commit comments

Comments
 (0)