nautechsystems/nautilus_trader · error

Python on_queue_state failed: {e}

Error message

Python on_queue_state failed: {e}

What it means

Wraps any failure raised when the Rust core dispatches a `QueueStateChanged` event into the Python actor's `on_queue_state` handler. The prefix 'Python on_queue_state failed' indicates the exception originated in the Python-side handler or the dispatch call into Python, and Rust re-raised it as anyhow.

Source

Thrown at crates/common/src/python/actor.rs:1084

    }

    #[allow(unused_variables)]
    fn on_data(&mut self, data: &CustomData) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_data: Py<PyAny> = Py::new(py, data.clone())?.into_any();
            self.dispatch_on_data(py_data)
                .map_err(|e| anyhow::anyhow!("Python on_data failed: {e}"))
        })
    }

    fn on_signal(&mut self, signal: &Signal) -> anyhow::Result<()> {
        self.dispatch_on_signal(signal)
            .map_err(|e| anyhow::anyhow!("Python on_signal failed: {e}"))
    }

    fn on_queue_state(&mut self, event: &QueueStateChanged) -> anyhow::Result<()> {
        self.dispatch_on_queue_state(event)
            .map_err(|e| anyhow::anyhow!("Python on_queue_state failed: {e}"))
    }

    fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {
        self.dispatch_on_socket_state(event)
            .map_err(|e| anyhow::anyhow!("Python on_socket_state failed: {e}"))
    }

    fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_instrument = instrument_any_to_pyobject(py, instrument.clone())
                .map_err(|e| anyhow::anyhow!("Failed to convert InstrumentAny to Python: {e}"))?;
            self.dispatch_on_instrument(py_instrument)
                .map_err(|e| anyhow::anyhow!("Python on_instrument failed: {e}"))
        })
    }

    fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {
        self.dispatch_on_quote(*quote)

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the wrapped `{e}` message/traceback and fix the exception in your `on_queue_state` override
  2. Check the handler signature is `on_queue_state(self, event)` receiving a QueueStateChanged
  3. Make the handler defensive: validate queue state assumptions before acting
  4. Add logging around queue-state handling during development to capture the failing state

Example fix

// before
def on_queue_state(self, event):
    self.pending.remove(event.queue_id)  # KeyError if absent
// after
def on_queue_state(self, event):
    if event.queue_id in self.pending:
        self.pending.remove(event.queue_id)
Defensive patterns

Strategy: try-catch

Validate before calling

def _check_on_queue_state(self, event):
    try:
        self.on_queue_state(event)
    except Exception as e:
        self.log.error(f'on_queue_state failed: {e!r}')

Try / catch

try:
    self.dispatch_on_queue_state(event)
except Exception as e:
    self.log.error(f'Python on_queue_state failed: {e}', exc_info=True)

Prevention

When it happens

Trigger: A queue state change event (queue full/empty transitions on an internal queue) is delivered to a Python actor that overrides `on_queue_state`, and that Python method raises or the dispatch machinery fails.

Common situations: Backpressure handling code in `on_queue_state` that indexes into state that does not exist yet, handler misdeclared with wrong arity, or state mutated concurrently with queue transitions.

Related errors


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