{"record":{"id":"acfff0b971686773","repo":"nautechsystems/nautilus_trader","slug":"simulation-module-module-index-log-diagnostics-f","errorCode":null,"errorMessage":"Simulation module {module_index} log_diagnostics failed: {e:#}","messagePattern":"Simulation module (.+?) log_diagnostics failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/exchange.rs","lineNumber":1724,"sourceCode":"        self.funding_settlements.clear();\n        self.message_queue.clear();\n        self.inflight_queue.clear();\n        self.inflight_counter.clear();\n\n        log::info!(\"Resetting exchange state\");\n        self.module_error = module_error;\n        self.check_module_error()\n    }\n\n    /// Logs diagnostic information from all simulation modules.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if a simulation module cannot produce its diagnostics.\n    pub fn log_diagnostics(&self) -> anyhow::Result<()> {\n        for (module_index, module) in self.modules.iter().enumerate() {\n            module.log_diagnostics().map_err(|e| {\n                anyhow::anyhow!(\"Simulation module {module_index} log_diagnostics failed: {e:#}\")\n            })?;\n        }\n        Ok(())\n    }\n\n    /// Checks if any margin accounts have breached maintenance margin and liquidates open\n    /// positions when the trigger threshold is met.\n    ///\n    /// Liquidation is scoped to the breached settlement currency: only positions whose\n    /// instrument settles in the same currency as the breached margin account are closed.\n    /// Positions settled in other currencies remain open, isolating the liquidation to\n    /// the currency whose equity fell below the maintenance threshold.\n    ///\n    /// > **Note**: A future `cross_margin_mode` venue configuration could extend this to\n    /// > liquidate all positions across all settlement currencies simultaneously.\n    pub fn process_liquidations(&mut self, ts_now: UnixNanos) {\n        if !self.liquidation_enabled {\n            return;","sourceCodeStart":1706,"sourceCodeEnd":1742,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/exchange.rs#L1706-L1742","documentation":"log_diagnostics iterates all registered simulation modules and propagates any failure from a module's own log_diagnostics call, wrapping it with the failing module's index. The root cause is inside the specific module named in the message; this wrapper only identifies which module failed.","triggerScenarios":"Calling SimulationEngine/Exchange log_diagnostics() when any registered simulation module (e.g. the CFD swap module) returns an error from its internal log_diagnostics implementation.","commonSituations":"Calling diagnostics on an engine whose modules are not yet initialized (e.g. CFD swap module before rollover day setup); running diagnostics mid-backtest when module internal state is transiently empty.","solutions":["Read the chained '{e:#}' detail in the message to find the module's underlying failure","Ensure modules are fully initialized (e.g. first on_bar/on_event processing done) before calling log_diagnostics","Wrap the call and treat diagnostics as best-effort: log and continue rather than aborting the backtest"],"exampleFix":"// before\nengine.log_diagnostics()?;\n// after\nif let Err(e) = engine.log_diagnostics() {\n    log::warn!(\"diagnostics unavailable: {e:#}\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// treat diagnostics as best-effort\nif let Err(e) = engine.log_diagnostics() {\n    log::warn!(\"diagnostics failed: {e:#\");\n}","preventionTips":["Initialize all modules (on_start / first event) before requesting diagnostics","Read the chained module index and inner error to find the root cause"],"tags":["backtest","diagnostics","error-wrapping"],"backgroundTag":"internal-invariant-violation","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}