aboutsummaryrefslogtreecommitdiff
path: root/src/panic_handler.zig
blob: 4e2ca05f97ea39f27c31c96a0eb76429791cb57a (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("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("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 });
        }
    };
}
e526ce9e020808&follow=1'>Add File to binary data TOC (#6025)Gravatar Aaron Dewes 1-3/+3 2023-10-10docs: rearranged cli/runtime related sections (#6275)Gravatar cyfung1031 8-305/+110 2023-10-10Update inspector-protocolGravatar Ashcon Partovi 3-99/+357 2023-10-10Update debug-adapter-protocolGravatar Ashcon Partovi 3-11/+33 2023-10-10Add missing ws declarations (#6307)Gravatar Vasilis Themelis 1-0/+4 2023-10-10Update vite.md (#6399)Gravatar Clément P 1-2/+1 2023-10-10Documentation for the IPC of Bun.spawn (#6400)Gravatar Nicolae-Rares Ailincai 3-0/+122 2023-10-09fix(AbortSignal/fetch) fix AbortSignal.timeout, fetch lock behavior and fetch...Gravatar Ciro Spaciari 29-61/+303 2023-10-09Fix npm tag for canary bun-types, againGravatar Ashcon Partovi 2-56/+10 2023-10-09Add Fedora build instructions to development.md (#6359)Gravatar otterDeveloper 1-0/+10 2023-10-09added commands (#6314)Gravatar babar 1-1/+2 2023-10-09Update README.md (#6291)Gravatar TPLJ 1-1/+1 2023-10-09docs: fixing a couple typos (#6331)Gravatar Michael Di Prisco 2-2/+2 2023-10-09fix: support uint8 exit code range (#6303)Gravatar Liz 2-2/+11 2023-10-09Fix array variables preview in debugger (#6379)Gravatar 2hu 1-1/+4 2023-10-07feat(KeyObject) (#5940)Gravatar Ciro Spaciari 106-67/+9342 2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+1 2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+2 2023-10-07Update settings.jsonGravatar Jarred Sumner 1-1/+2 2023-10-07Update settings.jsonGravatar Jarred Sumner 1-2/+3 2023-10-06fix a couple install testsGravatar Dylan Conway 1-8/+8 2023-10-06formatGravatar Dylan Conway 1-1/+2 2023-10-06Fix memory leak in fetch() (#6350)Gravatar Jarred Sumner 1-2/+0 2023-10-06[types] allow onLoad plugin callbacks to return undefined (#6346)Gravatar Silver 1-1/+1 2023-10-06docs: `file.stream()` is not a promise (#6337)Gravatar Paul Nodet 1-1/+1