{"record":{"id":"fc034cd69a4ac590","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-query-update-position-to-database-m","errorCode":null,"errorMessage":"Failed to send query update_position to database message handler: {e}","messagePattern":"Failed to send query update_position to database message handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/cache.rs","lineNumber":1286,"sourceCode":"            anyhow::anyhow!(\"Failed to send query add_account to database message handler: {e}\")\n        })\n    }\n\n    fn update_order(&self, event: &OrderEventAny) -> anyhow::Result<()> {\n        let query = DatabaseQuery::UpdateOrder(event.clone());\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query update_order to database message handler: {e}\")\n        })\n    }\n\n    fn update_position(&self, position: &Position) -> anyhow::Result<()> {\n        let query = if position.fill_voids.is_empty() {\n            DatabaseQuery::UpdatePosition(position_last_event(position)?)\n        } else {\n            DatabaseQuery::AddPositionSnapshot(PositionSnapshot::from_replay_state(position, None))\n        };\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query update_position to database message handler: {e}\")\n        })\n    }\n\n    fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> {\n        let snapshot = OrderSnapshot::from(order.clone());\n        self.add_order_snapshot(&snapshot)\n    }\n\n    fn snapshot_position_state(\n        &self,\n        position: &Position,\n        ts_snapshot: UnixNanos,\n        unrealized_pnl: Option<Money>,\n    ) -> anyhow::Result<()> {\n        let mut snapshot = if position.fill_voids.is_empty() {\n            PositionSnapshot::from(position, unrealized_pnl)\n        } else {\n            PositionSnapshot::from_replay_state(position, unrealized_pnl)","sourceCodeStart":1268,"sourceCodeEnd":1304,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/cache.rs#L1268-L1304","documentation":"update_position builds either DatabaseQuery::UpdatePosition (last position event) or DatabaseQuery::AddPositionSnapshot (when fill_voids exist) and sends it over self.tx to the database message handler; send failure becomes this error. The only failure mode is a dropped receiver — the background persistence task is not running. Position state was not persisted when this fires.","triggerScenarios":"Calling update_position after the handler task terminated: adapter shutdown, task abort, or handler dying at startup.","commonSituations":"Position updates during node shutdown ordering issues; handler crashed earlier leaving the channel closed; adapter recreated elsewhere while old instance still referenced.","solutions":["Ensure the database adapter and handler task are running before position updates","Recreate the adapter after any shutdown instead of reusing closed instances","Fix the handler's exit root cause found in logs","Guard position persistence: losing position updates corrupts recovery state, so surface this error prominently"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = cache.update_position(&position) {\n    tracing::error!(\"position persistence failed: {e:#}\");\n    return Err(e.into());  // position state loss corrupts recovery\n}","preventionTips":["Treat position persistence failures as critical — surface them, never ignore","Enforce shutdown ordering: flush positions before dropping the adapter","Detect handler task exit early and rebuild the adapter","Test failure paths (handler panic/shutdown) in CI to catch closed-channel writes"],"tags":["rust","channel","database","postgres"],"backgroundTag":"broken-pipe","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"}