mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(lint): run Studio and producer lint off the host's own thread (#7974)
* fix(lint): run Studio and producer lint off the host's own thread A composition that sends the linter's parser into a runaway froze or crashed the long-lived hosts that lint it in their own process. CLI Studio's single-page lint now runs in a child process with a heap cap and a 15 s limit. Producer /lint (runHyperframeLint) runs on a worker thread with the same limits. A page that times out, runs out of memory, or whose child or thread ends without an answer gets one lint_aborted error finding and is never linted in the host process. Studio's project lint keeps its timeout and gains a heap cap. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(producer): cap concurrent /lint workers at two Each lint worker can hold up to 512 MB, so parallel /lint requests multiplied that without bound. At most two run at once; the rest wait in order, and a request still waiting when its own 15 s runs out gets the lint_aborted result. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
95e4e7f880
commit
778a005641
@@ -210,17 +210,19 @@ function tsxLoaderFromCliInstall(): string {
|
||||
}
|
||||
|
||||
export async function runRenderSetupWorker<T>(
|
||||
mode: "browser" | "browser-install" | "lint" | "orphan-cleanup",
|
||||
mode: "browser" | "browser-install" | "lint" | "lint-html" | "orphan-cleanup",
|
||||
input: unknown,
|
||||
options: CancellableProcessOptions,
|
||||
{ execArgv: nodeFlags = [], ...options }: CancellableProcessOptions & { execArgv?: string[] },
|
||||
): Promise<T> {
|
||||
const sourceMode = import.meta.url.endsWith(".ts");
|
||||
const workerUrl = new URL(
|
||||
sourceMode ? "../renderSetupWorker.ts" : "./renderSetupWorker.js",
|
||||
import.meta.url,
|
||||
);
|
||||
const execArgv =
|
||||
sourceMode && !process.versions.bun ? ["--import", tsxLoaderFromCliInstall()] : [];
|
||||
const execArgv = [
|
||||
...nodeFlags,
|
||||
...(sourceMode && !process.versions.bun ? ["--import", tsxLoaderFromCliInstall()] : []),
|
||||
];
|
||||
let result: CancellableProcessResult;
|
||||
try {
|
||||
result = await runCancellableProcess(
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { lintHyperframeHtml } from "@hyperframes/lint";
|
||||
import { runCancellableProcess, runRenderSetupWorker } from "./cancellableProcess.js";
|
||||
import { lintHtmlOutOfProcess } from "./lintHtmlWorker.js";
|
||||
|
||||
const PAGE = `<html><body><div id="root" data-composition-id="main" data-width="1920" data-height="1080"></div></body></html>`;
|
||||
const OPTS = { filePath: "index.html", host: "studio" as const };
|
||||
const withoutTimings = ({ timings: _timings, ...rest }: Record<string, unknown>) => rest;
|
||||
|
||||
describe("lintHtmlOutOfProcess", () => {
|
||||
// The second page is past the ~128 KB an env var can carry, so it only lints if the page travels in a file.
|
||||
it.each([PAGE, PAGE.replace("</body>", `<!-- ${"x".repeat(300_000)} -->\n</body>`)])(
|
||||
"lints a %#-th page in a child to what the linter returns in process",
|
||||
async (html) => {
|
||||
let pageDir = "";
|
||||
const run = ((mode, input, options) => {
|
||||
pageDir = dirname((input as { htmlPath: string }).htmlPath);
|
||||
return runRenderSetupWorker(mode, input, options);
|
||||
}) as typeof runRenderSetupWorker;
|
||||
const result = await lintHtmlOutOfProcess(html, OPTS, { run });
|
||||
expect(withoutTimings({ ...result })).toEqual(
|
||||
withoutTimings({ ...(await lintHyperframeHtml(html, OPTS)) }),
|
||||
);
|
||||
expect(existsSync(pageDir)).toBe(false);
|
||||
},
|
||||
30_000,
|
||||
);
|
||||
|
||||
it("answers a child that never answers with the lint_aborted error, and does not lint here", async () => {
|
||||
// A real child that never exits, stopped by the runner's own time limit.
|
||||
const hang = ((_mode, _input, options) =>
|
||||
runCancellableProcess(process.execPath, ["--eval", "setInterval(() => {}, 1000)"], {
|
||||
timeoutMs: options.timeoutMs,
|
||||
})) as typeof runRenderSetupWorker;
|
||||
const began = performance.now();
|
||||
const result = await lintHtmlOutOfProcess(PAGE, OPTS, { run: hang, timeoutMs: 300 });
|
||||
expect(performance.now() - began).toBeLessThan(10_000);
|
||||
expect([result.ok, result.errorCount, result.warningCount, result.infoCount]).toEqual([
|
||||
false,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
]);
|
||||
expect(result.findings.map(({ code, severity, file }) => [code, severity, file])).toEqual([
|
||||
["lint_aborted", "error", "index.html"],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { HyperframeLinterOptions, HyperframeLintResult } from "@hyperframes/lint";
|
||||
import { runRenderSetupWorker } from "./cancellableProcess.js";
|
||||
|
||||
/** A real composition lints in about a second at a few tens of MB of heap; this is where a runaway is stopped. */
|
||||
const LINT_HTML_HEAP_MB = 512;
|
||||
const LINT_HTML_TIMEOUT_MS = 15_000;
|
||||
|
||||
type LintHtmlTestHooks = { run?: typeof runRenderSetupWorker; timeoutMs?: number };
|
||||
|
||||
/** The linter's own result shape for a page it could not check: one error, never linted in this process. */
|
||||
function abortedLintResult(
|
||||
opts: HyperframeLinterOptions | undefined,
|
||||
afterMs: number,
|
||||
): HyperframeLintResult {
|
||||
return {
|
||||
ok: false,
|
||||
errorCount: 1,
|
||||
warningCount: 0,
|
||||
infoCount: 0,
|
||||
findings: [
|
||||
{
|
||||
code: "lint_aborted",
|
||||
severity: "error",
|
||||
message:
|
||||
"This composition's script could not be checked: linting it ran too long or out of memory, so it was stopped. A very large or deeply nested script is the usual cause.",
|
||||
...(opts?.filePath && { file: opts.filePath }),
|
||||
},
|
||||
],
|
||||
timings: { totalMs: afterMs, groupMs: {}, slowestRule: "", slowestRuleMs: 0 },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Lints one page in a child process with a heap cap and a time limit, so a long-lived host (Studio)
|
||||
* survives a page that sends the parser into a runaway: that page's lint is the `lint_aborted`
|
||||
* error. The child timing out, dying or exiting without a result never falls back to linting here.
|
||||
*/
|
||||
export async function lintHtmlOutOfProcess(
|
||||
html: string,
|
||||
opts: HyperframeLinterOptions | undefined,
|
||||
{ run = runRenderSetupWorker, timeoutMs = LINT_HTML_TIMEOUT_MS }: LintHtmlTestHooks = {},
|
||||
): Promise<HyperframeLintResult> {
|
||||
// The runner hands its input over in an env var, which a large page would overflow: the page goes in a file.
|
||||
const dir = await mkdtemp(join(tmpdir(), "hf-lint-"));
|
||||
const htmlPath = join(dir, "page.html");
|
||||
try {
|
||||
await writeFile(htmlPath, html);
|
||||
const began = performance.now();
|
||||
try {
|
||||
return await run<HyperframeLintResult>(
|
||||
"lint-html",
|
||||
{ htmlPath, opts },
|
||||
{
|
||||
execArgv: [`--max-old-space-size=${LINT_HTML_HEAP_MB}`],
|
||||
maxBufferBytes: 8 * 1024 * 1024,
|
||||
timeoutMs,
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
return abortedLintResult(opts, Math.round(performance.now() - began));
|
||||
}
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user