{"record":{"id":"671be8fe098ab205","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-query-to-database-message-handler","errorCode":null,"errorMessage":"Failed to send query to database message handler: {e}","messagePattern":"Failed to send query to database message handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/cache.rs","lineNumber":910,"sourceCode":"            \"delete_order not implemented for PostgreSQL cache adapter: {client_order_id}\"\n        )\n    }\n\n    fn delete_position(&self, position_id: &PositionId) -> anyhow::Result<()> {\n        anyhow::bail!(\"delete_position not implemented for PostgreSQL cache adapter: {position_id}\")\n    }\n\n    fn delete_account_event(&self, account_id: &AccountId, event_id: &str) -> anyhow::Result<()> {\n        anyhow::bail!(\n            \"delete_account_event not implemented for PostgreSQL cache adapter: {account_id}, {event_id}\"\n        )\n    }\n\n    fn add(&self, key: String, value: Bytes) -> anyhow::Result<()> {\n        let query = DatabaseQuery::Add(key, value.into());\n        self.tx\n            .send(query)\n            .map_err(|e| anyhow::anyhow!(\"Failed to send query to database message handler: {e}\"))\n    }\n\n    fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> {\n        let query = DatabaseQuery::AddCurrency(*currency);\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to query add_currency to database message handler: {e}\")\n        })\n    }\n\n    fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> {\n        let query = DatabaseQuery::AddInstrument(instrument.clone());\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query add_instrument to database message handler: {e}\")\n        })\n    }\n\n    fn add_instrument_close(&self, close: &InstrumentClose) -> anyhow::Result<()> {\n        self.tx","sourceCodeStart":892,"sourceCodeEnd":928,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/cache.rs#L892-L928","documentation":"Thrown by the SQL cache's `add` when sending a `DatabaseQuery::Add(key, value)` over the internal mpsc channel to the dedicated database message-handler task fails. A send fails only when the receiver side has been dropped/shut down, meaning the writer task is no longer processing queries. The write is not performed.","triggerScenarios":"Calling `add` (exposed to Python as `py_add`) after the backing task/thread running the message loop has exited or been dropped, so the `tx.send(query)` returns a SendError.","commonSituations":"Adapter shutdown ordering — cache database client stopped or its connection task panicked while the cache is still being written; a background panic killed the receiver; calling add during teardown/atexit from Python.","solutions":["Ensure the cache database adapter (and its message-handler task) is created, connected, and kept alive before calling `add`.","Check logs from the receiver task for a panic that dropped the channel.","Rebuild/reconnect the cache database client and retry the write.","Guard Python code so `py_add` is not called after the adapter has been stopped."],"exampleFix":"// before\nself.tx.send(query).map_err(|e| anyhow::anyhow!(\"Failed to send query to database message handler: {e}\"))?;\n// after\nif self.tx.is_closed() {\n    return Err(anyhow::anyhow!(\"Database message handler is not running; cannot persist cache entry\"));\n}\nself.tx.send(query).map_err(|e| anyhow::anyhow!(\"Failed to send query to database message handler: {e}\"))?;","handlingStrategy":"try-catch","validationCode":"# Python: verify adapter is writable before add\nif not cache_db_adapter.is_running:\n    raise RuntimeError(\"Cache database adapter is not running\")","typeGuard":null,"tryCatchPattern":"try:\n    cache.add(key, value)\nexcept RuntimeError as e:\n    logger.error(\"Cache database writer unavailable: %s\", e)","preventionTips":["Keep the cache database adapter connected for the whole session","Stop writes before disconnecting the adapter","Monitor the writer task's health/panics","Check send-channel liveness before bulk writes"],"tags":["database","channel","shutdown","sql"],"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"}