{"record":{"id":"6e1991c2410706cf","repo":"nautechsystems/nautilus_trader","slug":"python-on-socket-state-failed-e","errorCode":null,"errorMessage":"Python on_socket_state failed: {e}","messagePattern":"Python on_socket_state failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1089,"sourceCode":"            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)\n            .map_err(|e| anyhow::anyhow!(\"Python on_quote failed: {e}\"))\n    }\n\n    fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {\n        self.dispatch_on_trade(*tick)","sourceCodeStart":1071,"sourceCodeEnd":1107,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1071-L1107","documentation":"Wraps any failure raised when the Rust core dispatches a `SocketStateChanged` event into the Python actor's `on_socket_state` handler. The error indicates the Python handler raised (or dispatch into Python failed); Rust re-raises with the 'Python on_socket_state failed' prefix.","triggerScenarios":"A socket state change event is delivered to a Python actor overriding `on_socket_state`, and the Python method raises an exception or cannot be dispatched (e.g. not callable).","commonSituations":"Connection/reconnect logic in `on_socket_state` that touches sockets or clients not yet created, wrong handler arity, or exceptions raised while reacting to Disconnected/Connected transitions.","solutions":["Read the chained `{e}` message for the underlying Python traceback and fix the handler","Verify the override signature `on_socket_state(self, event)` with a SocketStateChanged argument","Handle all socket-state enum cases (Connected, Disconnected, etc.) without assuming prior state","Wrap risky reconnect logic in try/except and log instead of raising"],"exampleFix":"// before\ndef on_socket_state(self, event):\n    self.client.close()  # AttributeError when never connected\n// after\ndef on_socket_state(self, event):\n    if self.client is not None:\n        self.client.close()","handlingStrategy":"try-catch","validationCode":"def _check_on_socket_state(self, event):\n    try:\n        self.on_socket_state(event)\n    except Exception as e:\n        self.log.error(f'on_socket_state failed: {e!r}')","typeGuard":null,"tryCatchPattern":"try:\n    self.dispatch_on_socket_state(event)\nexcept Exception as e:\n    self.log.error(f'Python on_socket_state failed: {e}', exc_info=True)","preventionTips":["Handle Disconnected state when resources may not exist","Never assume connection order in on_socket_state","Test reconnect/disconnect paths explicitly"],"tags":["python","actor","callback","socket"],"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-14T05:17:10.506Z"}