{"record":{"id":"5b488edad7251c63","repo":"nautechsystems/nautilus_trader","slug":"cannot-set-cache-database-while-node-is-running-s","errorCode":null,"errorMessage":"Cannot set cache database while node is running, set it before running the node","messagePattern":"Cannot set cache database while node is running, set it before running the node","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/live/src/node/mod.rs","lineNumber":2485,"sourceCode":"    pub fn is_running(&self) -> bool {\n        self.state().is_running()\n    }\n\n    /// Sets the cache database adapter for persistence.\n    ///\n    /// This allows setting a database adapter (e.g., PostgreSQL, Redis) after the node\n    /// is built but before it starts running. The database adapter is used to persist\n    /// cache data for recovery and state management.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the node is already running.\n    pub fn set_cache_database(\n        &mut self,\n        database: Box<dyn CacheDatabaseAdapter>,\n    ) -> anyhow::Result<()> {\n        if self.state() != NodeState::Idle {\n            anyhow::bail!(\n                \"Cannot set cache database while node is running, set it before running the node\"\n            );\n        }\n\n        self.kernel.cache().borrow_mut().set_database(database);\n        Ok(())\n    }\n\n    /// Returns the execution manager.\n    #[must_use]\n    pub fn exec_manager(&self) -> &ExecutionManager {\n        &self.exec_manager\n    }\n\n    /// Returns a mutable reference to the execution manager.\n    #[must_use]\n    pub fn exec_manager_mut(&mut self) -> &mut ExecutionManager {\n        &mut self.exec_manager","sourceCodeStart":2467,"sourceCodeEnd":2503,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/2114cf6f761429e0adb5ca9596fcd7b895b16011/crates/live/src/node/mod.rs#L2467-L2503","documentation":"set_cache_database (crates/live/src/node/mod.rs:2480) swaps the backing CacheDatabaseAdapter (PostgreSQL, Redis, ...) on the kernel's cache. It is only legal while the node is NodeState::Idle, because the database adapter is wired into the cache before startup; once the node has left Idle the adapter is live and replacing it mid-run would drop or corrupt persisted state.","triggerScenarios":"Calling node.set_cache_database(adapter) after run()/run_with_mode() has started, during shutdown, or on a node whose state is anything other than Idle.","commonSituations":"Configuration code placed inside on_start or after the run call; reusing one node instance and attempting to reconfigure it for a second run; hosted apps that connect the database lazily after the node began running.","solutions":["Move set_cache_database into the setup phase, immediately after building the node and before run().","Prefer configuring the cache database through the node builder/config so the ordering cannot be wrong.","Build a new node instance for each run session instead of reconfiguring a used one."],"exampleFix":"// before\nnode.run().await?;                 // node leaves Idle\nnode.set_cache_database(pg)?;      // bails\n\n// after\nnode.set_cache_database(pg)?;      // while Idle\nnode.run().await?;","handlingStrategy":"validation","validationCode":"if node.state() == NodeState::Idle {\n    node.set_cache_database(Box::new(pg_adapter))?;\n} else {\n    anyhow::bail!(\"configure the cache database before running the node\");\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = node.set_cache_database(adapter) {\n    if e.to_string().contains(\"while node is running\") {\n        log::error!(\"stop the node, reconfigure, and start a fresh instance\");\n    }\n    return Err(e);\n}","preventionTips":["Configure the cache database during node construction, before run().","Prefer the builder/config path so ordering cannot be wrong.","Treat any node that has run as non-reconfigurable; build a new one."],"tags":["live-node","cache","configuration","lifecycle"],"backgroundTag":"invalid-lifecycle-state","analyzedSha":"2114cf6f761429e0adb5ca9596fcd7b895b16011","analyzedAt":"2026-08-21T11:28:30.864Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}