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

This commit is contained in:
Pooya Parsa
2026-11-22 12:29:17 +00:00
parent ef3486551a
commit 7d4272fc4f
10 changed files with 36 additions and 14 deletions

View File

@@ -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!;
}

View File

@@ -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<string, HostCo
if (!gone) {
await drain();
direct(CMD_STDERR, `${name}: ${(e as Error)?.message ?? e}\n`);
log(`[host] command ${name} failed: ${(e as Error)?.stack ?? e}`);
log(`[host] command ${name} failed: ${errorText(e)}`);
}
}
await drain();
@@ -365,7 +366,7 @@ export class CommandTable {
try {
this.#listener = vsock.listen(CMD_PORT, (conn) => {
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();
});
});

View File

@@ -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<void> {
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();
};

View File

@@ -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 });
}

View File

@@ -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)}`);
}
}

View File

@@ -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: <frame>" 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}`;
}

View File

@@ -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;
}

View File

@@ -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<void>
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();
})

View File

@@ -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 });
},

View File

@@ -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 });