{"record":{"id":"e14515ea1d4b1826","repo":"nautechsystems/nautilus_trader","slug":"heartbeat-health-margin-should-be-shorter-than-the","errorCode":null,"errorMessage":"heartbeat health margin should be shorter than the safety timeout","messagePattern":"heartbeat health margin should be shorter than the safety timeout","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/lifecycle.rs","lineNumber":737,"sourceCode":"        self.teardown_partial_connect().await?;\n\n        log::info!(\"Disconnected: client_id={}\", self.core.client_id);\n        Ok(())\n    }\n\n    pub(super) fn on_instrument_update(&self, instrument: &InstrumentAny) {\n        self.upsert_execution_lookup(instrument);\n    }\n}\n\nasync fn run_heartbeats(\n    http_client: crate::http::clob::PolymarketClobHttpClient,\n    cancellation: CancellationToken,\n    healthy: Arc<AtomicBool>,\n) {\n    let heartbeat_health_timeout = HEARTBEAT_SAFETY_TIMEOUT\n        .checked_sub(HEARTBEAT_HEALTH_MARGIN)\n        .expect(\"heartbeat health margin should be shorter than the safety timeout\");\n    let mut interval = tokio::time::interval(HEARTBEAT_INTERVAL);\n    interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);\n    let mut heartbeat_id = String::new();\n    let mut request_failures = 0;\n    let mut last_acknowledged = None;\n\n    loop {\n        tokio::select! {\n            () = cancellation.cancelled() => break,\n            _ = interval.tick() => {}\n        }\n\n        let mut resynchronized = false;\n\n        loop {\n            let now = tokio::time::Instant::now();\n            let request_timeout = now\n                .checked_add(HEARTBEAT_REQUEST_TIMEOUT)","sourceCodeStart":719,"sourceCodeEnd":755,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/lifecycle.rs#L719-L755","documentation":"The Polymarket heartbeat task computes its health-check timeout as HEARTBEAT_SAFETY_TIMEOUT - HEARTBEAT_HEALTH_MARGIN using checked_sub on Durations. The expect asserts the compile-time constant margin is smaller than the safety timeout; with any valid build this cannot fail, so the panic indicates corrupted constants or a binary/patch tampering with them.","triggerScenarios":"Starting the heartbeat task (start_heartbeat_task) when HEARTBEAT_HEALTH_MARGIN >= HEARTBEAT_SAFETY_TIMEOUT — only possible if the constants were modified (e.g. local patch or a mis-scoped fork build).","commonSituations":"Forked adapter builds where timing constants were tweaked without preserving the invariant margin < safety timeout; binary-level constant corruption.","solutions":["Restore the stock constant relationship: HEARTBEAT_HEALTH_MARGIN must be strictly less than HEARTBEAT_SAFETY_TIMEOUT.","Rebuild from an unmodified nautilus source tree.","If tuning constants, add/keep a static assert or startup check validating margin < safety timeout."],"exampleFix":"// before\nconst HEARTBEAT_HEALTH_MARGIN: Duration = Duration::from_secs(20);\nconst HEARTBEAT_SAFETY_TIMEOUT: Duration = Duration::from_secs(15); // margin > safety\n\n// after\nconst HEARTBEAT_SAFETY_TIMEOUT: Duration = Duration::from_secs(30);\nconst HEARTBEAT_HEALTH_MARGIN: Duration = Duration::from_secs(10); // margin < safety","handlingStrategy":"validation","validationCode":"// build-time/static check if you tune the constants\nconst _: () = assert!(HEARTBEAT_HEALTH_MARGIN < HEARTBEAT_SAFETY_TIMEOUT);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep stock timing constants; if tuning, always preserve margin < safety timeout.","Add a compile-time assert to catch bad constant edits.","Rebuild adapters from unmodified upstream sources."],"tags":["rust","panic","polymarket","heartbeat","configuration","timing"],"backgroundTag":"invalid-config-value","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"}