Skip to content

Commit 00a9b67

Browse files
committed
fix(server): html previews say how their browser exited
1 parent 5fe9d02 commit 00a9b67

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ const layerTest = layerHtmlRender();
5656

5757
const PNG_BYTES = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
5858

59+
/**
60+
* A stand-in browser that reads T3's first command off its debugging pipe,
61+
* then runs `exit`. Reading it first keeps the pipe from resetting.
62+
*/
63+
const exitingBrowser = (exit: string) =>
64+
Effect.gen(function* () {
65+
const fileSystem = yield* FileSystem.FileSystem;
66+
const path = yield* Path.Path;
67+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-exiting-browser-" });
68+
const executable = path.join(directory, "chrome-headless-shell");
69+
yield* fileSystem.writeFileString(
70+
executable,
71+
[
72+
`#!${process.execPath}`,
73+
`new (process.getBuiltinModule("node:net").Socket)({ fd: 3 }).on("data", (chunk) => { if (chunk.includes(0)) { ${exit} } });`,
74+
].join("\n"),
75+
);
76+
yield* fileSystem.chmod(executable, 0o755);
77+
return executable;
78+
});
79+
80+
const previewError = (executable: string) =>
81+
Effect.gen(function* () {
82+
const htmlRender = yield* HtmlRender.HtmlRender;
83+
return yield* htmlRender.preview({ html: "<p>x</p>" }).pipe(Effect.flip);
84+
}).pipe(Effect.provide(layerHtmlRender(executable)));
85+
5986
describe("HtmlRender", () => {
6087
it.effect("inlines local images by absolute path and leaves URLs and relative paths alone", () =>
6188
Effect.gen(function* () {
@@ -234,6 +261,37 @@ describe("HtmlRender", () => {
234261
}),
235262
);
236263

264+
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
265+
"when the browser exits before rendering",
266+
() => {
267+
it.live("reports its exit code and the last line it printed", () =>
268+
Effect.gen(function* () {
269+
const executable = yield* exitingBrowser(
270+
`process.stderr.write("[WARNING:bus.cc] Failed to connect to the bus\\n[FATAL:shared_memory.cc] Check failed: /dev/shm is not writable\\n"); process.exit(3);`,
271+
);
272+
273+
const error = yield* previewError(executable);
274+
275+
expect(error.message).toBe(
276+
"Headless Chrome could not render the page: the browser exited unexpectedly (exit code 3): [FATAL:shared_memory.cc] Check failed: /dev/shm is not writable.",
277+
);
278+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
279+
);
280+
281+
it.live("reports the signal that ended it", () =>
282+
Effect.gen(function* () {
283+
const executable = yield* exitingBrowser(`process.kill(process.pid, "SIGKILL");`);
284+
285+
const error = yield* previewError(executable);
286+
287+
expect(error.message).toBe(
288+
"Headless Chrome could not render the page: the browser exited unexpectedly (signal SIGKILL).",
289+
);
290+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
291+
);
292+
},
293+
);
294+
237295
it.live(
238296
"screenshots the page in headless Chrome with the requested theme and every console level",
239297
(ctx) =>

‎apps/server/src/htmlRender/headlessChrome.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface ConsoleMessage {
3535

3636
const MAX_CONSOLE_MESSAGES = 20;
3737
const MAX_CONSOLE_TEXT_CHARS = 500;
38+
const MAX_EXIT_OUTPUT_CHARS = 300;
3839
const VIEWPORT_HEIGHT = 800;
3940
const MAX_CAPTURE_HEIGHT = 4_000;
4041
const CAPTURE_TIMEOUT = "20 seconds";
@@ -161,6 +162,19 @@ const Ignored = Schema.Unknown;
161162
const Navigation = Schema.Struct({ errorText: Schema.optional(Schema.String) });
162163
const Measured = Schema.Struct({ result: Schema.Struct({ value: Schema.Finite }) });
163164

165+
// Effect's spawner names the signal that ended a child only in its error message.
166+
const EXIT_SIGNAL = /signal: '(\w+)'/;
167+
168+
/** Why the browser went away: its exit code or signal, and the last line it printed. */
169+
const exitReason = (status: string | undefined, output: string) => {
170+
const lastLine = output.trimEnd().split("\n").at(-1)?.trim() ?? "";
171+
const said =
172+
lastLine.length > MAX_EXIT_OUTPUT_CHARS
173+
? `${lastLine.slice(0, MAX_EXIT_OUTPUT_CHARS)}…`
174+
: lastLine;
175+
return `the browser exited unexpectedly${status ? ` (${status})` : ""}${said ? `: ${said}` : ""}`;
176+
};
177+
164178
interface PageEvents {
165179
/** The page's main frame, which only ever shows `PAGE_URL`. */
166180
readonly mainFrameId: string;
@@ -334,8 +348,15 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
334348
// sandbox, says why on stderr, which may still be draining, so give it a moment.
335349
const disconnect = Effect.gen(function* () {
336350
yield* Fiber.await(stderrReader).pipe(Effect.timeout("1 second"), Effect.ignore);
351+
const status = yield* child.exitCode.pipe(
352+
Effect.map((code) => `exit code ${code}`),
353+
Effect.catch((error) =>
354+
Effect.succeed(`signal ${EXIT_SIGNAL.exec(String(error.cause))?.[1] ?? "unknown"}`),
355+
),
356+
Effect.timeoutOption("1 second"),
357+
);
337358
const error = new HtmlRenderBrowserError({
338-
reason: "the browser exited unexpectedly",
359+
reason: exitReason(Option.getOrUndefined(status), stderrTail),
339360
output: stderrTail,
340361
});
341362
disconnected = error;

0 commit comments

Comments
 (0)