{"record":{"id":"96f3a281a950fadb","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-query-add-order-snapshot-to-databas","errorCode":null,"errorMessage":"Failed to send query add_order_snapshot to database message handler: {e}","messagePattern":"Failed to send query add_order_snapshot to database message handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/cache.rs","lineNumber":975,"sourceCode":"        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\n                \"Failed to send query add_order_snapshot to database message handler: {e}\"\n            )\n        })\n    }\n\n    fn add_position(&self, position: &Position) -> anyhow::Result<()> {\n        let event = position_last_event(position)?;\n        let query = DatabaseQuery::AddPosition(position.id, event);\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query add_position to database message handler: {e}\")\n        })\n    }\n\n    fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()> {\n        let query = DatabaseQuery::AddPositionSnapshot(snapshot.to_owned());\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\n                \"Failed to send query add_position_snapshot to database message handler: {e}\"\n            )\n        })\n    }\n\n    fn add_order_book(&self, _order_book: &OrderBook) -> anyhow::Result<()> {\n        todo!()\n    }\n\n    fn add_quote(&self, quote: &QuoteTick) -> anyhow::Result<()> {\n        let query = DatabaseQuery::AddQuote(quote.to_owned());\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query add_quote to database message handler: {e}\")\n        })\n    }\n\n    fn load_quotes(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<QuoteTick>> {\n        let pool = self.pool.clone();","sourceCodeStart":957,"sourceCodeEnd":993,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/cache.rs#L957-L993","documentation":"Thrown by `add_position_snapshot` when the `DatabaseQuery::AddPositionSnapshot` message cannot be sent to the database message-handler task because the receiving end of the channel was dropped. The snapshot is not persisted. Despite the message text, this is the position-snapshot write path.","triggerScenarios":"Calling `add_position_snapshot` (via `py_add_position_snapshot` or `snapshot_position_state`) when the writer task has exited or panicked.","commonSituations":"Snapshotting position state during shutdown; dead writer task from an earlier handler crash.","solutions":["Ensure the adapter is running before taking position snapshots.","Check for panics in the message-handler task.","Reconnect/recreate the adapter and re-issue the snapshot.","Sequence shutdown so snapshots are flushed before the writer stops."],"exampleFix":"// before\nself.tx.send(query).map_err(|e| anyhow::anyhow!(\"Failed to send query add_position_snapshot to database message handler: {e}\"))?;\n// after\nif self.tx.is_closed() {\n    return Err(anyhow::anyhow!(\"Cannot persist position snapshot: database message handler is stopped\"));\n}\nself.tx.send(query).map_err(|e| anyhow::anyhow!(\"Failed to send query add_position_snapshot to database message handler: {e}\"))?;","handlingStrategy":"try-catch","validationCode":"if not cache_db_adapter.is_running:\n    raise RuntimeError(\"Cannot snapshot position state: adapter not running\")","typeGuard":null,"tryCatchPattern":"try:\n    cache.add_position_snapshot(snapshot)\nexcept RuntimeError as e:\n    logger.error(\"Position snapshot persistence failed: %s\", e)","preventionTips":["Snapshot positions while the adapter is running","Sequence shutdown so the writer stops last","Monitor writer task health","Retry the snapshot after reconnecting the adapter"],"tags":["database","channel","position","snapshot","shutdown"],"backgroundTag":"database-write-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"}