Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/cli/src/renderSetupWorker.priority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ vi.mock("node:os", async (importOriginal) => ({
setPriority: os.setPriority,
}));
vi.mock("./utils/lintProject.js", () => ({ lintProject: vi.fn(async () => ({ ok: true })) }));
vi.mock("./browser/installRun.js", () => ({
runBrowserInstall: vi.fn(async () => ({ ok: true })),
}));

beforeEach(() => {
vi.resetModules();
Expand Down Expand Up @@ -39,6 +42,33 @@ it("a render's lint keeps normal priority", async () => {
expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
});

// fails without the fix: at normal priority, each write of the download queues behind a busy PC's work.
it("the Chrome install runs one step above normal priority", async () => {
process.argv[2] = "browser-install";
process.env.HYPERFRAMES_RENDER_SETUP_INPUT = "{}";
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);

await import("./renderSetupWorker.js");

expect(os.setPriority).toHaveBeenCalledWith(constants.priority.PRIORITY_ABOVE_NORMAL);
expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
});

it("the Chrome install still runs when the system refuses the higher priority", async () => {
process.argv[2] = "browser-install";
process.env.HYPERFRAMES_RENDER_SETUP_INPUT = "{}";
os.setPriority.mockImplementation(() => {
throw nodeSystemErrorWithOsCodeInInfo("EACCES");
});
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
const warn = vi.spyOn(process.stderr, "write").mockImplementation(() => true);

await import("./renderSetupWorker.js");

expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
expect(warn).not.toHaveBeenCalled();
});

const nodeSystemErrorWithOsCodeInInfo = (code: string) =>
Object.assign(new Error(`A system error occurred: uv_os_setpriority returned ${code}`), {
code: "ERR_SYSTEM_ERROR",
Expand Down
13 changes: 8 additions & 5 deletions packages/cli/src/renderSetupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ const disposeSignalHandlers = installRenderSetupSignalHandlers(

const SANDBOX_REFUSED_PRIORITY = new Set(["EPERM", "EACCES"]);

function lowerPriority(): void {
function setOwnPriority(priority: number, whatStaysNormal: string): void {
try {
setPriority(constants.priority.PRIORITY_LOW);
setPriority(priority);
} catch (error) {
if (SANDBOX_REFUSED_PRIORITY.has((error as { info?: { code?: string } }).info?.code ?? ""))
return;
process.stderr.write(`Lint runs at normal priority: ${(error as Error).message}\n`);
process.stderr.write(`${whatStaysNormal} at normal priority: ${(error as Error).message}\n`);
}
}

async function runMode(): Promise<unknown> {
if (mode === "browser") return ensureBrowser(input);
if (mode === "browser-install") return runBrowserInstall(input);
if (mode === "browser-install") {
setOwnPriority(constants.priority.PRIORITY_ABOVE_NORMAL, "The Chrome install runs");
return runBrowserInstall(input);
}
if (mode === "lint") {
// A background lint (Studio's, preview's) takes idle CPU only; a render waits on its lint, so that one does not.
if (input.background) lowerPriority();
if (input.background) setOwnPriority(constants.priority.PRIORITY_LOW, "Lint runs");
return lintProject(
input.projectDir,
input.entryFile,
Expand Down
Loading