Expand description
Lifecycle hooks: idempotent export_once + panic/ctrlc/atexit handlers.
Lifecycle orchestration for the auto-export subsystem.
This is module 3 of the 4-module auto-export feature. It provides the
single idempotent [export_once] entry point that every exit path (the
Drop guard, the panic hook, the Ctrl-C handler, and the atexit callback)
funnels into, plus the one-time installation logic for those hooks.
§Why this module is critical for panic = "abort" release builds
memscope-rs release profiles set panic = "abort". Under abort semantics
Rust destructors do NOT run when a panic occurs, so a plain Drop guard
cannot flush a report on panic. The Rust runtime DOES invoke the registered
panic hook before aborting, however, so installing a panic hook that calls
[export_once] is the only mechanism that produces a report on a release
panic. The hook is therefore installed unconditionally (not gated behind a
feature flag) and is carefully re-entrancy-safe: [export_once] swaps
[EXPORTED] to true BEFORE doing any work, so a panic raised by the
export itself cannot cause the re-entrant panic-hook invocation to loop.
§Concurrency model
- [
EXPORTED] is an [AtomicBool] used as a once-only latch. It is set viaswap(true, SeqCst)before the export work begins so that re-entrant invocations (e.g. a panic inside the panic hook) see it already set and return immediately. - [
TRACKER_HANDLE] and [AUTO_EXPORT_CFG] live inparking_lot::RwLock(notOnceLock) so tests can reset them between cases. The exit-path handlers clone the values out and drop the read guards before the potentially slow file I/O, so no lock is held across the export. - [
HOOKS_INSTALLED] is astd::sync::Oncethat guarantees the panic / ctrlc / atexit hooks are registered exactly once per process.
Enums§
- Export
Reason - Which exit path triggered an export. Used by
export_for_reasonto consult the per-path enable flags (on_exit,on_panic) inAutoExportConfigBEFORE consuming the one-shot idempotency latch, so a disabled path does not prevent a later enabled path from exporting.
Functions§
- export_
for_ reason - Reason-aware idempotent export. Like
export_oncebut consults the per-path enable flag inAutoExportConfigbefore consuming the idempotency latch, so a disabled exit path (e.g.on_exit: false) does NOT prevent a later enabled path (e.g.on_panic: true) from exporting. - export_
once - Idempotent export: writes HTML and/or JSON to the configured output path.
Called by the on-demand path (
trigger_export_now, periodic flusher). - install
- Store the tracker + config and install all enabled exit hooks (panic hook
always; ctrlc handler if the
auto-signalfeature is enabled; atexit hook if theatexitfeature is enabled). - snapshot_
json - In-memory JSON snapshot (no disk write). Builds an
AnalysisReportviaAnalyzerand serializes it. Intended for HTTP endpoints that want live metrics without touching the filesystem. - trigger_
export_ now - On-demand export from anywhere in user code. Resets [
EXPORTED] then callsexport_once, so it always exports AND re-arms the exit-path idempotency guard (a later panic/Ctrl-C will produce a fresh report).