nautechsystems/nautilus_trader · error · anyhow::Error

Polymarket execution client is shutting down

Error message

Polymarket execution client is shutting down

What it means

cancel_all_orders_command submits the batch cancellation as a spawned task; `spawned` is false when the execution client's task group refuses admission because shutdown has begun. anyhow::ensure! then fails with 'Polymarket execution client is shutting down', signalling the command was not dispatched.

Source

Thrown at crates/adapters/polymarket/src/execution/cancellations.rs:417

                                venue_order_id
                            );
                        }
                    }

                    log::debug!(
                        "Cancel-all completed for instrument_id={instrument_id}: canceled={}, not_canceled={}",
                        response.canceled.len(),
                        response.not_canceled.len()
                    );
                    Ok(())
                }
                Err(e) => {
                    apply_cancel_http_failure(&e, &orders, &emitter, clock);
                    Err(anyhow::Error::new(e).context("failed to cancel all orders"))
                }
            }
        });
        anyhow::ensure!(spawned, "Polymarket execution client is shutting down");

        Ok(())
    }

    pub(super) fn batch_cancel_orders_command(&self, cmd: &BatchCancelOrders) {
        if cmd.cancels.is_empty() {
            return;
        }

        let mut orders = Vec::new();

        for c in &cmd.cancels {
            if let Some(order) = self.core.cache().order(&c.client_order_id) {
                orders.push(order.clone());
            }
        }

        if orders.is_empty() {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the client is connected/not disconnecting before sending commands; stop strategies before disconnect()
  2. Handle the error as 'command not sent' and re-issue after reconnect if the cancel still matters
  3. Serialize shutdown: await disconnect completion only after all order-management commands are drained
  4. Reconnect the client and retry cancellation for orders that may remain live on the venue
Defensive patterns

Strategy: try-catch

Validate before calling

// gate order commands on client liveness
if not exec_client.is_connected:
    raise RuntimeError("execution client not connected; refusing cancel_all")

Try / catch

try:
    exec_client.cancel_all_orders(command)
except Exception as e:
    if "shutting down" in str(e):
        log.warning("cancel-all dropped due to client shutdown; will reconcile after reconnect")
        pending_cancels.append(command)  # re-issue after reconnect

Prevention

When it happens

Trigger: Calling cancel_all_orders after the execution client began disconnect/shutdown, so the spawn used to submit the cancel-all HTTP request is rejected and returns false.

Common situations: Order-management code issuing cancels during application shutdown or after disconnect was initiated; a race between a strategy teardown and an in-flight cancel-all request.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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