Skip to content
TypeScript

fix(server): HTML preview browsers quit when done instead of waiting to be killed (#27224)

Julius Marminge2ba2426af640
diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts
index 80d367b2f8a..19163276725 100644
--- a/apps/server/src/htmlRender/HtmlRender.test.ts
+++ b/apps/server/src/htmlRender/HtmlRender.test.ts
@@ -316,6 +316,58 @@ describe("HtmlRender", () => {
},
);
+ // The fake browser is a shebang script, which Windows cannot execute.
+ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "when the browser cannot be signalled",
+ () => {
+ // A browser T3 cannot signal, as under AppArmor in an LXC container, must still exit.
+ it.live("asks the browser to close when the preview finishes instead of signalling it", () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const directory = yield* fileSystem.makeTempDirectoryScoped({
+ prefix: "t3-fake-browser-",
+ });
+ const executable = path.join(directory, "chrome-headless-shell");
+ const exitRecord = path.join(directory, "exited");
+ yield* fileSystem.writeFileString(
+ executable,
+ `#!${process.execPath}
+const fs = require("node:fs");
+process.on("SIGTERM", () => {});
+const quit = () => { fs.writeFileSync(${JSON.stringify(exitRecord)}, "asked to close"); process.exit(0); };
+const reply = (message) => fs.writeSync(4, JSON.stringify(message) + "\\0");
+const results = {
+ "Target.createTarget": { targetId: "page" },
+ "Target.attachToTarget": { sessionId: "session" },
+ "Runtime.evaluate": { result: { value: 100 } },
+ "Page.captureScreenshot": { data: "iVBORw0KGgo=" },
+};
+let buffered = "";
+fs.createReadStream(null, { fd: 3 }).on("end", quit).on("data", (chunk) => {
+ const messages = (buffered + chunk).split("\\0");
+ buffered = messages.pop();
+ for (const { id, method, sessionId } of messages.map((raw) => JSON.parse(raw))) {
+ if (method === "Browser.close") quit();
+ reply({ id, sessionId, result: results[method] ?? {} });
+ if (method === "Page.navigate") reply({ sessionId, method: "Page.loadEventFired", params: {} });
+ }
+});
+`,
+ { mode: 0o755 },
+ );
+
+ yield* HtmlRender.HtmlRender.use((htmlRender) =>
+ htmlRender.preview({ html: "<p>x</p>" }),
+ ).pipe(Effect.provide(layerHtmlRender(executable)));
+
+ const exitedBy = yield* fileSystem.readFileString(exitRecord).pipe(Effect.option);
+ expect(Option.getOrElse(exitedBy, () => "killed by a signal")).toBe("asked to close");
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+ },
+ );
+
it.live("matches preview media queries and text colors to the requested appearance", (ctx) =>
Effect.gen(function* () {
const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV];
diff --git a/apps/server/src/htmlRender/headlessChrome.ts b/apps/server/src/htmlRender/headlessChrome.ts
index aaf64bdcecc..13ce0c68457 100644
--- a/apps/server/src/htmlRender/headlessChrome.ts
+++ b/apps/server/src/htmlRender/headlessChrome.ts
@@ -247,7 +247,7 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
let disconnected: BrowserFailure | undefined;
let nextId = 0;
- const post = (method: string, params: Record<string, unknown>, sessionId: string) =>
+ const post = (method: string, params: Record<string, unknown>, sessionId?: string) =>
Queue.offer(
outgoing,
new TextEncoder().encode(
@@ -374,6 +374,18 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
Effect.forkScoped,
);
+ // Signals can miss a confined browser (AppArmor in LXC), so ask it to quit first.
+ // Runs before the spawn's release, which still signals a browser that stays up.
+ yield* Effect.addFinalizer(() =>
+ disconnected
+ ? Effect.void
+ : post("Browser.close", {}).pipe(
+ Effect.andThen(child.exitCode),
+ Effect.timeout("2 seconds"),
+ Effect.ignore,
+ ),
+ );
+
const send = Effect.fn("headlessChrome.send")(function* <A>(
method: string,
params: Record<string, unknown>,