{"record":{"id":"bc512acb4fff1103","repo":"nautechsystems/nautilus_trader","slug":"cannot-add-components-to-disposed-trader","errorCode":null,"errorMessage":"Cannot add components to disposed trader","messagePattern":"Cannot add components to disposed trader","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":837,"sourceCode":"        );\n\n        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!(","sourceCodeStart":819,"sourceCodeEnd":855,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L819-L855","documentation":"validate_actor_or_strategy_registration allows adding components only in PreInitialized, Ready, Starting, Stopped, or Running states. Once the trader is Disposed, its resources are torn down and no new actors or strategies may be added, so any registration attempt fails with this message.","triggerScenarios":"Calling add_actor, add_strategy, add_actor_from_importable_config, add_strategy_from_importable_config, or prepare_python_strategy_instance after trader.dispose() (or equivalent teardown) has moved the trader's state to ComponentState::Disposed.","commonSituations":"Reusing a trader across backtest runs without recreating it; calling add_strategy in shutdown/cleanup code that runs after dispose; long-lived services holding a disposed trader after a stop-and-dispose sequence.","solutions":["Create a new Trader instance instead of reusing the disposed one.","Move all add_actor/add_strategy calls before dispose(), typically right after construction or after stop() if restarting.","Track trader state (e.g. check the component state) before registration calls."],"exampleFix":"// before\ntrader.dispose();\ntrader.add_strategy(strategy)?; // bails: disposed\n\n// after\ntrader.dispose();\nlet trader = Trader::new(trader_id, ...);\ntrader.add_strategy(strategy)?;","handlingStrategy":"validation","validationCode":"if trader.state == ComponentState::Disposed {\n    // create a new trader before adding components\n}","typeGuard":"fn can_register(trader: &Trader) -> bool {\n    !matches!(trader.state, ComponentState::Disposed)\n}","tryCatchPattern":"match trader.add_strategy(strategy) {\n    Err(e) if e.to_string().contains(\"disposed\") => {\n        let mut trader = Trader::new(trader_id, ...);\n        trader.add_strategy(strategy)?;\n    }\n    other => other?,\n}","preventionTips":["Treat Trader as single-use: rebuild after dispose","Register all components during setup, never in shutdown paths","Check trader state before every registration call"],"tags":["rust","lifecycle","disposed","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-14T00:17:10.932Z"}