{"record":{"id":"e993bc30fb8ee074","repo":"nautechsystems/nautilus_trader","slug":"cannot-clear-external-order-claims-e","errorCode":null,"errorMessage":"Cannot clear external order claims: {e}","messagePattern":"Cannot clear external order claims: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":1459,"sourceCode":"    /// through a bulk operation leaves the trader's bookkeeping consistent with the registries.\n    fn retire_actor(&mut self, actor_id: ActorId) -> anyhow::Result<()> {\n        Self::dispose_registered_component(actor_id.inner())?;\n\n        self.release_component(ComponentId::from(actor_id));\n        self.actor_ids.retain(|id| id != &actor_id);\n        self.actor_state_callbacks.remove(&actor_id);\n\n        Ok(())\n    }\n\n    /// Disposes a strategy, then releases everything its registration created.\n    fn retire_strategy(&mut self, strategy_id: StrategyId) -> anyhow::Result<()> {\n        // Check before disposal so a borrow failure cannot leave a disposed strategy registered.\n        {\n            let _cache = self\n                .cache\n                .try_borrow_mut()\n                .map_err(|e| anyhow::anyhow!(\"Cannot clear external order claims: {e}\"))?;\n        }\n\n        Self::dispose_registered_component(strategy_id.inner())?;\n\n        self.cache\n            .try_borrow_mut()\n            .map_err(|e| anyhow::anyhow!(\"Cannot clear external order claims: {e}\"))?\n            .set_external_order_claims(strategy_id, &[])?;\n\n        self.remove_strategy_subscriptions(strategy_id);\n        self.release_component(ComponentId::from(strategy_id));\n        self.strategy_ids.retain(|id| id != &strategy_id);\n        self.strategy_state_callbacks.remove(&strategy_id);\n        self.strategy_stop_fns.remove(&strategy_id);\n\n        Ok(())\n    }\n","sourceCodeStart":1441,"sourceCodeEnd":1477,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L1441-L1477","documentation":"Raised in `retire_strategy` when the cache `Rc<RefCell<Cache>>` cannot be mutably borrowed while attempting to clear external order claims for the retiring strategy. A live mutable borrow elsewhere (RefCell already borrowed) prevents the check, and the strategy would risk being disposed while still tracked.","triggerScenarios":"Calling `remove_strategy`, `clear_strategies`, or `dispose_components` while another component (actor, strategy, or callback on the same thread) holds a mutable borrow of the cache, causing `try_borrow_mut` to fail with a BorrowError.","commonSituations":"Reentrant calls into the trader from within a strategy/actor callback that itself holds the cache borrow; nested strategy disposal while iterating cache state; calling trader lifecycle methods from inside event handlers.","solutions":["Avoid calling trader retirement/removal APIs from inside code that holds a cache borrow (e.g. inside strategy handlers)","Defer retirement to outside the current callback (queue it and execute after the handler returns)","Inspect the chained BorrowError context to find who holds the borrow","Refactor to use the trader's message bus/command pattern instead of direct nested calls"],"exampleFix":"// before: retiring from inside an on_event handler that borrows cache\ndef on_event(self, event):\n    self.trader.remove_strategy(self.id)\n// after: defer via message/command\ndef on_event(self, event):\n    self.msgbus.send(\"trader.remove_strategy\", self.id)  # executed outside the borrow","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    trader.remove_strategy(strategy_id)\nexcept Exception as e:\n    if \"Cannot clear external order claims\" in str(e):\n        logging.error(\"cache is borrowed elsewhere; defer removal: %s\", e)\n    raise","preventionTips":["Never call trader removal/lifecycle APIs from inside strategy or actor callbacks that hold the cache","Use the message bus to defer commands out of the current borrow scope","Keep cache access short-lived and non-nested"],"tags":["cache","borrow-error","strategy"],"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"}