mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(studio-server): a preview file request no longer waits on a silent drive on the event loop (#8254)
* test(studio-server): a preview file request on a silent drive asks it nothing on the event loop * fix(studio-server): a preview file request reads its project off the event loop * ci(studio-server): refuse a new synchronous fs call in Studio's routes and history engine * fix(studio-server): a preview file request and a miss stay off the event loop to the end * test(studio-server): a shared still's remake no longer depends on busy requests' slot order * fix(ci): the studio-server sync fs gate counts every sync call node:fs has, globSync included
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
// Studio's routes and history engine run on the host's event loop (the CLI's preview server, the desktop app's main
|
||||
// process): a synchronous read of a folder whose drive stopped answering holds that loop for as long as the drive is
|
||||
// silent. Each file may only lose such calls, never gain one; its count is kept in the baseline beside this script.
|
||||
import * as fs from "node:fs";
|
||||
import { readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
||||
import { join, relative, resolve, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const ROOT = join(import.meta.dirname, "..");
|
||||
const AREAS = ["packages/studio-server/src/routes", "packages/studio-server/src/history"];
|
||||
const BASELINE = join(import.meta.dirname, "studio-server-sync-fs-baseline.json");
|
||||
// Every synchronous call the running node:fs has (`globSync` and any later one), `realpathSync.native` included.
|
||||
const SYNC_NAMES = Object.keys(fs).filter((name) => name.endsWith("Sync"));
|
||||
const SYNC_CALL = new RegExp(`\\b(?:${SYNC_NAMES.join("|")})(?:\\.native)?\\s*\\(`, "g");
|
||||
|
||||
/** How many synchronous fs calls `source` makes, comment lines aside. @param {string} source */
|
||||
export function countSyncCalls(source) {
|
||||
return source
|
||||
.split("\n")
|
||||
.filter((line) => !/^\s*(\/\/|\*|\/\*)/.test(line))
|
||||
.reduce((sum, line) => sum + (line.match(SYNC_CALL)?.length ?? 0), 0);
|
||||
}
|
||||
|
||||
/** @param {string} dir @returns {string[]} */
|
||||
function sourceFiles(dir) {
|
||||
return readdirSync(dir).flatMap((name) => {
|
||||
const path = join(dir, name);
|
||||
if (statSync(path).isDirectory()) return name === "__fixtures__" ? [] : sourceFiles(path);
|
||||
return /\.ts$/.test(name) && !/\.test\.ts$/.test(name) ? [path] : [];
|
||||
});
|
||||
}
|
||||
|
||||
/** Each area file's count, by its path from the repo root. */
|
||||
export function currentCounts(root = ROOT) {
|
||||
return Object.fromEntries(
|
||||
AREAS.flatMap((area) => sourceFiles(join(root, area)))
|
||||
.map((path) => [
|
||||
relative(root, path).split(sep).join("/"),
|
||||
countSyncCalls(readFileSync(path, "utf8")),
|
||||
])
|
||||
.filter(([, count]) => count > 0)
|
||||
.sort(([a], [b]) => a.localeCompare(b)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The files that gained a synchronous fs call over the baseline. */
|
||||
export function gained(current, baseline) {
|
||||
return Object.entries(current)
|
||||
.filter(([path, count]) => count > (baseline[path] ?? 0))
|
||||
.map(
|
||||
([path, count]) => `${path}: ${count} synchronous fs calls, ${baseline[path] ?? 0} allowed`,
|
||||
);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const current = currentCounts();
|
||||
if (process.argv.includes("--write")) {
|
||||
writeFileSync(BASELINE, `${JSON.stringify(current, null, 2)}\n`);
|
||||
return;
|
||||
}
|
||||
const problems = gained(current, JSON.parse(readFileSync(BASELINE, "utf8")));
|
||||
if (problems.length === 0) return;
|
||||
console.error(
|
||||
"Studio's routes and history engine read the disk with fs/promises, never a *Sync call:\n" +
|
||||
problems.map((line) => ` ${line}`).join("\n"),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();
|
||||
@@ -0,0 +1,32 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import * as fs from "node:fs";
|
||||
import { countSyncCalls, gained } from "./check-studio-server-sync-fs.mjs";
|
||||
|
||||
test("counts synchronous fs calls a file makes, not the ones a comment names", () => {
|
||||
const source = [
|
||||
"const stat = existsSync(file) ? statSync(file) : null;",
|
||||
"// readFileSync(file) would hold the event loop",
|
||||
"const text = await readFile(file);",
|
||||
"const names = readdirSync (dir);",
|
||||
"const real = realpathSync.native(dir);",
|
||||
"readSync(fd, buffer); writeSync(fd, buffer); fstatSync(fd);",
|
||||
].join("\n");
|
||||
assert.equal(countSyncCalls(source), 7);
|
||||
});
|
||||
|
||||
test("a file may lose synchronous fs calls, never gain one, and a new file starts at none", () => {
|
||||
const baseline = { "routes/preview.ts": 4 };
|
||||
assert.deepEqual(gained({ "routes/preview.ts": 3 }, baseline), []);
|
||||
assert.deepEqual(gained({ "routes/preview.ts": 5, "routes/new.ts": 1 }, baseline), [
|
||||
"routes/preview.ts: 5 synchronous fs calls, 4 allowed",
|
||||
"routes/new.ts: 1 synchronous fs calls, 0 allowed",
|
||||
]);
|
||||
});
|
||||
|
||||
// fails without the fix: the gate kept its own list of names, so a new route calling Node 22's globSync passed.
|
||||
test("every synchronous call node:fs has is counted, globSync included", () => {
|
||||
const names = Object.keys(fs).filter((name) => name.endsWith("Sync"));
|
||||
assert.ok(names.includes("globSync"), "this Node has globSync");
|
||||
for (const name of names) assert.equal(countSyncCalls(`${name}(x);`), 1, name);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"packages/studio-server/src/history/artifactCleanup.ts": 7,
|
||||
"packages/studio-server/src/history/containedPath.ts": 9,
|
||||
"packages/studio-server/src/history/historyId.ts": 8,
|
||||
"packages/studio-server/src/history/historyLog.ts": 5,
|
||||
"packages/studio-server/src/history/ownerLock.ts": 20,
|
||||
"packages/studio-server/src/history/projectHistory.ts": 21,
|
||||
"packages/studio-server/src/history/pruneHistories.ts": 6,
|
||||
"packages/studio-server/src/history/reclaimUncopied.ts": 1,
|
||||
"packages/studio-server/src/history/snapshotRuns.ts": 24,
|
||||
"packages/studio-server/src/history/videoState.ts": 5,
|
||||
"packages/studio-server/src/routes/files.ts": 59,
|
||||
"packages/studio-server/src/routes/fonts.ts": 4,
|
||||
"packages/studio-server/src/routes/freezeFrame.ts": 7,
|
||||
"packages/studio-server/src/routes/globalAssets.ts": 2,
|
||||
"packages/studio-server/src/routes/lint.ts": 1,
|
||||
"packages/studio-server/src/routes/loudness.ts": 1,
|
||||
"packages/studio-server/src/routes/media.ts": 3,
|
||||
"packages/studio-server/src/routes/peaks.ts": 1,
|
||||
"packages/studio-server/src/routes/preview.ts": 7,
|
||||
"packages/studio-server/src/routes/recoveryDrafts.ts": 5,
|
||||
"packages/studio-server/src/routes/render.ts": 12,
|
||||
"packages/studio-server/src/routes/thumbnail.ts": 11,
|
||||
"packages/studio-server/src/routes/waveform.ts": 1
|
||||
}
|
||||
Reference in New Issue
Block a user