aboutsummaryrefslogtreecommitdiff
path: root/src/panic_handler.zig
blob: 761c25c3ab1904d74efc11962f16996427bdb5aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const std = @import("std");
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const CLI = @import("./cli.zig").Cli;
const Features = @import("./analytics/analytics_thread.zig").Features;
const HTTP = @import("root").bun.HTTP.AsyncHTTP;
const Report = @import("./report.zig");

pub fn NewPanicHandler(comptime panic_func: fn ([]const u8, ?*std.builtin.StackTrace, ?usize) noreturn) type {
    return struct {
        panic_count: usize = 0,
        skip_next_panic: bool = false,
        log: *bun.logger.Log,

        pub var Singleton: ?*Handler = null;
        const Handler = @This();

        pub fn init(log: *bun.logger.Log) Handler {
            return Handler{
                .log = log,
            };
        }
        pub inline fn handle_panic(msg: []const u8, error_return_type: ?*std.builtin.StackTrace, addr: ?usize) noreturn {
            // This exists to ensure we flush all buffered output before panicking.
            Output.flush();

            Report.fatal(null, msg);

            Output.disableBuffering();

            if (bun.auto_reload_on_crash) {
                // attempt to prevent a double panic
                bun.auto_reload_on_crash = false;

                Output.prettyErrorln("<d>--- Bun is auto-restarting due to crash <d>[time: <b>{d}<r><d>] ---<r>", .{@max(std.time.milliTimestamp(), 0)});
                Output.flush();
                bun.reloadProcess(bun.default_allocator, false);
            }

            // // We want to always inline the panic handler so it doesn't show up in the stacktrace.
            @call(.always_inline, panic_func, .{ msg, error_return_type, addr });
        }
    };
}
ar' /> dave caruso 4-275/+284 2023-08-14Small docs changeGravatar Colin McDonnell 1-2/+2 2023-08-13fix makefile (#4148)Gravatar dave caruso 1-1/+1 2023-08-12Update Stric guides (#4137)Gravatar Revenity 1-4/+6 2023-08-12Fix worker event loop ref/unref + leak (#4114)Gravatar dave caruso 35-263/+465 2023-08-11Fix using uninitialized variable when formatting `config_path`. (#4129)Gravatar Ai Hoshino 2-1/+21 2023-08-11Deprecate loading `node_modules.bun` (#4131)Gravatar Jarred Sumner 34-2204/+222 2023-08-11`bun-wasm` fixes & improvements (#4126)Gravatar jhmaster 14-44/+252 2023-08-11Support TypeScript's `export type * as Foo from 'bar'` (#4125)Gravatar Jarred Sumner 2-9/+42 2023-08-11Fix wasm buildGravatar Jarred Sumner 1-1/+5 2023-08-10async realpath (#4117)Gravatar Jarred Sumner 6-58/+184 2023-08-10Remove assertionGravatar Jarred Sumner 1-3/+0 2023-08-10More lazily initialize these static stringsGravatar Jarred Sumner 1-5/+7 2023-08-10don't check for trailing slash, var streamGravatar Dylan Conway 3-9/+9 2023-08-10run files without extensions (#4113)Gravatar Dylan Conway 4-11/+46 2023-08-10Fix memory leak in base64url (#4111)Gravatar Jarred Sumner 1-2/+2 2023-08-10Handle thundering herd of setInterval (#4109)Gravatar Jarred Sumner 1-2/+17 2023-08-10fs.zig: create temp files with 0o700, not 0o007 (#4107)Gravatar Adhityaa Chandrasekar 1-1/+1 2023-08-10Update remix guideGravatar Colin McDonnell 1-2/+4 2023-08-10zig fmtGravatar Jarred Sumner 1-4/+4 2023-08-10Fixes #4062 (#4106)Gravatar Jarred Sumner 8-26/+87 2023-08-10bun test: format description of test.each (#4092)Gravatar Jacques 3-13/+311 2023-08-10add util.formatWithOptions (#4090)Gravatar dave caruso 3-9/+30 2023-08-10Fix titleGravatar Colin McDonnell 1-1/+1