Improve crash handling

This adds a crash handler which reports a crash (if telemetry is enabled). On a
crash, this looks for `curl` (using the PATH env), and forks the process to then
call execve. This relies on a new endpoint to be setup to accept the "report".
Also, we include very little data..I figured just knowing about crashes would
be a good place to start.

A panic handler is provided, which override's Zig default handler and hooks
into the crash handler.

An `assert` function is added and hooks into the crash handler. This is
currently only used in one place (Session.zig) to demonstrate its use. In
addition to reporting a failed assert, the assert aborts execution in
ReleaseFast (as opposed to an undefined behavior with std.debug.assert).

I want to hook this into the v8 global error handler, but only after direct_v8
is merged.

Much of this is inspired by bun's code. They have their own assert (1) and
a [more sophisticated] crashHandler (2).
:

(1) beccd01647/src/bun.zig (L2987)
(2) beccd01647/src/crash_handler.zig (L198)
This commit is contained in:
Karl Seguin
2026-01-12 10:17:28 +08:00
parent 393227a786
commit 0f9c9e2089
6 changed files with 159 additions and 4 deletions

View File

@@ -27,6 +27,9 @@ pub const log = @import("log.zig");
pub const js = @import("browser/js/js.zig");
pub const dump = @import("browser/dump.zig");
pub const build_config = @import("build_config");
pub const crash_handler = @import("crash_handler.zig");
const IS_DEBUG = @import("builtin").mode == .Debug;
pub const FetchOpts = struct {
wait_ms: u32 = 5000,
@@ -69,6 +72,23 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
try writer.flush();
}
pub inline fn assert(ok: bool, comptime ctx: []const u8, args: anytype) void {
if (!ok) {
if (comptime IS_DEBUG) {
unreachable;
}
assertionFailure(ctx, args);
}
}
noinline fn assertionFailure(comptime ctx: []const u8, args: anytype) noreturn {
@branchHint(.cold);
if (@inComptime()) {
@compileError(std.fmt.comptimePrint("assertion failure: " ++ ctx, args));
}
@import("crash_handler.zig").crash(ctx, args, @returnAddress());
}
test {
std.testing.refAllDecls(@This());
}