{"record":{"id":"c77171cb0cd529ac","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-query-add-instrument-to-database-me","errorCode":null,"errorMessage":"Failed to send query add_instrument to database message handler: {e}","messagePattern":"Failed to send query add_instrument to database message handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/cache.rs","lineNumber":923,"sourceCode":"\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\n            .send(DatabaseQuery::AddInstrumentClose(*close))\n            .map_err(|e| {\n                anyhow::anyhow!(\n                    \"Failed to send query add_instrument_close to database message handler: {e}\"\n                )\n            })\n    }\n\n    fn add_synthetic(&self, _synthetic: &SyntheticInstrument) -> anyhow::Result<()> {\n        todo!()\n    }\n\n    fn add_account(&self, account: &AccountAny) -> anyhow::Result<()> {","sourceCodeStart":905,"sourceCodeEnd":941,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/cache.rs#L905-L941","documentation":"Thrown by `add_instrument` when the `DatabaseQuery::AddInstrument` message cannot be delivered to the database message-handler task over its mpsc channel, i.e. the receiver has been dropped. The instrument is not persisted.","triggerScenarios":"Calling `add_instrument` (via `py_add_instrument` or `seed_order_event_dependencies`) when the writer task is gone — adapter stopped, handler panicked, or process is shutting down.","commonSituations":"Adding instruments to a cache database whose background writer already terminated; shutdown-ordering bugs in live trading setups.","solutions":["Ensure the adapter's message-handler task is alive before adding instruments.","Check handler logs for panics.","Reconnect/restart the cache database client and retry.","In Python, avoid calling `py_add_instrument` after adapter disconnect."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if not cache_db_adapter.is_running:\n    raise RuntimeError(\"Cannot add_instrument: adapter not running\")","typeGuard":null,"tryCatchPattern":"try:\n    cache.add_instrument(instrument)\nexcept RuntimeError as e:\n    logger.error(\"Instrument persistence failed (writer stopped): %s\", e)","preventionTips":["Load instruments before starting data/trading flows","Keep the writer task alive until all instruments are added","Log and handle SendError-like failures explicitly","Reconnect the adapter on failure instead of dropping writes silently"],"tags":["database","channel","instrument","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-14T05:17:10.506Z"}