{"record":{"id":"9d8f75e83ad827cc","repo":"nautechsystems/nautilus_trader","slug":"failed-to-initialize-tracing-subscriber-e","errorCode":null,"errorMessage":"Failed to initialize tracing subscriber: {e}","messagePattern":"Failed to initialize tracing subscriber: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/common/src/logging/bridge.rs","lineNumber":113,"sourceCode":"/// # Errors\n///\n/// Returns an error if the tracing subscriber has already been initialized.\npub fn init_tracing() -> anyhow::Result<()> {\n    if TRACING_INITIALIZED.load(Ordering::SeqCst) {\n        anyhow::bail!(\"Tracing subscriber already initialized\");\n    }\n\n    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(\"warn\"));\n\n    let subscriber = tracing_subscriber::registry()\n        .with(filter)\n        .with(fmt::layer().event_format(NautilusFormatter));\n\n    // Install only the tracing subscriber here. Python logging manages the\n    // global `log` logger separately, so we must not claim it through\n    // SubscriberInitExt::try_init().\n    tracing::subscriber::set_global_default(subscriber)\n        .map_err(|e| anyhow::anyhow!(\"Failed to initialize tracing subscriber: {e}\"))?;\n\n    TRACING_INITIALIZED.store(true, Ordering::SeqCst);\n    Ok(())\n}\n\n#[cfg(test)]\nmod tests {\n    use rstest::rstest;\n\n    use super::*;\n\n    #[rstest]\n    fn test_tracing_is_initialized_returns_bool() {\n        let _ = tracing_is_initialized();\n    }\n}\n","sourceCodeStart":95,"sourceCodeEnd":130,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/logging/bridge.rs#L95-L130","documentation":"`init_tracing` builds a fmt tracing subscriber with the Nautilus formatter and installs it via `tracing::subscriber::set_global_default`. The global default subscriber can only be set once per process, so a second initialization attempt returns this error wrapping the tracing SetGlobalDefaultError.","triggerScenarios":"Calling init_tracing (or a higher-level init that reaches it) more than once in the same process — e.g. a test harness and the code under test both calling it, multiple subsystems initializing logging, or re-running after a previous call already flipped TRACING_INITIALIZED.","commonSituations":"Running multiple tests in one process that each call init; embedding nautilus into an app that already installed its own tracing subscriber; double-start of a runtime/node; mixing init_tracing with another crate that claims the global subscriber.","solutions":["Call init_tracing only once per process; gate it behind a OnceLock/once flag or the library's existing TRACING_INITIALIZED check.","Check the returned error's source for 'a global default trace dispatcher has already been set' to detect double init.","Use tracing::subscriber::with_default (thread-local) or set_default in scoped contexts instead of global installation for tests.","Ensure the host application does not install its own global subscriber before nautilus init."],"exampleFix":"// before\ninit_tracing(level)?; // called in setup and again in each test\n// after\nstatic INIT: OnceLock<()> = OnceLock::new();\nif INIT.set(()).is_ok() {\n    init_tracing(level)?;\n}","handlingStrategy":"try-catch","validationCode":"// Rust: guard against double global subscriber install\nstatic TRACING_DONE: OnceLock<()> = OnceLock::new();\nif TRACING_DONE.get().is_some() { return Ok(()); }","typeGuard":"fn tracing_ready() -> bool { TRACING_INITIALIZED.load(Ordering::SeqCst) }","tryCatchPattern":"if let Err(e) = init_tracing(level) {\n    if e.to_string().contains(\"already been set\") { debug!(\"tracing already installed\"); }\n    else { return Err(e); }\n}","preventionTips":["Install the global subscriber exactly once at process entry.","Use thread-local with_default for tests needing isolated subscribers.","Coordinate init between host application and library (single init entry point).","Gate init behind OnceLock/atomic flags even in test harnesses."],"tags":["rust","tracing","logging","double-initialization"],"backgroundTag":"module-init-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}