{"record":{"id":"29e456e2c76ae5e5","repo":"nautechsystems/nautilus_trader","slug":"cannot-add-execution-algorithms-to-running-trader","errorCode":null,"errorMessage":"Cannot add execution algorithms to running trader","messagePattern":"Cannot add execution algorithms to running trader","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":850,"sourceCode":"            | 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\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();","sourceCodeStart":832,"sourceCodeEnd":868,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L832-L868","documentation":"validate_exec_algorithm_registration permits exec algorithm registration only in PreInitialized, Ready, or Stopped states. Adding an execution algorithm while the trader is Running is rejected because the algorithm's msgbus endpoint and subscription hooks must be installed before the trader starts routing orders.","triggerScenarios":"Calling add_exec_algorithm (directly or via add_py_execution_algorithm_instance) while trader.state == ComponentState::Running — e.g. loading an algorithm mid-run or in a live callback.","commonSituations":"Attempting hot-loading of exec algorithms in a live trading session; a config loader that runs after trader.start(); background tasks lazily registering algorithms once the engine is already trading.","solutions":["Register all exec algorithms before calling trader.start() (PreInitialized/Ready).","If restarting is acceptable: stop the trader (Stopped allows registration), add the algorithm, then start again.","Preload algorithms from config during setup instead of registering them on demand at runtime."],"exampleFix":"// before\ntrader.start()?;\ntrader.add_exec_algorithm(algo)?; // bails: running\n\n// after\ntrader.add_exec_algorithm(algo)?; // before start\ntrader.start()?;","handlingStrategy":"validation","validationCode":"if trader.state == ComponentState::Running {\n    // defer registration until stopped, or preload before start\n}","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(\"running trader\") => {\n        trader.stop()?;\n        trader.add_exec_algorithm(algo)?;\n        trader.start()?;\n    }\n    other => other?,\n}","preventionTips":["Register all exec algorithms before trader.start()","Avoid lazy/hot registration from live callbacks","Load algorithms from config during the setup phase"],"tags":["rust","lifecycle","exec-algorithm","trader-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"}