nautechsystems/nautilus_trader · error

Cannot add components in current state: {}

Error message

Cannot add components in current state: {}

What it means

validate_actor_or_strategy_registration rejects component registration when the trader is in a state outside the explicitly allowed set (PreInitialized, Ready, Starting, Stopped, Running, Disposed). The remaining states (e.g. Degraded, Faulted, or otherwise uninitialized states) fall through to this catch-all bail, embedding the current state in the message.

Source

Thrown at crates/system/src/trader.rs:839

        Ok(())
    }

    /// Validates that the trader is in a valid state for actor and strategy registration.
    ///
    /// Actors and strategies can be added while the trader is `PreInitialized`, `Ready`,
    /// `Stopped`, or `Running`. This enables the [`Controller`](crate::controller::Controller)
    /// to add them at runtime.
    pub(crate) fn validate_actor_or_strategy_registration(&self) -> anyhow::Result<()> {
        match self.state {
            ComponentState::PreInitialized
            | ComponentState::Ready
            | ComponentState::Starting
            | ComponentState::Stopped
            | ComponentState::Running => Ok(()),
            ComponentState::Disposed => {
                anyhow::bail!("Cannot add components to disposed trader")
            }
            _ => anyhow::bail!("Cannot add components in current state: {}", self.state),
        }
    }

    /// Validates that the trader is in a valid state for execution algorithm registration.
    pub(crate) fn validate_exec_algorithm_registration(&self) -> anyhow::Result<()> {
        match self.state {
            ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Stopped => {
                Ok(())
            }
            ComponentState::Running => {
                anyhow::bail!("Cannot add execution algorithms to running trader")
            }
            ComponentState::Disposed => {
                anyhow::bail!("Cannot add components to disposed trader")
            }
            _ => anyhow::bail!(
                "Cannot add execution algorithms in current state: {}",
                self.state

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the current state in the error message and resolve it first: recover or reset a Degraded/Faulted trader.
  2. Stop then restart the trader (Stopped is a valid state for adding components), or build a fresh Trader.
  3. Add health checks before registration so you only add components when the trader is Ready or Running.

Example fix

// before
trader.add_actor(actor)?; // bails while Degraded

// after
if matches!(trader.state, ComponentState::Ready | ComponentState::Stopped | ComponentState::PreInitialized) {
    trader.add_actor(actor)?;
} else {
    // recreate or recover trader first
}
Defensive patterns

Strategy: validation

Validate before calling

let ok = matches!(trader.state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Starting | ComponentState::Stopped | ComponentState::Running);
if !ok { /* recover or recreate trader first */ }

Type guard

fn accepts_components(state: &ComponentState) -> bool {
    matches!(state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Starting | ComponentState::Stopped | ComponentState::Running)
}

Try / catch

match trader.add_actor(actor) {
    Err(e) if e.to_string().contains("Cannot add components in current state") => {
        // recover/reset trader, then retry
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling add_actor/add_strategy (or their config-based and Python-instance variants) while the trader's state is one of the unlisted ComponentState values — typically Degraded or Faulted after a component failure.

Common situations: A strategy or actor faulted the trader earlier, and application code keeps trying to register more components; resuming a run after an unhandled error left the trader degraded.

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/85c1a3b177bdcbd7. Report an issue: GitHub.