{"record":{"id":"300a7603d5ab5c41","repo":"nautechsystems/nautilus_trader","slug":"cannot-cancel-polymarket-orders-while-a-modificati","errorCode":null,"errorMessage":"Cannot cancel Polymarket orders while a modification or cancellation is in flight","messagePattern":"Cannot cancel Polymarket orders while a modification or cancellation is in flight","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/cancellations.rs","lineNumber":319,"sourceCode":"            log::debug!(\n                \"No cached {side:?} orders to cancel for instrument_id={}\",\n                cmd.instrument_id\n            );\n            return Ok(());\n        }\n\n        let cancel_guard = if side.is_none() {\n            CancelCommandGuard::market(self.ws_dispatch_state.clone(), cmd.instrument_id)\n        } else {\n            let pending = open_orders\n                .iter()\n                .map(|order| (order.client_order_id(), order.instrument_id()))\n                .collect::<Vec<_>>();\n            CancelCommandGuard::available_orders(self.ws_dispatch_state.clone(), &pending)\n        };\n\n        let Some(cancel_guard) = cancel_guard else {\n            anyhow::bail!(\n                \"Cannot cancel Polymarket orders while a modification or cancellation is in flight\"\n            );\n        };\n\n        let mut orders = Vec::new();\n\n        for order in open_orders {\n            if side.is_some()\n                && !cancel_guard\n                    .client_order_ids\n                    .contains(&order.client_order_id())\n            {\n                continue;\n            }\n\n            if let Some(venue_order_id) = self.cancel_venue_order_id(&order) {\n                orders.push((venue_order_id, order.clone()));\n            } else {","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/cancellations.rs#L301-L337","documentation":"The execution client serializes cancellation flows with a CancelCommandGuard: only one modify/cancel operation may be in flight per client. cancel_all_orders_command acquires this guard; if a concurrent modification or cancellation is still in flight, acquiring the guard fails and the cancel-all command is rejected rather than racing with the in-flight operation.","triggerScenarios":"Calling cancel_all_orders (via cancel_all_orders_command) while another order modification or cancellation request is currently in flight, so no CancelCommandGuard is available for the pending orders.","commonSituations":"Risk/kill-switch logic issuing cancel-all from a separate thread or event handler while the strategy is concurrently amending orders; rapid-fire cancel-all calls without waiting for the previous one to complete; WebSocket in-flight modify requests overlapping a market-wide flatten.","solutions":["Wait for the in-flight modify/cancel operation to complete before issuing cancel_all_orders, then retry.","Serialize order management so risk-driven cancel-all and strategy order amendments don't run concurrently.","Add retry-with-backoff around cancel_all_orders for this transient contention error.","Check whether the in-flight modify is stuck (e.g. no WS response) and reset/reconnect the execution WebSocket if needed."],"exampleFix":"// before\nclient.cancel_all_orders(command)?; // may fail while modify in flight\n\n// after\nmatch client.cancel_all_orders(command) {\n    Ok(()) => {},\n    Err(e) if is_cancel_guard_busy(&e) => {\n        std::thread::sleep(Duration::from_millis(200));\n        client.cancel_all_orders(command)?;\n    }\n    Err(e) => return Err(e),\n}","handlingStrategy":"retry","validationCode":"// Track in-flight modify/cancel operations in your strategy layer before issuing cancel-all\nlet busy = inflight_modifies.load(Ordering::SeqCst) > 0;\nif busy { wait_for_quiescence()?; }","typeGuard":null,"tryCatchPattern":"for attempt in 0..5 {\n    match client.cancel_all_orders(cmd.clone()) {\n        Ok(()) => break,\n        Err(e) if e.to_string().contains(\"in flight\") => {\n            std::thread::sleep(Duration::from_millis(200 * (attempt + 1)));\n        }\n        Err(e) => return Err(e),\n    }\n}","preventionTips":["Serialize risk flatten and strategy order amendments.","Apply retry/backoff to cancel-all for transient contention.","Monitor for stuck in-flight modifications and reconnect the execution WS if they hang."],"tags":["polymarket","execution","concurrency","cancel-orders"],"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"}