{"record":{"id":"89944a94a34d6550","repo":"nautechsystems/nautilus_trader","slug":"python-on-queue-state-failed-e-89944a","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/trading/src/python/algorithm.rs","lineNumber":596,"sourceCode":"    fn on_fault(&mut self) -> anyhow::Result<()> {\n        self.dispatch_no_args(\"on_fault\")\n            .map_err(|e| anyhow::anyhow!(\"Python on_fault failed: {e}\"))\n    }\n\n    fn on_time_event(&mut self, event: &TimeEvent) -> anyhow::Result<()> {\n        ExecutionAlgorithm::on_time_event(self, event)?;\n        self.dispatch_time_event(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_time_event failed: {e}\"))\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\n#[pyo3::pymethods]\n#[pyo3_stub_gen::derive::gen_stub_pymethods]\n#[allow(\n    clippy::large_types_passed_by_value,\n    reason = \"PyO3 callbacks accept Python-owned event values\"\n)]\n#[expect(\n    clippy::unused_self,\n    reason = \"default PyO3 callbacks must remain instance methods\"\n)]","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/algorithm.rs#L578-L614","documentation":"Raised by the Rust `ExecutionAlgorithm` wrapper when the Python callback `on_queue_state` throws. Internal `QueueStateChanged` events (message-queue state transitions) are dispatched to the Python subclass via `dispatch_on_queue_state`; a `PyErr` from the user's handler is wrapped as `anyhow!(\"Python on_queue_state failed: {e}\")`.","triggerScenarios":"A `QueueStateChanged` event is emitted (queue becomes full/drainable/idle) and the registered algorithm's Python subclass overrides `on_queue_state` and raises — typically by assuming attributes or event fields that don't exist, or by raising inside backpressure handling logic.","commonSituations":"Rarely overridden intentionally; hit when a user overrides the hook to monitor queue pressure but references the wrong field name of `QueueStateChanged`, or performs actions (e.g. submitting orders) that themselves fail during backpressure.","solutions":["Inspect the embedded Python traceback to find the failing statement in your `on_queue_state` override","Match the override signature exactly: `def on_queue_state(self, event) -> None:` and only read documented `QueueStateChanged` fields","Guard the handler body with try/except so queue monitoring never disrupts trading","If you don't need the hook, remove the override so the base no-op runs","Check the nautilus_trader changelog for `QueueStateChanged` field changes if this appeared after an upgrade"],"exampleFix":"# before\ndef on_queue_state(self, event):\n    if event.state == QueueState.FULL:  # AttributeError: wrong field\n\n# after\ndef on_queue_state(self, event):\n    try:\n        if event.is_full:\n            self.log.warning(\"Queue full\")\n    except Exception as e:\n        self.log.error(f\"queue-state handler failed: {e}\")","handlingStrategy":"try-catch","validationCode":"sig = inspect.signature(MyAlgo.on_queue_state)\nassert list(sig.parameters) == ['self', 'event']","typeGuard":"def is_queue_state_event(ev):\n    return type(ev).__name__ == 'QueueStateChanged'","tryCatchPattern":"try:\n    algo.on_queue_state(event)\nexcept Exception as e:\n    log.error(f\"on_queue_state handler failed: {e}\")  # never let monitoring hooks break trading","preventionTips":["Treat queue-state hooks as observability only; do not trade from them","Read only documented `QueueStateChanged` fields — no guessed attribute names","Wrap the handler body in try/except so monitoring never raises","Skip the override entirely if you don't need queue telemetry"],"tags":["python","callback","queue","rust","execution-algorithm"],"backgroundTag":"api-error-response","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"}