{"record":{"id":"cb9a097430ee1b58","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-query-add-bar-to-database-message-h","errorCode":null,"errorMessage":"Failed to send query add_bar to database message handler: {e}","messagePattern":"Failed to send query add_bar to database message handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/cache.rs","lineNumber":1065,"sourceCode":"        });\n        Ok(rx.recv()?)\n    }\n\n    fn add_funding_rate(&self, _funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {\n        anyhow::bail!(\"add_funding_rate not implemented for PostgreSQL cache adapter\")\n    }\n\n    fn load_funding_rates(\n        &self,\n        _instrument_id: &InstrumentId,\n    ) -> anyhow::Result<Vec<FundingRateUpdate>> {\n        anyhow::bail!(\"load_funding_rates not implemented for PostgreSQL cache adapter\")\n    }\n\n    fn add_bar(&self, bar: &Bar) -> anyhow::Result<()> {\n        let query = DatabaseQuery::AddBar(bar.to_owned());\n        self.tx.send(query).map_err(|e| {\n            anyhow::anyhow!(\"Failed to send query add_bar to database message handler: {e}\")\n        })\n    }\n\n    fn load_bars(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<Bar>> {\n        let pool = self.pool.clone();\n        let instrument_id = instrument_id.to_owned();\n        let (tx, rx) = std::sync::mpsc::channel();\n\n        tokio::spawn(async move {\n            let result = DatabaseQueries::load_bars(&pool, &instrument_id).await;\n            match result {\n                Ok(bars) => {\n                    if let Err(e) = tx.send(bars) {\n                        log::error!(\"Failed to send bars for instrument {instrument_id}: {e:?}\");\n                    }\n                }\n                Err(e) => {\n                    log::error!(\"Failed to load bars for instrument {instrument_id}: {e:?}\");","sourceCodeStart":1047,"sourceCodeEnd":1083,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/cache.rs#L1047-L1083","documentation":"add_bar wraps the Bar in DatabaseQuery::AddBar and sends it over self.tx to the database message handler; on send failure anyhow::anyhow! raises this error. Sends fail only when the receiver half was dropped, i.e. the background writer task is gone. Note the message text says add_bar while some sibling methods (e.g. add_custom_data) reuse other labels — the mechanism is identical.","triggerScenarios":"Calling add_bar (or py_add_bar) after the handler task exited — adapter shutdown, aborted task, or handler that died at startup.","commonSituations":"Bar persistence during a long backfill while the DB adapter was recreated or disconnected; shutdown ordering bug where the cache is dropped before writers stop; handler crashed earlier on a bad batch.","solutions":["Ensure the cache adapter and its handler task are alive before bar writes","Recreate the adapter after shutdown rather than reusing the dropped-channel instance","Find and fix why the handler task ended (startup failure, panic) from its logs","Guard bar writes with error handling when persistence loss is acceptable"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    cache.add_bar(bar)\nexcept Exception as e:\n    if \"Failed to send query add_bar\" in str(e):\n        logger.warning(\"bar persistence skipped: handler down\")\n        return  # or buffer bar for retry","preventionTips":["Buffer bars locally and flush when the adapter is restored","Stop backfill jobs before shutting down the adapter","Track adapter liveness with a flag checked before writes","Review handler panics in logs to prevent silent channel closure"],"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-14T00:17:10.932Z"}