{"record":{"id":"abae70509ac3565b","repo":"nautechsystems/nautilus_trader","slug":"failed-to-calculate-duration-e","errorCode":null,"errorMessage":"Failed to calculate duration: {e}","messagePattern":"Failed to calculate duration: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/execution/src/engine/mod.rs","lineNumber":993,"sourceCode":"                    .map(|o| (o.instrument_id(), o.to_own_book_order()))\n                    .collect()\n            } else {\n                Vec::new()\n            }\n        };\n\n        for (instrument_id, own_order) in own_book_entries {\n            let mut own_book = self.get_or_init_own_order_book(&instrument_id);\n            own_book.add(own_order);\n        }\n\n        self.set_position_id_counts();\n\n        log::info!(\n            \"Loaded cache in {}ms\",\n            SystemTime::now() // dst-ok: init-time log timing, not on DST state path\n                .duration_since(ts)\n                .map_err(|e| anyhow::anyhow!(\"Failed to calculate duration: {e}\"))?\n                .as_millis()\n        );\n\n        Ok(())\n    }\n\n    /// Flushes the database to persist all cached data.\n    pub fn flush_db(&self) {\n        self.cache.borrow_mut().flush_db();\n    }\n\n    /// Reconciles an execution report.\n    pub fn reconcile_execution_report(&mut self, report: &ExecutionReport) {\n        if !matches!(report, ExecutionReport::MassStatus(_)) {\n            self.report_count += 1;\n        }\n\n        match report {","sourceCodeStart":975,"sourceCodeEnd":1011,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/execution/src/engine/mod.rs#L975-L1011","documentation":"At the end of load_cache, the engine logs how long the cache load took using SystemTime::now().duration_since(ts). If the system clock moved backwards since ts (manual adjustment, NTP correction, VM suspend/resume), duration_since returns an Err and the engine converts it into this anyhow error, aborting load_cache even though the cache itself loaded successfully.","triggerScenarios":"Calling load_cache (e.g. during execution engine initialization) when SystemTime::now() is earlier than the timestamp ts captured at the start of the load — the OS clock was set backwards during the load.","commonSituations":"NTP stepping the clock backward on a VM or container host during startup; laptop resumed from sleep with a stale clock; operator manually changing system time; running in a VM with paused/resumed host time.","solutions":["Retry the load once the system clock is synchronized (systemd-timesyncd/chronyd) — the cache data itself is unaffected.","Replace the timing code with Instant (monotonic) instead of SystemTime for elapsed-time measurement.","Downgrade the duration calculation to a non-fatal log path so a clock jump doesn't fail cache loading.","Ensure the host clock is synced (NTP enabled) before starting the node."],"exampleFix":"// before\nlet elapsed = SystemTime::now()\n    .duration_since(ts)\n    .map_err(|e| anyhow::anyhow!(\"Failed to calculate duration: {e}\"))?;\n// after\nlet elapsed = ts.elapsed(); // ts: Instant — monotonic, cannot go backwards","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// tolerate clock jumps during init-time logging\nmatch SystemTime::now().duration_since(ts) {\n    Ok(d) => log::info!(\"Loaded cache in {}ms\", d.as_millis()),\n    Err(_) => log::warn!(\"clock stepped backwards during cache load\"),\n}","preventionTips":["Enable NTP/chrony so the host clock does not step backwards at runtime.","Prefer Instant over SystemTime for measuring elapsed durations.","Avoid running nodes on hosts that suspend/resume or share unmanaged clocks (VMs)."],"tags":["clock","systemtime","cache-load","rust"],"backgroundTag":"system-clock-error","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"}