nautechsystems/nautilus_trader · error · anyhow::Error

Cannot cancel order: not connected

Error message

Cannot cancel order: not connected

What it means

cancel_order state guard in the dYdX execution client: a cancel command arrived while the client is not connected, so no WebSocket/HTTP session exists to relay it. The command is rejected instead of being silently dropped.

Source

Thrown at crates/adapters/dydx/src/execution/mod.rs:1899

    ///
    /// Strategies should handle `OrderModifyRejected` by canceling and resubmitting.
    fn modify_order(&self, cmd: ModifyOrder) -> anyhow::Result<()> {
        let reason = "dYdX does not support order modification. Use cancel and resubmit instead.";
        log::error!("{reason}");

        self.send_modify_rejected(
            cmd.strategy_id,
            cmd.instrument_id,
            cmd.client_order_id,
            cmd.venue_order_id,
            reason,
        );
        Ok(())
    }

    fn cancel_order(&self, cmd: CancelOrder) -> anyhow::Result<()> {
        if !self.is_connected() {
            anyhow::bail!("Cannot cancel order: not connected");
        }

        let client_order_id = cmd.client_order_id;
        let instrument_id = cmd.instrument_id;
        let strategy_id = cmd.strategy_id;
        let venue_order_id = cmd.venue_order_id;

        let (order_time_in_force, order_expire_time) = {
            let cache = self.core.cache();

            let order = match cache.order(&client_order_id) {
                Some(order) => order,
                None => {
                    log::error!("Cannot cancel order {client_order_id}: not found in cache");
                    return Ok(()); // Not an error - order may have been filled/canceled already
                }
            };

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Reconnect the client before retrying the cancel
  2. Check network connectivity and dYdX status
  3. On the venue side, verify whether the order still exists once reconnected — it may have filled or expired

Example fix

// before
client.cancel_order(cmd)?;
// after
if client.is_connected() {
    client.cancel_order(cmd)?;
} else {
    client.connect().await?;
    client.cancel_order(cmd)?;
}
Defensive patterns

Strategy: validation

Validate before calling

if !client.is_connected() { return Err(anyhow!("client not connected")); }

Try / catch

match client.cancel_order(cmd) {
    Err(e) if e.to_string().contains("not connected") => { client.connect().await?; client.cancel_order(cmd)?; }
    r => r?,
}

Prevention

When it happens

Trigger: Calling `cancel_order` when `is_connected()` is false — pre-connect, after WebSocket/gRPC drop, or during reconnect.

Common situations: Attempting to flatten positions during a network outage; canceling stale orders after the adapter lost its link; shutdown ordering where the client disconnects before pending cancels flush.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/e9bc57ac65c1631e. Report an issue: GitHub.