{"record":{"id":"98a33dbbc3076cce","repo":"nautechsystems/nautilus_trader","slug":"betfair-execution-startup-teardown-failed-teardo","errorCode":null,"errorMessage":"Betfair execution startup teardown failed: {teardown_error}","messagePattern":"Betfair execution startup teardown failed: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/betfair/src/execution.rs","lineNumber":1564,"sourceCode":"        };\n        let state_sink = match self.socket_control.as_ref() {\n            Some(control) => control.sink_with(halt_on_disconnect),\n            None => SocketStateSink::new(halt_on_disconnect),\n        };\n\n        let stream_client_result = BetfairStreamClient::connect_with_state_sink(\n            &self.credential,\n            session_token,\n            handler,\n            self.stream_config.clone(),\n            HeartbeatTimeoutSource::Server,\n            Some(state_sink),\n        )\n        .await;\n        let stream_client = match stream_client_result {\n            Ok(stream_client) => stream_client,\n            Err(e) => {\n                let e = anyhow::Error::new(e);\n                if let Err(teardown_error) = self.teardown_partial_connect().await {\n                    return Err(e.context(format!(\n                        \"Betfair execution startup teardown failed: {teardown_error}\"\n                    )));\n                }\n                return Err(e);\n            }\n        };\n\n        let stream_client = Arc::new(stream_client);\n\n        if let Err(e) = stream_client.subscribe_orders(None, None).await {\n            if let Err(close_error) = stream_client.close().await {\n                log::warn!(\n                    \"Failed to close Betfair order stream after subscribe failure: {close_error}\"\n                );\n            }\n            let e = anyhow::Error::new(e);","sourceCodeStart":1546,"sourceCodeEnd":1582,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/betfair/src/execution.rs#L1546-L1582","documentation":"Raised in the Betfair execution client's `connect` when constructing the stream client fails and the subsequent `teardown_partial_connect` also errors; the teardown error is attached as anyhow context to the original error. It signals both a failed startup and a failed partial-cleanup.","triggerScenarios":"Stream client creation (auth/token handshake to Betfair stream API) errors, and teardown_partial_connect cannot cleanly unwind already-started resources (e.g. execution client already spawned, order stream partially established).","commonSituations":"Expired/invalid Betfair session token, network drop mid-handshake, node stopping while connect is in flight, or misconfigured Betfair app key.","solutions":["Inspect the root error source (chain) — fix the original stream connection failure first (credentials, app key, network).","Check logs for the teardown_partial_connect error to identify the resource that failed cleanup.","Refresh Betfair credentials/session token and retry connect.","If teardown persistently fails, look for leaked stream/heartbeat tasks in the adapter."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Verify Betfair session token validity before connect\n// e.g. call keep_alive / login API and check HTTP 200","typeGuard":null,"tryCatchPattern":"match exec_client.connect().await {\n    Err(e) => {\n        log::error!(\"Betfair connect failed: {e:#}\"); // prints full chain incl. teardown\n        refresh_session_token().await?;\n        retry_with_backoff().await;\n    }\n    Ok(_) => {},\n}","preventionTips":["Refresh Betfair session tokens before/at expiry","Validate app key and credentials in config","Use backoff-based reconnect instead of tight retry loops"],"tags":["rust","adapter","betfair","connect"],"backgroundTag":"connection-refused","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"}