{"record":{"id":"4fab0170fee3f02e","repo":"nautechsystems/nautilus_trader","slug":"actor-actor-id-is-already-registered-4fab01","errorCode":null,"errorMessage":"Actor {actor_id} is already registered","messagePattern":"Actor (.+?) is already registered","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":310,"sourceCode":"\n    /// Adds an actor to the trader.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if:\n    /// - The trader is not in a valid state for adding components.\n    /// - An actor with the same ID is already registered.\n    pub fn add_actor<T>(&mut self, actor: T) -> anyhow::Result<()>\n    where\n        T: DataActor + DataActorNative + Component + Debug + 'static,\n    {\n        self.validate_actor_or_strategy_registration()?;\n\n        let actor_id = actor.actor_id();\n\n        // Check for duplicate registration\n        if self.actor_ids.contains(&actor_id) {\n            anyhow::bail!(\"Actor {actor_id} is already registered\");\n        }\n\n        let component_id = ComponentId::from(actor_id);\n        let clock = self.create_component_clock(component_id);\n\n        let mut actor_mut = actor;\n        actor_mut.register(self.trader_id, clock, self.cache.clone())?;\n\n        self.add_registered_actor(actor_mut)\n    }\n\n    /// Adds an actor to the trader using a factory function.\n    ///\n    /// The factory function is called at registration time to create the actor,\n    /// avoiding cloning issues with non-cloneable actor types.\n    ///\n    /// # Errors\n    ///","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L292-L328","documentation":"Trader.add_actor registers an actor component and first checks its ActorId against actor_ids already tracked. Registering an actor whose ID (name + instance_id) is already present is refused, since a trader cannot manage two components under one identity.","triggerScenarios":"Calling trader.add_actor twice with the same actor instance or two actors built with identical names/instance ids; also re-adding an actor after it was only ID-tracked via add_actor_id_for_lifecycle.","commonSituations":"Re-running setup code in notebooks/tests, constructing actors without distinct `name` or `instance_id` args, retry logic that re-adds the same component after a partial failure.","solutions":["Give each actor a unique name or instance_id when constructing it.","Guard add_actor with a check of existing actor ids.","Rebuild the trader/system instead of re-adding the same component."],"exampleFix":"// before\ntrader.add_actor(MyActor())\ntrader.add_actor(MyActor())  # same default name -> duplicate id\n// after\ntrader.add_actor(MyActor(name=\"Actor-001\"))\ntrader.add_actor(MyActor(name=\"Actor-002\"))","handlingStrategy":"validation","validationCode":"existing = {a.actor_id for a in trader.actors()}\nassert actor.actor_id() not in existing, f\"{actor.actor_id()} already registered\"","typeGuard":"def actor_is_new(actor, trader) -> bool:\n    return actor.actor_id() not in {a.actor_id() for a in trader.actors()}","tryCatchPattern":"match trader.add_actor(actor) {\n    Err(e) if e.to_string().contains(\"is already registered\") => {\n        log::warn!(\"actor already present; skipping\");\n    }\n    r => r?,\n}","preventionTips":["Give every actor a unique name/instance_id at construction","Track added components and skip re-adds in setup code","Use a fresh Trader per test/run instead of reusing a populated one"],"tags":["actor","duplicate","trader","registration"],"backgroundTag":"file-already-exists","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"}