{"record":{"id":"65d4a6d5897b3e55","repo":"nautechsystems/nautilus_trader","slug":"derive-execution-startup-teardown-failed-teardow","errorCode":null,"errorMessage":"Derive execution startup teardown failed: {teardown_error}","messagePattern":"Derive execution startup teardown failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/derive/src/execution.rs","lineNumber":647,"sourceCode":"            if let Err(teardown_error) = self.teardown_partial_connect().await {\n                return Err(e.context(format!(\n                    \"Derive execution startup teardown failed: {teardown_error}\"\n                )));\n            }\n            return Err(e);\n        };\n\n        let subaccount_id = self.credential.subaccount_id();\n        let channels = vec![\n            DeriveWsChannel::orders(subaccount_id),\n            DeriveWsChannel::private_trades(subaccount_id),\n            DeriveWsChannel::balances(subaccount_id),\n        ];\n\n        if let Err(e) = self.ws_client.subscribe_channels(channels).await {\n            log::warn!(\"Derive private WS subscriptions failed: {e}; tearing down\");\n            if let Err(teardown_error) = self.teardown_partial_connect().await {\n                return Err(anyhow::Error::new(e).context(format!(\n                    \"Derive execution startup teardown failed: {teardown_error}\"\n                )));\n            }\n            return Err(anyhow::Error::new(e).context(\"failed Derive private WS subscriptions\"));\n        }\n\n        if let Err(e) = self.start_ws_dispatch(rx) {\n            if let Err(teardown_error) = self.teardown_partial_connect().await {\n                return Err(e.context(format!(\n                    \"Derive execution startup teardown failed: {teardown_error}\"\n                )));\n            }\n            return Err(e.context(\"failed to register Derive execution WebSocket dispatch task\"));\n        }\n\n        // Fail-fast if the initial account snapshot cannot load: without it,\n        // `await_account_registered` would block the full timeout window and\n        // surface a misleading registration timeout. Tear down the WS we","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/derive/src/execution.rs#L629-L665","documentation":"This error is returned by Derive execution client connect() when subscribing to the private WebSocket channels (orders, balances) fails AND the partial-connect teardown also fails. The original subscription error is preserved as the source, with the teardown error appended, telling the caller the startup failed and cleanup could not complete either — the client may be left in a partially-connected state.","triggerScenarios":"ws_client.subscribe_channels(channels) for the Derive private channels returns Err, then teardown_partial_connect().await also returns Err.","commonSituations":"Derive API auth failure (bad API credentials) causing subscription rejection, followed by a teardown that itself errors (broken WS state); network drop during startup; ws_client in an inconsistent state after the failed subscribe.","solutions":["Check the chained source error for the original subscription failure cause (auth, network)","Force-disconnect or recreate the ws_client since teardown failed and state may be inconsistent","Verify Derive API credentials and subaccount_id are valid before connecting","Retry connect() with a freshly constructed execution client","Check Derive service status / network reachability if subscriptions time out"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Validate credentials and connectivity before subscribing\nif !derive_credentials_valid(config) {\n    return Err(anyhow!(\"invalid Derive credentials; refusing connect\"));\n}","typeGuard":"fn is_teardown_failure(err: &anyhow::Error) -> bool {\n    err.to_string().contains(\"startup teardown failed\")\n}","tryCatchPattern":"match exec_client.connect().await {\n    Err(e) if is_teardown_failure(&e) => {\n        // subscribe AND teardown failed: client state unknown, recreate it\n        let fresh = DeriveExecutionClient::new(config)?;\n        fresh.connect().await\n    }\n    other => other,\n}","preventionTips":["Verify Derive API credentials and subaccount_id before connect","Recreate the client when teardown fails — state may be inconsistent","Monitor Derive service status for outages","Keep connect/teardown logic symmetrical and tested"],"tags":["websocket","derive","subscription","startup"],"backgroundTag":"websocket-connect-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"}