{"record":{"id":"85c1a3b177bdcbd7","repo":"nautechsystems/nautilus_trader","slug":"cannot-add-components-in-current-state","errorCode":null,"errorMessage":"Cannot add components in current state: {}","messagePattern":"Cannot add components in current state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":839,"sourceCode":"        Ok(())\n    }\n\n    /// Validates that the trader is in a valid state for actor and strategy registration.\n    ///\n    /// Actors and strategies can be added while the trader is `PreInitialized`, `Ready`,\n    /// `Stopped`, or `Running`. This enables the [`Controller`](crate::controller::Controller)\n    /// to add them at runtime.\n    pub(crate) fn validate_actor_or_strategy_registration(&self) -> anyhow::Result<()> {\n        match self.state {\n            ComponentState::PreInitialized\n            | ComponentState::Ready\n            | ComponentState::Starting\n            | ComponentState::Stopped\n            | ComponentState::Running => Ok(()),\n            ComponentState::Disposed => {\n                anyhow::bail!(\"Cannot add components to disposed trader\")\n            }\n            _ => anyhow::bail!(\"Cannot add components in current state: {}\", self.state),\n        }\n    }\n\n    /// Validates that the trader is in a valid state for execution algorithm registration.\n    pub(crate) fn validate_exec_algorithm_registration(&self) -> anyhow::Result<()> {\n        match self.state {\n            ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Stopped => {\n                Ok(())\n            }\n            ComponentState::Running => {\n                anyhow::bail!(\"Cannot add execution algorithms to running trader\")\n            }\n            ComponentState::Disposed => {\n                anyhow::bail!(\"Cannot add components to disposed trader\")\n            }\n            _ => anyhow::bail!(\n                \"Cannot add execution algorithms in current state: {}\",\n                self.state","sourceCodeStart":821,"sourceCodeEnd":857,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L821-L857","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the current state in the error message and resolve it first: recover or reset a Degraded/Faulted trader.","Stop then restart the trader (Stopped is a valid state for adding components), or build a fresh Trader.","Add health checks before registration so you only add components when the trader is Ready or Running."],"exampleFix":"// before\ntrader.add_actor(actor)?; // bails while Degraded\n\n// after\nif matches!(trader.state, ComponentState::Ready | ComponentState::Stopped | ComponentState::PreInitialized) {\n    trader.add_actor(actor)?;\n} else {\n    // recreate or recover trader first\n}","handlingStrategy":"validation","validationCode":"let ok = matches!(trader.state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Starting | ComponentState::Stopped | ComponentState::Running);\nif !ok { /* recover or recreate trader first */ }","typeGuard":"fn accepts_components(state: &ComponentState) -> bool {\n    matches!(state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Starting | ComponentState::Stopped | ComponentState::Running)\n}","tryCatchPattern":"match trader.add_actor(actor) {\n    Err(e) if e.to_string().contains(\"Cannot add components in current state\") => {\n        // recover/reset trader, then retry\n    }\n    other => other?,\n}","preventionTips":["Resolve Degraded/Faulted states before registering new components","Add components only in PreInitialized/Ready during setup","Monitor component failures so the trader is reset promptly"],"tags":["rust","lifecycle","trader-state","invalid-state"],"backgroundTag":"invalid-state-transition","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"}