nautechsystems/nautilus_trader · error

Cannot set cache database while node is running, set it…

Error message

Cannot set cache database while node is running, set it before running the node

What it means

set_cache_database only accepts a cache database adapter while the node is in the Idle state. Calling it after the node has begun starting/running is rejected to prevent mutating the cache backend while the node is live.

Solutions

  1. Move the set_cache_database call before node.run()/start() in the setup sequence
  2. Check node.state() == NodeState::Idle before calling if the order is dynamic
  3. Restructure so all kernel/adapter configuration happens in a single pre-run setup block

Example fix

// before
node.start().await?;
node.set_cache_database(db)?;
// after
node.set_cache_database(db)?;
node.start().await?;
Defensive patterns

Strategy: validation

Validate before calling

if node.state() == NodeState::Idle {
    node.set_cache_database(db)?;
} else {
    return Err(anyhow::anyhow!("cache database must be set before node start"));
}

Prevention

When it happens

Trigger: Calling node.set_cache_database(...) after start/run has been initiated (state != NodeState::Idle), e.g. configuring the database inside a callback or after awaiting node start in async code.

Common situations: Reordering refactors that move configuration after the run call; conditional setup code that runs post-start; examples copied into code that already started the node.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/5b488edad7251c63. Report an issue: GitHub.

Appendix: source

Thrown at crates/live/src/node/mod.rs:2605

    pub fn is_running(&self) -> bool {
        self.state().is_running()
    }

    /// Sets the cache database adapter for persistence.
    ///
    /// This allows setting a database adapter (e.g., PostgreSQL, Redis) after the node
    /// is built but before it starts running. The database adapter is used to persist
    /// cache data for recovery and state management.
    ///
    /// # Errors
    ///
    /// Returns an error if the node is already running.
    pub fn set_cache_database(
        &mut self,
        database: Box<dyn CacheDatabaseAdapter>,
    ) -> anyhow::Result<()> {
        if self.state() != NodeState::Idle {
            anyhow::bail!(
                "Cannot set cache database while node is running, set it before running the node"
            );
        }

        self.kernel.cache().borrow_mut().set_database(database);
        Ok(())
    }

    /// Returns the execution manager.
    #[must_use]
    pub fn exec_manager(&self) -> &ExecutionManager {
        &self.exec_manager
    }

    /// Returns a mutable reference to the execution manager.
    #[must_use]
    pub fn exec_manager_mut(&mut self) -> &mut ExecutionManager {
        &mut self.exec_manager

View on GitHub (pinned to 18893faf8b)