Skip to content

Commit 7e00f37

Browse files
fix(cli): render starts in a sandbox that forbids lowering CPU priority (#5814)
1 parent d0da232 commit 7e00f37

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { constants } from "node:os";
2-
import { afterEach, expect, it, vi } from "vitest";
2+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
33

44
const os = vi.hoisted(() => ({ setPriority: vi.fn() }));
55
vi.mock("node:os", async (importOriginal) => ({
@@ -8,15 +8,33 @@ vi.mock("node:os", async (importOriginal) => ({
88
}));
99
vi.mock("./utils/lintProject.js", () => ({ lintProject: vi.fn(async () => ({ ok: true })) }));
1010

11+
beforeEach(() => {
12+
vi.resetModules();
13+
os.setPriority.mockReset();
14+
process.argv[2] = "lint";
15+
process.env.HYPERFRAMES_RENDER_SETUP_INPUT = JSON.stringify({ projectDir: "/project" });
16+
});
1117
afterEach(() => vi.restoreAllMocks());
1218

1319
it("lints at the lowest CPU priority so it never competes with a Studio boot", async () => {
14-
process.argv[2] = "lint";
15-
process.env.HYPERFRAMES_RENDER_SETUP_INPUT = JSON.stringify({ projectDir: "/project" });
1620
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
1721

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

2024
expect(os.setPriority).toHaveBeenCalledWith(constants.priority.PRIORITY_LOW);
2125
expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
2226
});
27+
28+
it("still lints when a sandbox forbids lowering the priority", async () => {
29+
os.setPriority.mockImplementation(() => {
30+
throw new Error("uv_os_setpriority returned EPERM (operation not permitted)");
31+
});
32+
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
33+
const warn = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
34+
35+
await import("./renderSetupWorker.js");
36+
37+
expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
38+
expect(warn).toHaveBeenCalledTimes(1);
39+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("EPERM"));
40+
});

‎packages/cli/src/renderSetupWorker.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ const disposeSignalHandlers = installRenderSetupSignalHandlers(
1818
process.env.HYPERFRAMES_RENDER_DETACHED !== "1",
1919
);
2020

21+
function lowerPriority(): void {
22+
try {
23+
setPriority(constants.priority.PRIORITY_LOW);
24+
} catch (error) {
25+
process.stderr.write(`Lint runs at normal priority: ${(error as Error).message}\n`);
26+
}
27+
}
28+
2129
async function runMode(): Promise<unknown> {
2230
if (mode === "browser") return ensureBrowser(input);
2331
if (mode === "lint") {
2432
// Nobody waits on a background lint the way they wait on a Studio boot, so it takes idle CPU only.
25-
setPriority(constants.priority.PRIORITY_LOW);
33+
lowerPriority();
2634
return lintProject(
2735
input.projectDir,
2836
input.entryFile,

0 commit comments

Comments
 (0)