From 7d4272fc4fe89b72de6c3f6bf078e4d46b28e89c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 22 Nov 2026 12:29:17 +0000 Subject: [PATCH] js: log an error with its name and message where the engine's stack leaves them out (errorText): on Firefox and Safari a trap on another CPU was logged as its frames alone --- js/core/9p.ts | 5 +++-- js/core/commands.ts | 5 +++-- js/core/cpu.ts | 3 ++- js/core/kernel.ts | 5 +++-- js/core/net.ts | 3 ++- js/core/output.ts | 14 ++++++++++++++ js/core/vsock.ts | 3 ++- js/core/wasi-exec.ts | 5 +++-- js/core/worker-host.ts | 4 ++-- js/web/web-cpu-entry.ts | 3 ++- 10 files changed, 36 insertions(+), 14 deletions(-) diff --git a/js/core/9p.ts b/js/core/9p.ts index 8070e4e..913ba2b 100644 --- a/js/core/9p.ts +++ b/js/core/9p.ts @@ -42,6 +42,7 @@ import type { MaybePromise } from "./block.ts"; import { ERRNO, FS_DEFAULT_OWNER, FsError, type Fs, type FsAttr, type FsDirEntry, type FsFile, type FsFileType, type FsSetAttr } from "./fs.ts"; import type { MountStats } from "./mounts.ts"; import { saveVsockConnection, type Vsock, type VsockConnection, type VsockConnSnapshot, type VsockListener } from "./vsock.ts"; +import { errorText } from "./output.ts"; /** The 9p server's vsock port (the 9P well-known port; kw9p's). */ export const P9_PORT = 564; @@ -369,7 +370,7 @@ class Export implements P9Export { if (open) this.conn = c; else if (this.conn !== c || !this.connected) return; this.connected = open; - try { this.onConnection?.(open); } catch (e) { this.server.log(`[9p] ${this.name}: onConnection threw: ${(e as Error)?.stack ?? e}`); } + try { this.onConnection?.(open); } catch (e) { this.server.log(`[9p] ${this.name}: onConnection threw: ${errorText(e)}`); } } remove(): void { @@ -774,7 +775,7 @@ class Session { #failed(what: string, e: unknown): number { const n = knownErrno(e); - if (!n) this.#log(`${what}: ${(e as Error)?.stack ?? e}`); + if (!n) this.#log(`${what}: ${errorText(e)}`); return n || ERRNO.EIO!; } diff --git a/js/core/commands.ts b/js/core/commands.ts index d9010eb..6d8b218 100644 --- a/js/core/commands.ts +++ b/js/core/commands.ts @@ -32,6 +32,7 @@ import { guestRun } from "./mounts.ts"; import { PtyError, TTY_FLAG_CRITICAL, TTY_SIGNALS, TtyFrameParser, encodeTtyFrame, type ExecResult } from "./tty.ts"; import type { Vsock, VsockConnection, VsockListener } from "./vsock.ts"; +import { errorText } from "./output.ts"; /** The host port kwcmd connects to (guest/src/kwcmd.c KWCMD_PORT). */ export const CMD_PORT = 1020; @@ -275,7 +276,7 @@ async function serve(conn: VsockConnection, commands: ReadonlyMap { serve(conn, this.#commands, this.#log).catch((e) => { - this.#log(`[host] host command: ${(e as Error)?.stack ?? e}`); + this.#log(`[host] host command: ${errorText(e)}`); conn.reset(); }); }); diff --git a/js/core/cpu.ts b/js/core/cpu.ts index dabef70..58216ca 100644 --- a/js/core/cpu.ts +++ b/js/core/cpu.ts @@ -21,6 +21,7 @@ import { ConsoleRing, type ConsoleBatch, type ConsoleFull } from "./console-ring.ts"; import { JitHost } from "./jit.ts"; import { ERRNO, never, yieldToEventLoop } from "./wasi.ts"; +import { errorText } from "./output.ts"; /** What CPU 0's host sends to start a CPU (structured-cloneable). */ export interface CpuInit { @@ -200,7 +201,7 @@ export async function runCpu(init: CpuInit, port: CpuPort): Promise { const trap = (e: unknown) => { if (exited) return; exited = true; - port.post({ t: "trap", error: `CPU ${cpu}: ${(e as Error)?.stack ?? String(e)}` }); + port.post({ t: "trap", error: `CPU ${cpu}: ${errorText(e)}` }); stopCpu0(); }; diff --git a/js/core/kernel.ts b/js/core/kernel.ts index 783ab21..223b4a3 100644 --- a/js/core/kernel.ts +++ b/js/core/kernel.ts @@ -46,6 +46,7 @@ import { VirtioHost } from "./virtio.ts"; import { VirtioGpu, type GpuBackend } from "./virtio-gpu.ts"; import { VsockDevice } from "./vsock.ts"; import { WasiHost, never } from "./wasi.ts"; +import { errorText } from "./output.ts"; const IRQ_CONSOLE = 1; const IRQ_NET = 2; @@ -575,7 +576,7 @@ export class Kernel { this.jitMirrors.push(jitMirror); ep.listen({ message: (m) => this.onCpuMessage(m), - error: (e) => this.fatal(`CPU ${cpu}: ${(e as Error)?.stack ?? e}`), + error: (e) => this.fatal(`CPU ${cpu}: ${errorText(e)}`), }); return 0; } @@ -1128,7 +1129,7 @@ export class Kernel { } onTrap(e: unknown): void { - this.log(`[host] wasm trap: ${(e as Error)?.stack ?? e}`); + this.log(`[host] wasm trap: ${errorText(e)}`); this.exit({ code: 254, reason: String(e), kind: "trap", error: e }); } diff --git a/js/core/net.ts b/js/core/net.ts index 4b952e6..305f1f7 100644 --- a/js/core/net.ts +++ b/js/core/net.ts @@ -21,6 +21,7 @@ // TCP options beyond MSS and window scale (no SACK, no timestamps). import type { Kernel, NetDevice } from "./kernel.ts"; +import { errorText } from "./output.ts"; /** Events a backend reports for one TCP connection. */ export interface NetConnectionEvents { @@ -485,7 +486,7 @@ export class NetStack implements NetDevice { try { this.input(frame); } catch (e) { - this.log?.(`[net] dropped malformed frame: ${(e as Error).stack ?? e}`); + this.log?.(`[net] dropped malformed frame: ${errorText(e)}`); } } diff --git a/js/core/output.ts b/js/core/output.ts index 1da8540..b5917ee 100644 --- a/js/core/output.ts +++ b/js/core/output.ts @@ -71,3 +71,17 @@ export class ConsoleOutput { return () => this.#listeners.delete(listener); } } + +/** + * An error as log text: its stack, led by its name and message where the + * engine's stack leaves them out (Firefox and Safari give the frames alone, + * so "CPU 1: " said nothing of what failed). + */ +export function errorText(e: unknown): string { + const err = e as { name?: unknown; message?: unknown; stack?: unknown } | null | undefined; + if (err === null || typeof err !== "object") return String(e); + const head = `${typeof err.name === "string" && err.name ? err.name : "Error"}: ${typeof err.message === "string" ? err.message : ""}`; + const stack = typeof err.stack === "string" ? err.stack : ""; + if (!stack) return head; + return typeof err.message === "string" && err.message && stack.includes(err.message) ? stack : `${head}\n${stack}`; +} diff --git a/js/core/vsock.ts b/js/core/vsock.ts index c9d1f8c..6ae95be 100644 --- a/js/core/vsock.ts +++ b/js/core/vsock.ts @@ -37,6 +37,7 @@ // (Connection.saveState(), VsockHost.adopt()). import type { MountsSnapshot } from "./mounts.ts"; +import { errorText } from "./output.ts"; /** The host's CID (VMADDR_CID_HOST). */ export const VSOCK_HOST_CID = 2; @@ -823,7 +824,7 @@ class Listener implements VsockListener { if (this.#handler) { const h = this.#handler; queueMicrotask(() => { - try { h(conn); } catch (e) { this.#host.log(`[vsock] listener on port ${this.port} threw: ${(e as Error)?.stack ?? e}`); } + try { h(conn); } catch (e) { this.#host.log(`[vsock] listener on port ${this.port} threw: ${errorText(e)}`); } }); return; } diff --git a/js/core/wasi-exec.ts b/js/core/wasi-exec.ts index 4ba77b4..1b8e071 100644 --- a/js/core/wasi-exec.ts +++ b/js/core/wasi-exec.ts @@ -47,6 +47,7 @@ import { type WasiFromProgram, type WasiProgramEndpoint, } from "./wasi-program.ts"; import { sharedMemoryAvailable } from "./worker-shared.ts"; +import { errorText } from "./output.ts"; export type { WasiFromProgram, WasiProgramEndpoint, WasiProgramPort, WasiToProgram } from "./wasi-program.ts"; @@ -197,7 +198,7 @@ export function serveWasiProgramsWith(vsock: Vsock, opts: WasiExecOptions & Wasi const pool = new WorkerPool(opts.spawn, opts.idleWorkers ?? 2, stats); const shared = opts.sharedMemory ?? wasiSharedChannelAvailable(); const listener: VsockListener = vsock.listen(port, (conn) => { - void runProgram(conn, { log, maxModule, stats, cache, pool, shared }).catch((e) => log(`[wasi] ${(e as Error)?.stack ?? e}`)); + void runProgram(conn, { log, maxModule, stats, cache, pool, shared }).catch((e) => log(`[wasi] ${errorText(e)}`)); }); return { port, @@ -525,7 +526,7 @@ async function runProgram(conn: VsockConnection, ctx: RunContext): Promise ctx.stats.channels++; void serve() .catch((e) => { - ctx.log(`[wasi] ${name}: ${(e as Error)?.stack ?? e}`); + ctx.log(`[wasi] ${name}: ${errorText(e)}`); conn.reset(); end(); }) diff --git a/js/core/worker-host.ts b/js/core/worker-host.ts index 0a481d3..4d561da 100644 --- a/js/core/worker-host.ts +++ b/js/core/worker-host.ts @@ -34,7 +34,7 @@ import type { ProfileReport } from "./profile.ts"; import { moduleIsMemory64 } from "./kernel.ts"; import { BootError, loadBytes, loadKernelModule, type BootFailStage, type KernelSource } from "./load.ts"; import { ConsoleRing, ConsoleRingReader, newConsoleRing } from "./console-ring.ts"; -import { ConsoleOutput } from "./output.ts"; +import { ConsoleOutput, errorText } from "./output.ts"; import { SnapshotCommitError, receivedSnapshot, restoreSnapshotMemory, sentSnapshot, type KernelSnapshot, type SnapshotOptions } from "./snapshot.ts"; import type { FromWorker, ToWorker, WorkerBootSpec, WorkerEndpoint, WorkerExit, WorkerSource } from "./worker-protocol.ts"; @@ -330,7 +330,7 @@ export class WorkerKernel implements KernelHandle { endpoint.listen({ message: (m) => this.#message(m), error: (e) => { - const msg = (e as Error)?.stack ?? String(e); + const msg = errorText(e); this.#log(`[host] kernel worker error: ${msg}`); this.#finish({ code: 255, reason: `kernel worker error: ${(e as Error)?.message ?? e}`, kind: "fatal", error: e }); }, diff --git a/js/web/web-cpu-entry.ts b/js/web/web-cpu-entry.ts index 9a20d91..7dac1d4 100644 --- a/js/web/web-cpu-entry.ts +++ b/js/web/web-cpu-entry.ts @@ -6,6 +6,7 @@ // build bundles it into a module that imports nothing. import { runCpu, type CpuInit, type CpuMessage } from "../core/cpu.ts"; +import { errorText } from "../core/output.ts"; /** The dedicated worker's global (the page's tsconfig has no webworker lib). */ const scope = globalThis as unknown as { @@ -17,5 +18,5 @@ scope.addEventListener("message", (e) => { const post = (msg: CpuMessage, transfer?: ArrayBuffer[]) => scope.postMessage(msg, transfer); // runCpu() reports its own failures; this catches anything it did not // (a browser's worker reports no unhandled rejection to its owner). - runCpu(init, { post }).catch((err: unknown) => post({ t: "fatal", msg: `CPU ${init.cpu}: ${(err as Error)?.stack ?? err}` })); + runCpu(init, { post }).catch((err: unknown) => post({ t: "fatal", msg: `CPU ${init.cpu}: ${errorText(err)}` })); }, { once: true });