Skip to content

Commit 8dfa121

Browse files
committed
fix(studio-server): a long source's in-progress piece counts as the owner's progress
1 parent 1ffd9dd commit 8dfa121

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎packages/studio-server/src/helpers/proxyLock.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ const within = <T>(work: Promise<T>, ms: number) =>
2727
]);
2828

2929
describe("acquireCopyLock", () => {
30+
it("keeps waiting while the owner writes a piece of a long source", async () => {
31+
const cachePath = staleLock("c.mp4");
32+
writeFileSync(join(dir, ".tmp-1234-.part-c-0.mp4"), "x");
33+
const abort = new AbortController();
34+
35+
const waiting = acquireCopyLock(cachePath, abort.signal, 60_000);
36+
setTimeout(() => abort.abort(new Error("stopped")), 400);
37+
38+
await expect(within(waiting, 3_000)).rejects.toThrow("stopped");
39+
});
40+
3041
it("gives up on a live owner that has made no progress for the stall window, naming the copy", async () => {
3142
const cachePath = staleLock("a.mp4");
3243

‎packages/studio-server/src/helpers/proxyLock.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,18 @@ export class CopyLockStalledError extends Error {
2626
}
2727
}
2828

29-
/** The newest change among the lock and the owner's temp and piece files: how recently the owner did anything. */
3029
function lastActivityMs(cachePath: string, lockPath: string): number {
3130
const name = basename(cachePath);
3231
const stem = basename(cachePath, extname(cachePath));
3332
const mine = (file: string) =>
3433
file === basename(lockPath) ||
35-
(file.startsWith(".tmp-") && file.endsWith(`-${name}`)) ||
34+
(file.startsWith(".tmp-") && (file.endsWith(`-${name}`) || file.includes(`-.part-${stem}-`))) ||
3635
file.startsWith(`.part-${stem}-`);
3736
let newest = 0;
3837
for (const file of readdirSync(dirname(cachePath))) {
3938
if (!mine(file)) continue;
40-
try {
41-
newest = Math.max(newest, statSync(join(dirname(cachePath), file)).mtimeMs);
42-
} catch {
43-
// gone between the listing and the stat
44-
}
39+
const changed = statSync(join(dirname(cachePath), file), { throwIfNoEntry: false })?.mtimeMs;
40+
newest = Math.max(newest, changed ?? 0);
4541
}
4642
return newest;
4743
}
@@ -77,8 +73,7 @@ function pause(ms: number, signal: AbortSignal): Promise<void> {
7773
});
7874
}
7975

80-
/** The right to make the copy at `cachePath`; null once another process finished it. Takes over a dead owner's lock,
81-
* and gives up on a live one that has changed nothing for `stallMs` (a hung or recycled-pid owner). */
76+
/** The right to make the copy at `cachePath`; null once another process finished it; throws on a stalled live owner. */
8277
export async function acquireCopyLock(
8378
cachePath: string,
8479
signal: AbortSignal,

0 commit comments

Comments
 (0)