{"record":{"id":"d31c91368cf2f975","repo":"nautechsystems/nautilus_trader","slug":"cannot-submit-order-order-client-order-id-cac","errorCode":null,"errorMessage":"Cannot submit order {order.client_order_id()}: cache is currently borrowed","messagePattern":"Cannot submit order (.+?): cache is currently borrowed","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/strategy/mod.rs","lineNumber":2359,"sourceCode":"    let market_exit_tag = core.market_exit_tag;\n    let is_market_exit_order = order\n        .tags()\n        .is_some_and(|tags| tags.contains(&market_exit_tag));\n    let should_deny_for_market_exit =\n        core.is_exiting && !order.is_reduce_only() && !is_market_exit_order;\n\n    if should_deny_for_market_exit {\n        strategy.deny_order(order, Ustr::from(\"MARKET_EXIT_IN_PROGRESS\"));\n        return Ok(());\n    }\n\n    let core = StrategyNative::strategy_core_mut(strategy);\n    let params = params.filter(|params| !params.is_empty());\n\n    {\n        let cache_rc = core.cache_rc();\n        let mut cache = cache_rc.try_borrow_mut().map_err(|_| {\n            anyhow::anyhow!(\n                \"Cannot submit order {}: cache is currently borrowed\",\n                order.client_order_id()\n            )\n        })?;\n        cache.add_order(order.clone(), position_id, client_id, true)?;\n    }\n\n    publish_order_initialized(order);\n\n    let command = SubmitOrder::new(\n        trader_id,\n        client_id,\n        strategy_id,\n        order.instrument_id(),\n        order.client_order_id(),\n        order.init_event().clone(),\n        order.exec_algorithm_id(),\n        position_id,","sourceCodeStart":2341,"sourceCodeEnd":2377,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/strategy/mod.rs#L2341-L2377","documentation":"Raised in `submit_order_native` when the strategy cache is already mutably (or immutably) borrowed at the point the binding tries `try_borrow_mut`, so the new order cannot be added to the cache. This is a runtime borrow-check guard of the RefCell-based cache, not a domain error.","triggerScenarios":"Calling `submit_order` from a callback/re-entrant path (e.g. inside an event handler or from `binding_submit_order`) while another piece of code still holds a borrow of the strategy cache on the same thread.","commonSituations":"Submitting an order directly from within on_order/on_event/order-book callbacks that hold a cache borrow, or nested strategy calls where an earlier `cache.borrow()` was not released.","solutions":["Do not submit orders from inside callbacks that hold a cache borrow; defer via a message queue or flag.","Scope cache borrows tightly (drop them before calling back into strategy methods).","Avoid holding the result of cache.borrow()/borrow_mut() across calls into strategy APIs.","Restructure so submission happens after event handling completes (e.g. in the next timer tick)."],"exampleFix":"// before\nfn on_order(&mut self, event: &OrderEventAny) {\n    self.submit_order(new_order)?; // cache still borrowed here\n}\n// after\nfn on_order(&mut self, event: &OrderEventAny) {\n    self.pending_submits.push(new_order); // submit after borrow released\n}","handlingStrategy":"try-catch","validationCode":"// No pre-call check exists; ensure no live cache borrow on the calling path\n// (avoid calling submit_order inside on_event/on_order callbacks that borrow cache)","typeGuard":null,"tryCatchPattern":"if let Err(e) = strategy.submit_order(&order) {\n    if e.to_string().contains(\"cache is currently borrowed\") {\n        deferred_submits.push(order); // retry after handler returns\n    }\n}","preventionTips":["Never submit orders from inside callbacks holding a cache borrow","Scope cache.borrow()/borrow_mut() tightly and drop before re-entering strategy APIs","Queue submissions and flush them after event handling completes"],"tags":["rust","refcell","borrow-checker","reentrancy"],"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"}