nautechsystems/nautilus_trader · error

Python on_signal failed: {e}

Error message

Python on_signal failed: {e}

What it means

This error wraps any failure raised while the Rust core dispatches the `on_signal` handler of a Python actor/strategy into its Python implementation. The Rust side called `dispatch_on_signal`, the Python `on_signal` method (or the GIL/dispatch machinery around it) raised, and Rust re-raises it as an anyhow error prefixed with 'Python on_signal failed'. It means the user's Python handler code itself failed, not the signal data transport.

Source

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

    }

    fn on_time_event(&mut self, event: &TimeEvent) -> anyhow::Result<()> {
        self.dispatch_on_time_event(event.clone())
            .map_err(|e| anyhow::anyhow!("Python on_time_event failed: {e}"))
    }

    #[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}"))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the chained `{e}` message to find the original Python traceback and fix the exception raised inside your `on_signal` override
  2. Verify your `on_signal(self, signal)` signature matches what the actor dispatches (single Signal argument)
  3. Guard the handler body with try/except during development and log the full traceback
  4. Confirm the actor was fully initialized/registered before signals can arrive

Example fix

// before
def on_signal(self, signal):
    self.orders.submit(...)  # raises if self.orders is None
// after
def on_signal(self, signal):
    if self.orders is None:
        self.log.warning('orders not ready')
        return
    self.orders.submit(...)
Defensive patterns

Strategy: try-catch

Validate before calling

def _check_on_signal(self, signal):
    assert callable(getattr(self, 'on_signal', None)), 'on_signal not defined'
    try:
        self.on_signal(signal)
    except Exception as e:
        self.log.error(f'on_signal failed: {e!r}')

Try / catch

try:
    self.dispatch_on_signal(signal)
except Exception as e:
    self.log.error(f'Python on_signal failed: {e}', exc_info=True)

Prevention

When it happens

Trigger: A Signal arrives (subscribe_signal was used) and the user's Python `on_signal(self, signal)` override raises an exception, or the dispatch into Python fails (e.g. the handler is not callable or the GIL callback errors).

Common situations: User `on_signal` overrides referencing attributes not yet initialized, typos in the handler signature, raising inside signal-handling logic such as order submission on signal, or a bug in the Python class registered with the trader.

Related errors


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