Skip to content

[metrics] init() not idempotent — second call panics, breaks test isolation #223

@obchain

Description

@obchain

Refs #50

File: crates/charon-metrics/src/lib.rs

Problem

metrics-exporter-prometheus uses a process-global recorder. PrometheusBuilder::install() panics on second call within the same process. Any test that calls init() twice (or two tests in the same binary) panics. Tests must use serial_test or share a OnceCell.

Fix

Wrap init in OnceCell:

use std::sync::OnceLock;
static INIT: OnceLock<()> = OnceLock::new();

pub fn init(cfg: &MetricsConfig) -> Result<(), MetricsError> {
    INIT.get_or_init(|| { /* install recorder */ });
    Ok(())
}

Add unit test: init twice does not panic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglayer:rustRust crates (core / scanner / protocols / executor / cli)pr-reviewFindings from PR review processpriority:p2-polishNice-to-have / polishstatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions