{"record":{"id":"9d9f047ad49fb48b","repo":"nautechsystems/nautilus_trader","slug":"python-on-instrument-close-failed-e","errorCode":null,"errorMessage":"Python on_instrument_close failed: {e}","messagePattern":"Python on_instrument_close failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1153,"sourceCode":"\n    fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_index_price(*index_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_index_price failed: {e}\"))\n    }\n\n    fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_funding_rate(*funding_rate)\n            .map_err(|e| anyhow::anyhow!(\"Python on_funding_rate failed: {e}\"))\n    }\n\n    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {\n        self.dispatch_on_instrument_status(*data)\n            .map_err(|e| anyhow::anyhow!(\"Python on_instrument_status failed: {e}\"))\n    }\n\n    fn on_instrument_close(&mut self, update: &InstrumentClose) -> anyhow::Result<()> {\n        self.dispatch_on_instrument_close(*update)\n            .map_err(|e| anyhow::anyhow!(\"Python on_instrument_close failed: {e}\"))\n    }\n\n    fn on_option_greeks(&mut self, greeks: &OptionGreeks) -> anyhow::Result<()> {\n        self.dispatch_on_option_greeks(*greeks)\n            .map_err(|e| anyhow::anyhow!(\"Python on_option_greeks failed: {e}\"))\n    }\n\n    fn on_option_chain(&mut self, slice: &OptionChainSlice) -> anyhow::Result<()> {\n        self.dispatch_on_option_chain(slice.clone())\n            .map_err(|e| anyhow::anyhow!(\"Python on_option_chain failed: {e}\"))\n    }\n\n    #[cfg(feature = \"defi\")]\n    fn on_block(&mut self, block: &Block) -> anyhow::Result<()> {\n        self.dispatch_on_block(block.clone())\n            .map_err(|e| anyhow::anyhow!(\"Python on_block failed: {e}\"))\n    }\n","sourceCodeStart":1135,"sourceCodeEnd":1171,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1135-L1171","documentation":"This error wraps any Python exception raised inside the actor's `on_instrument_close` callback when an `InstrumentClose` event is delivered to the Python subclass. The Rust `PyDataActor` calls the Python method via `call_method1` and converts the resulting `PyErr` into an anyhow error with this message. The real failure is in the user's Python handler or the Rust-to-Python conversion of the event.","triggerScenarios":"Any exception raised by the user's `on_instrument_close(self, update)` override — e.g. closing positions for an instrument not tracked in the strategy's state, removing from a list while iterating, or dereferencing fields absent from the converted InstrumentClose.","commonSituations":"Exchanges delisting or closing instruments mid-session while a strategy still holds subscriptions; handlers that assume a close always follows an open for the same instrument_id; stale version assumptions after a nautilus_trader upgrade.","solutions":["Inspect the Python traceback appended after 'Python on_instrument_close failed:' to find the raising line.","Make the handler idempotent: check the instrument exists in strategy state before cleanup.","Verify InstrumentClose field names against the installed nautilus_trader version.","Clean up subscriptions/state defensively (use .pop/.get with defaults)."],"exampleFix":"// before\ndef on_instrument_close(self, update):\n    self.subs.pop(update.instrument_id.value)\n    del self.books[update.instrument_id.value]\n\n// after\ndef on_instrument_close(self, update):\n    iid = str(update.instrument_id)\n    self.subs.pop(iid, None)\n    self.books.pop(iid, None)","handlingStrategy":"validation","validationCode":"iid = str(update.instrument_id)\nif iid not in self.books and iid not in self.subs:\n    return  # nothing to clean up","typeGuard":"def is_tracked_close(update, state):\n    return str(getattr(update, \"instrument_id\", \"\")) in state.tracked","tryCatchPattern":"def on_instrument_close(self, update):\n    try:\n        self._cleanup(update)\n    except Exception as e:\n        self.log.error(f\"on_instrument_close failed: {e}\", exc_info=True)","preventionTips":["Make cleanup idempotent with dict.pop(key, None)","Never assume a close follows a tracked open for the same instrument","Log close events so delistings mid-session are auditable","Verify InstrumentClose fields against the installed version"],"tags":["python","rust-bridge","callback-exception","actor"],"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"}