@devries-cameron20 / t3code-1 / commits / 2ba2426af6
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

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

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 29, 2026 at 22:07 UTC 2ba2426af640899eee1ca1c544e9321bd2170762
2 files changed +65 -1
apps/server/src/htmlRender/HtmlRender.test.ts
+52
@@ -316,6 +316,58 @@ describe("HtmlRender", () => {
316 },
317 );
318
319 + // The fake browser is a shebang script, which Windows cannot execute.
320 + describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
321 + "when the browser cannot be signalled",
322 + () => {
323 + // A browser T3 cannot signal, as under AppArmor in an LXC container, must still exit.
324 + it.live("asks the browser to close when the preview finishes instead of signalling it", () =>
325 + Effect.gen(function* () {
326 + const fileSystem = yield* FileSystem.FileSystem;
327 + const path = yield* Path.Path;
328 + const directory = yield* fileSystem.makeTempDirectoryScoped({
329 + prefix: "t3-fake-browser-",
330 + });
331 + const executable = path.join(directory, "chrome-headless-shell");
332 + const exitRecord = path.join(directory, "exited");
333 + yield* fileSystem.writeFileString(
334 + executable,
335 + `#!${process.execPath}
336 +const fs = require("node:fs");
337 +process.on("SIGTERM", () => {});
338 +const quit = () => { fs.writeFileSync(${JSON.stringify(exitRecord)}, "asked to close"); process.exit(0); };
339 +const reply = (message) => fs.writeSync(4, JSON.stringify(message) + "\\0");
340 +const results = {
341 + "Target.createTarget": { targetId: "page" },
342 + "Target.attachToTarget": { sessionId: "session" },
343 + "Runtime.evaluate": { result: { value: 100 } },
344 + "Page.captureScreenshot": { data: "iVBORw0KGgo=" },
345 +};
346 +let buffered = "";
347 +fs.createReadStream(null, { fd: 3 }).on("end", quit).on("data", (chunk) => {
348 + const messages = (buffered + chunk).split("\\0");
349 + buffered = messages.pop();
350 + for (const { id, method, sessionId } of messages.map((raw) => JSON.parse(raw))) {
351 + if (method === "Browser.close") quit();
352 + reply({ id, sessionId, result: results[method] ?? {} });
353 + if (method === "Page.navigate") reply({ sessionId, method: "Page.loadEventFired", params: {} });
354 + }
355 +});
356 +`,
357 + { mode: 0o755 },
358 + );
359 +
360 + yield* HtmlRender.HtmlRender.use((htmlRender) =>
361 + htmlRender.preview({ html: "<p>x</p>" }),
362 + ).pipe(Effect.provide(layerHtmlRender(executable)));
363 +
364 + const exitedBy = yield* fileSystem.readFileString(exitRecord).pipe(Effect.option);
365 + expect(Option.getOrElse(exitedBy, () => "killed by a signal")).toBe("asked to close");
366 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
367 + );
368 + },
369 + );
370 +
371 it.live("matches preview media queries and text colors to the requested appearance", (ctx) =>
372 Effect.gen(function* () {
373 const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV];
apps/server/src/htmlRender/headlessChrome.ts
+13 -1
@@ -247,7 +247,7 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
247 let disconnected: BrowserFailure | undefined;
248 let nextId = 0;
249
250 - const post = (method: string, params: Record<string, unknown>, sessionId: string) =>
250 + const post = (method: string, params: Record<string, unknown>, sessionId?: string) =>
251 Queue.offer(
252 outgoing,
253 new TextEncoder().encode(
@@ -374,6 +374,18 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
374 Effect.forkScoped,
375 );
376
377 + // Signals can miss a confined browser (AppArmor in LXC), so ask it to quit first.
378 + // Runs before the spawn's release, which still signals a browser that stays up.
379 + yield* Effect.addFinalizer(() =>
380 + disconnected
381 + ? Effect.void
382 + : post("Browser.close", {}).pipe(
383 + Effect.andThen(child.exitCode),
384 + Effect.timeout("2 seconds"),
385 + Effect.ignore,
386 + ),
387 + );
388 +
389 const send = Effect.fn("headlessChrome.send")(function* <A>(
390 method: string,
391 params: Record<string, unknown>,