{"record":{"id":"1b525e1333f0d1a1","repo":"nautechsystems/nautilus_trader","slug":"failed-to-register-betfair-keep-alive-task-e","errorCode":null,"errorMessage":"Failed to register Betfair keep-alive task: {e}","messagePattern":"Failed to register Betfair keep-alive task: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/betfair/src/data.rs","lineNumber":1031,"sourceCode":"                            }\n                        };\n\n                        let _ = keep_alive_client\n                            .with_session_token(|token| {\n                                refresh_stream_sessions(\n                                    keep_alive_stream.as_ref(),\n                                    keep_alive_race_stream.as_deref(),\n                                    keep_alive_cricket_stream.as_deref(),\n                                    &keep_alive_app_key,\n                                    token,\n                                    session_replaced,\n                                );\n                            })\n                            .await;\n                        log::debug!(\"Betfair session keep-alive sent\");\n                    }\n                })\n                .map_err(|e| anyhow::anyhow!(\"Failed to register Betfair keep-alive task: {e}\"))?;\n\n            let reconnect_http = Arc::clone(&self.http_client);\n            let reconnect_stream = Arc::clone(self.stream_client.as_ref().unwrap());\n            let reconnect_race_stream = self.race_stream_client.as_ref().map(Arc::clone);\n            let reconnect_cricket_stream = self.cricket_stream_client.as_ref().map(Arc::clone);\n            let reconnect_app_key = self.credential.app_key().to_string();\n\n            self.session_tasks\n                .spawn(async move {\n                    while reconnect_rx.recv().await.is_some() {\n                        log::info!(\"Handling data stream reconnection\");\n\n                        let session_replaced = match reconnect_http.keep_alive_with_token().await {\n                            Ok(_) => false,\n                            Err(ref e) if e.is_login_failed() => {\n                                log::warn!(\n                                    \"Session expired on reconnect, attempting re-login: {e}\"\n                                );","sourceCodeStart":1013,"sourceCodeEnd":1049,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/betfair/src/data.rs#L1013-L1049","documentation":"During BetfairMarketDataClient::connect, a background keep-alive task is spawned to periodically re-send the keepAlive message and prevent the Betfair session from expiring. If the task registration itself fails (e.g. the runtime/task registry rejects the spawn), connect returns this anyhow error and the whole connect sequence fails.","triggerScenarios":"The keep-alive task registration future returns Err while connect is running — e.g. the tokio runtime is shutting down, the registration channel is closed, or the underlying task-generation registry refuses a new task after shutdown was begun.","commonSituations":"Connecting during adapter teardown or executor shutdown; nested runtimes or blocking a runtime thread so the registration future cannot complete; calling connect concurrently with disconnect.","solutions":["Retry connect when the runtime is fully running, not during shutdown","Ensure connect is called on a live multi-threaded tokio runtime and never concurrently with disconnect/teardown","Inspect the inner error `e` in the message for the root registration failure"],"exampleFix":"// before: connect called during app shutdown\nmarket_client.connect().await?;\n// after: connect only while the runtime is healthy\nassert!(!shutting_down.load(Ordering::SeqCst));\nmarket_client.connect().await?;","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["rust","tokio","betfair","task-spawn","connection-setup"],"backgroundTag":"network-request-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"}