{"record":{"id":"dc8c3d3b381c0c12","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-lighter-task-generation-e","errorCode":null,"errorMessage":"Failed to start Lighter task generation: {e}","messagePattern":"Failed to start Lighter task generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/lighter/src/execution.rs","lineNumber":4066,"sourceCode":"            );\n        }\n\n        log::info!(\n            \"Connecting Lighter execution client {}\",\n            self.core.client_id\n        );\n\n        // Synchronous stop/reset can only initiate teardown. Complete it before\n        // publishing a replacement socket or sharing its connection epoch.\n        if !self.session_tasks_finished() || !self.pending_tasks.is_open() {\n            self.begin_session_shutdown();\n            self.finish_session_shutdown().await?;\n        }\n\n        if !self.pending_tasks.is_open() {\n            self.pending_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Lighter task generation: {e}\"))?;\n            self.cancellation_token = self.pending_tasks.cancellation_token();\n        }\n\n        let ws_client = self.ws_client.clone();\n        let setup_guard = TaskGroupGuard::new(&[&self.pending_tasks], move || {\n            ws_client.begin_shutdown();\n        });\n        self.auth_refresh_notify = Arc::new(tokio::sync::Notify::new());\n\n        // Reset the readiness gate and clear derived position/account caches\n        // so a prior session's state cannot leak past the strict-await gate.\n        // The Reconnected path (WS-layer transparent reconnect) is unaffected:\n        // it does not re-enter `connect()`. Its next `account_all_positions`\n        // frame replaces the position cache through the consumption loop.\n        self.dispatch.account_streams_ready.reset();\n        self.dispatch.clear_position_cache();\n        self.dispatch.clear_account_state_cache();\n","sourceCodeStart":4048,"sourceCodeEnd":4084,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/lighter/src/execution.rs#L4048-L4084","documentation":"During Lighter connection setup, after finishing any prior session shutdown, the client starts task generation on pending_tasks (a task group). If start_generation() returns an error, it is wrapped in this anyhow error and connection setup aborts. This means the client's internal task scheduler could not begin producing background tasks for the new connection session.","triggerScenarios":"Calling connect/connect_async when the task group's generation counter cannot advance — e.g. the group was already shut down and not reset, or its internal state (token/generation) is in a conflicting state from a previous failed shutdown.","commonSituations":"Rapid reconnect loops where a previous session shutdown raced the new start; cancellation token leaked from a cancelled prior session; calling connect twice concurrently on the same client.","solutions":["Ensure the previous connection's shutdown fully completed before reconnecting (await disconnect/shutdown before connect)","Avoid concurrent connect() calls on the same client instance; serialize connections","If the group is permanently wedged, rebuild/recreate the execution client instead of reusing it"],"exampleFix":"// before\nself.client.connect().await?;\n// after\nself.client.disconnect().await?;\nself.client.connect().await?;","handlingStrategy":"try-catch","validationCode":"if client.is_connected() || client.is_shutting_down() {\n    return Err(anyhow!(\"client busy\"));\n}","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"task generation\") => {\n        // recreate or retry after full shutdown\n    }\n    other => other?,\n}","preventionTips":["Always await full disconnect before reconnecting","Never call connect concurrently on one client","Recreate the client after repeated start_generation failures"],"tags":["rust","lighter","async","task-group","reconnect"],"backgroundTag":"invalid-state-transition","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"}