Skip to content
TypeScript

fix(server): html previews say how their browser exited (#16872)

Nikita Koynovbd395e9e6530
diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts
index bd322a49291..d3d953364b7 100644
--- a/apps/server/src/htmlRender/HtmlRender.test.ts
+++ b/apps/server/src/htmlRender/HtmlRender.test.ts
@@ -54,6 +54,33 @@ const layerTest = layerHtmlRender();
const PNG_BYTES = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
+/**
+ * A stand-in browser that reads T3's first command off its debugging pipe,
+ * then runs `exit`. Reading it first keeps the pipe from resetting.
+ */
+const exitingBrowser = (exit: string) =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-exiting-browser-" });
+ const executable = path.join(directory, "chrome-headless-shell");
+ yield* fileSystem.writeFileString(
+ executable,
+ [
+ `#!${process.execPath}`,
+ `new (process.getBuiltinModule("node:net").Socket)({ fd: 3 }).on("data", (chunk) => { if (chunk.includes(0)) { ${exit} } });`,
+ ].join("\n"),
+ );
+ yield* fileSystem.chmod(executable, 0o755);
+ return executable;
+ });
+
+const previewError = (executable: string) =>
+ Effect.gen(function* () {
+ const htmlRender = yield* HtmlRender.HtmlRender;
+ return yield* htmlRender.preview({ html: "<p>x</p>" }).pipe(Effect.flip);
+ }).pipe(Effect.provide(layerHtmlRender(executable)));
+
describe("HtmlRender", () => {
it.effect("inlines local images by absolute path and leaves URLs and relative paths alone", () =>
Effect.gen(function* () {
@@ -227,6 +254,37 @@ describe("HtmlRender", () => {
}),
);
+ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "when the browser exits before rendering",
+ () => {
+ it.live("reports its exit code and keeps what it printed out of the message", () =>
+ Effect.gen(function* () {
+ const executable = yield* exitingBrowser(
+ `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);`,
+ );
+
+ const error = yield* previewError(executable);
+
+ expect(error.message).toBe(
+ "Headless Chrome could not render the page: the browser exited unexpectedly (exit code 3).",
+ );
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+
+ it.live("reports the signal that ended it", () =>
+ Effect.gen(function* () {
+ const executable = yield* exitingBrowser(`process.kill(process.pid, "SIGKILL");`);
+
+ const error = yield* previewError(executable);
+
+ expect(error.message).toBe(
+ "Headless Chrome could not render the page: the browser exited unexpectedly (signal SIGKILL).",
+ );
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+ },
+ );
+
it.live(
"screenshots the page in headless Chrome with the requested theme and every console level",
(ctx) =>
diff --git a/apps/server/src/htmlRender/headlessChrome.ts b/apps/server/src/htmlRender/headlessChrome.ts
index 12eeeee066c..5c36cbb1d4d 100644
--- a/apps/server/src/htmlRender/headlessChrome.ts
+++ b/apps/server/src/htmlRender/headlessChrome.ts
@@ -23,10 +23,18 @@ export class HtmlRenderBrowserError extends Schema.TaggedError<HtmlRenderBrowser
reason: Schema.String,
cause: Schema.optional(Schema.Defect()),
output: Schema.optional(Schema.String),
+ exitCode: Schema.optional(Schema.Number),
+ signal: Schema.optional(Schema.String),
},
) {
override get message(): string {
- return `Headless Chrome could not render the page: ${this.reason}.`;
+ const exit =
+ this.exitCode !== undefined
+ ? ` (exit code ${this.exitCode})`
+ : this.signal !== undefined
+ ? ` (signal ${this.signal})`
+ : "";
+ return `Headless Chrome could not render the page: ${this.reason}${exit}.`;
}
}
@@ -238,6 +246,9 @@ const Ignored = Schema.Unknown;
const Navigation = Schema.Struct({ errorText: Schema.optional(Schema.String) });
const Measured = Schema.Struct({ result: Schema.Struct({ value: Schema.Finite }) });
+// Effect's spawner names the signal that ended a child only in its error message.
+const EXIT_SIGNAL = /signal: '(SIG[A-Z0-9]+)'/;
+
interface PageEvents {
readonly mainFrameId: string;
body: Uint8Array | undefined;
@@ -406,9 +417,20 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
// sandbox, says why on stderr, which may still be draining, so give it a moment.
const disconnect = Effect.gen(function* () {
yield* Fiber.await(stderrReader).pipe(Effect.timeout("1 second"), Effect.ignore);
+ const exit = yield* child.exitCode.pipe(
+ Effect.map((code): { exitCode?: number; signal?: string; cause?: unknown } => ({
+ exitCode: code,
+ })),
+ Effect.catch((error) => {
+ const signal = EXIT_SIGNAL.exec(String(error.cause))?.[1];
+ return Effect.succeed(signal === undefined ? { cause: error } : { signal, cause: error });
+ }),
+ Effect.timeoutOption("1 second"),
+ );
const error = new HtmlRenderBrowserError({
reason: "the browser exited unexpectedly",
output: stderrTail,
+ ...Option.getOrElse(exit, () => ({})),
});
disconnected = error;
const waiters = [...pending.values()];