{"record":{"id":"441646b17870c2b1","repo":"nautechsystems/nautilus_trader","slug":"cannot-set-external-order-claims-e","errorCode":null,"errorMessage":"Cannot set external order claims: {e}","messagePattern":"Cannot set external order claims: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/strategy/mod.rs","lineNumber":140,"sourceCode":"    ///\n    /// Returns an error if the strategy is not registered, the cache is already borrowed, an\n    /// instrument is repeated, or an instrument is claimed by another strategy.\n    fn set_external_order_instrument_ids(\n        &mut self,\n        instrument_ids: Vec<InstrumentId>,\n    ) -> anyhow::Result<()>\n    where\n        Self: StrategyNative,\n    {\n        let core = StrategyNative::strategy_core_mut(self);\n        let strategy_id = registered_strategy_id(core)?;\n        if !core.actor.is_registered() {\n            anyhow::bail!(\"Strategy {strategy_id} is not registered with a trader\");\n        }\n        let cache = core.cache_rc();\n        cache\n            .try_borrow_mut()\n            .map_err(|e| anyhow::anyhow!(\"Cannot set external order claims: {e}\"))?\n            .set_external_order_claims(strategy_id, &instrument_ids)?;\n        core.config.external_order_instrument_ids = Some(instrument_ids);\n        Ok(())\n    }\n\n    /// Returns the runtime strategy ID, when configured or registered.\n    fn strategy_id(&self) -> Option<StrategyId>\n    where\n        Self: StrategyNative,\n    {\n        StrategyNative::strategy_core(self).strategy_id()\n    }\n\n    /// Returns the user-facing order creation API.\n    fn order(&self) -> OrderApi<'_>\n    where\n        Self: StrategyNative,\n    {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/strategy/mod.rs#L122-L158","documentation":"While setting external order instrument IDs, the strategy borrows the shared Cache mutably via `cache.try_borrow_mut()`. If the Cache RefCell is already borrowed (by this or another actor on the same thread) the borrow fails and the error is re-wrapped as `Cannot set external order claims: {e}`.","triggerScenarios":"Calling `set_external_order_instrument_ids` while the Cache is mutably borrowed — e.g. from within a callback that itself holds a cache borrow, or nested cache access during dispatch on the same thread.","commonSituations":"Holding a cache reference (e.g. from `cache.orders()` iteration or a previous try_borrow_mut result) across the call; calling claims setup from inside `on_event`/handler code while the engine is mid-dispatch; deep call chains where an outer function still holds the borrow.","solutions":["Call `set_external_order_instrument_ids` from `on_start` or another point where no cache borrow is held.","Do not retain cache borrow guards across calls; drop any `cache` handle obtained earlier before invoking.","Restructure so the claims are set outside any cache iteration/dispatch context.","If `{e}` indicates a persistent borrow, look for leaked borrows in custom actor code on the same thread."],"exampleFix":"// before\ndef on_start(self):\n    orders = self.cache.orders_open()  # holds an internal borrow\n    self.set_external_order_instrument_ids([...])  # fails: cache already borrowed\n// after\ndef on_start(self):\n    self.set_external_order_instrument_ids([...])\n    orders = self.cache.orders_open()","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    self.set_external_order_instrument_ids(ids)\nexcept Exception as e:\n    if 'currently borrowed' in str(e):\n        self.clock.set_timer('retry-claims', 0.001, callback=self._retry_claims)\n    else:\n        raise","preventionTips":["Never hold cache handles or iteration state across claims setup","Call claims setup from on_start when no dispatch is in flight","Avoid nested calls into the cache within the same callback stack","Treat cache borrow errors as re-entrancy bugs in your own code"],"tags":["cache","borrow","concurrency","strategy"],"backgroundTag":"internal-invariant-violation","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"}