{"record":{"id":"a3c7ff822a276c28","repo":"nautechsystems/nautilus_trader","slug":"python-on-queue-state-failed-e","errorCode":null,"errorMessage":"Python on_queue_state failed: {e}","messagePattern":"Python on_queue_state failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1084,"sourceCode":"    }\n\n    #[allow(unused_variables)]\n    fn on_data(&mut self, data: &CustomData) -> anyhow::Result<()> {\n        Python::attach(|py| {\n            let py_data: Py<PyAny> = Py::new(py, data.clone())?.into_any();\n            self.dispatch_on_data(py_data)\n                .map_err(|e| anyhow::anyhow!(\"Python on_data failed: {e}\"))\n        })\n    }\n\n    fn on_signal(&mut self, signal: &Signal) -> anyhow::Result<()> {\n        self.dispatch_on_signal(signal)\n            .map_err(|e| anyhow::anyhow!(\"Python on_signal failed: {e}\"))\n    }\n\n    fn on_queue_state(&mut self, event: &QueueStateChanged) -> anyhow::Result<()> {\n        self.dispatch_on_queue_state(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_queue_state failed: {e}\"))\n    }\n\n    fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {\n        self.dispatch_on_socket_state(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_socket_state failed: {e}\"))\n    }\n\n    fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {\n        Python::attach(|py| {\n            let py_instrument = instrument_any_to_pyobject(py, instrument.clone())\n                .map_err(|e| anyhow::anyhow!(\"Failed to convert InstrumentAny to Python: {e}\"))?;\n            self.dispatch_on_instrument(py_instrument)\n                .map_err(|e| anyhow::anyhow!(\"Python on_instrument failed: {e}\"))\n        })\n    }\n\n    fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {\n        self.dispatch_on_quote(*quote)","sourceCodeStart":1066,"sourceCodeEnd":1102,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1066-L1102","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the wrapped `{e}` message/traceback and fix the exception in your `on_queue_state` override","Check the handler signature is `on_queue_state(self, event)` receiving a QueueStateChanged","Make the handler defensive: validate queue state assumptions before acting","Add logging around queue-state handling during development to capture the failing state"],"exampleFix":"// before\ndef on_queue_state(self, event):\n    self.pending.remove(event.queue_id)  # KeyError if absent\n// after\ndef on_queue_state(self, event):\n    if event.queue_id in self.pending:\n        self.pending.remove(event.queue_id)","handlingStrategy":"try-catch","validationCode":"def _check_on_queue_state(self, event):\n    try:\n        self.on_queue_state(event)\n    except Exception as e:\n        self.log.error(f'on_queue_state failed: {e!r}')","typeGuard":null,"tryCatchPattern":"try:\n    self.dispatch_on_queue_state(event)\nexcept Exception as e:\n    self.log.error(f'Python on_queue_state failed: {e}', exc_info=True)","preventionTips":["Handle all QueueStateChanged cases explicitly","Avoid assuming queue contents/state in the handler","Backtest with queue-full scenarios enabled"],"tags":["python","actor","callback","queue"],"backgroundTag":"python-callback-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}