Module panic_hooks

Source
Expand description

Provides a central registry for safe runtime registration and de-registration of panic hooks.

Registered hooks are called in an arbitrary order.

This is used inside turbo-tasks-backend to invalidate the persistent cache if a panic occurs anywhere inside of Turbopack. That panic hook must be dynamically registered as it contains a reference to the database.

The program using turbo-tasks must call std::panic::set_hook with handle_panic exactly once for these registered panic handlers to function. Short-lived programs or code that does not fully control its execution environment (like unit tests) may choose not to do this, so these panic hooks are best-effort.

It’s recommended that when adding this global panic handler (or any other panic handler) that:

  • You call it as early in the program as possible, to avoid race conditions with other threads.
  • The new panic handler should call any existing panic handler.
use std::panic::{set_hook, take_hook};
use turbo_tasks::panic_hooks::handle_panic;

let prev_hook = take_hook();
set_hook(Box::new(move |info| {
    handle_panic(info);
    prev_hook(info);
}));

This code is not particularly well-optimized under the assumption that panics are a rare occurrence.

Structs§

PanicHookGuard
A guard returned from register_panic_hook that cleans up the panic hook when dropped.

Functions§

handle_panic
This function should be registered as the global panic handler using std::panic::set_hook. See the module-level documentation for usage examples.
register_panic_hook
Registers a hook to be called when a panic occurs. Panic hooks are called in the order that they are registered. Dropping the returned PanicHookGuard removes the registered hook.

Type Aliases§

ArcPanicHook
PanicHook