{"record":{"id":"2378fc4bd8ae640f","repo":"nautechsystems/nautilus_trader","slug":"cannot-add-execution-algorithms-in-current-state","errorCode":null,"errorMessage":"Cannot add execution algorithms in current state: {}","messagePattern":"Cannot add execution algorithms in current state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":855,"sourceCode":"                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\n            ),\n        }\n    }\n\n    /// Starts all registered components.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if any component fails to start.\n    pub fn start_components(&mut self) -> anyhow::Result<()> {\n        let actor_ids = self.actor_ids.clone();\n        let strategy_ids = self.strategy_ids.clone();\n        let exec_algorithm_ids = self.exec_algorithm_ids.clone();\n\n        for actor_id in actor_ids {\n            log::debug!(\"Starting actor {actor_id}\");","sourceCodeStart":837,"sourceCodeEnd":873,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L837-L873","documentation":"validate_exec_algorithm_registration's catch-all arm rejects registration in any state other than PreInitialized, Ready, Stopped, Running, or Disposed, formatting the current ComponentState into the message. This covers states like Degraded or Faulted where the trader cannot safely wire a new exec algorithm.","triggerScenarios":"Calling add_exec_algorithm while the trader state is an unlisted ComponentState (commonly Degraded/Faulted following a component failure) — add_exec_algorithm calls this validation before doing any work.","commonSituations":"Attempting to register exec algorithms after a prior strategy or actor faulted the trader; recovering from a crash where the trader was left in a non-ready state and the app resumed registration without a reset.","solutions":["Inspect the state printed in the message and recover the trader (reset/restart) before registering.","Register exec algorithms during initial setup while the trader is PreInitialized or Ready.","Rebuild the Trader if it is in Degraded/Faulted state rather than registering onto it."],"exampleFix":"// before\ntrader.add_exec_algorithm(algo)?; // bails: Degraded\n\n// after\nif matches!(trader.state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Stopped) {\n    trader.add_exec_algorithm(algo)?;\n} else {\n    trader = rebuild_trader()?;\n    trader.add_exec_algorithm(algo)?;\n}","handlingStrategy":"validation","validationCode":"let ok = matches!(trader.state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Stopped);\nif !ok { /* recover or rebuild the trader first */ }","typeGuard":"fn accepts_exec_algorithm(state: &ComponentState) -> bool {\n    matches!(state, ComponentState::PreInitialized | ComponentState::Ready | ComponentState::Stopped)\n}","tryCatchPattern":"match trader.add_exec_algorithm(algo) {\n    Err(e) if e.to_string().contains(\"Cannot add execution algorithms in current state\") => {\n        trader = rebuild_trader()?;\n        trader.add_exec_algorithm(algo)?;\n    }\n    other => other?,\n}","preventionTips":["Recover from Degraded/Faulted states before any registration","Perform all exec algorithm wiring in the setup phase","Reset or rebuild the trader after component failures instead of patching state"],"tags":["rust","lifecycle","exec-algorithm","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"}