Skip to main content

Module lifecycle

Module lifecycle 

Source
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 via swap(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 in parking_lot::RwLock (not OnceLock) 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 a std::sync::Once that guarantees the panic / ctrlc / atexit hooks are registered exactly once per process.

Enums§

ExportReason
Which exit path triggered an export. Used by export_for_reason to consult the per-path enable flags (on_exit, on_panic) in AutoExportConfig BEFORE 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_once but consults the per-path enable flag in AutoExportConfig before 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-signal feature is enabled; atexit hook if the atexit feature is enabled).
snapshot_json
In-memory JSON snapshot (no disk write). Builds an AnalysisReport via Analyzer and 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 calls export_once, so it always exports AND re-arms the exit-path idempotency guard (a later panic/Ctrl-C will produce a fresh report).