{"record":{"id":"9f21c267f25ddfa8","repo":"nautechsystems/nautilus_trader","slug":"ws-session-authentication-timed-out","errorCode":null,"errorMessage":"WS session authentication timed out","messagePattern":"WS session authentication timed out","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/binance/src/spot/execution.rs","lineNumber":2210,"sourceCode":"    setup_errors: &mut tokio::sync::mpsc::UnboundedReceiver<String>,\n    timeout_message: &'static str,\n) -> anyhow::Result<()> {\n    tokio::pin!(success);\n\n    let result = tokio::time::timeout(timeout, async {\n        tokio::select! {\n            () = &mut success => Ok(()),\n            err = setup_errors.recv() => {\n                anyhow::bail!(\n                    \"{}\",\n                    err.unwrap_or_else(|| \"WS setup error channel closed\".to_string()),\n                )\n            }\n        }\n    })\n    .await;\n\n    result.map_err(|_| anyhow::anyhow!(timeout_message))?\n}\n\n#[expect(clippy::too_many_arguments)]\nfn dispatch_ws_trading_message(\n    msg: BinanceSpotWsTradingMessage,\n    emitter: &ExecutionEventEmitter,\n    http_client: &BinanceSpotHttpClient,\n    account_id: AccountId,\n    treat_expired_as_canceled: bool,\n    clock: &'static AtomicTime,\n    dispatch_state: &WsDispatchState,\n    ws_authenticated: &tokio::sync::Notify,\n    ws_user_data_subscribed: &tokio::sync::Notify,\n    ws_setup_error_tx: &tokio::sync::mpsc::UnboundedSender<String>,\n    seen_trade_ids: &std::sync::Arc<Mutex<FifoCache<(Ustr, i64), 10_000>>>,\n    task_spawner: &TaskSpawner,\n) {\n    match msg {","sourceCodeStart":2192,"sourceCodeEnd":2228,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/binance/src/spot/execution.rs#L2192-L2228","documentation":"The Binance Spot WebSocket trading session was authenticated (listenKey flow) but no WS setup/response message arrived before the configured timeout, so wait_for_ws_setup_response converts the empty result into this error. Without the setup response the trading stream cannot be used, so connect() aborts.","triggerScenarios":"Binance WS endpoint unreachable or slow during connect(); the dispatched trading message never matched the setup-response variant; network latency exceeding the timeout used by wait_for_ws_setup_response.","commonSituations":"Binance API outage or regional blockage; firewall/proxy dropping wss connections; overly aggressive timeout configured; listenKey keep-alive failure mid-handshake.","solutions":["Increase the WS setup timeout value used when connecting","Check network connectivity / proxy settings to wss:// endpoints for the configured Binance cluster","Retry connect() — this is transient under exchange load","Verify the adapter targets the correct Binance URL (spot vs testnet) so the setup response arrives"],"exampleFix":"// before\nlet timeout_secs = 5;\n// after\nlet timeout_secs = 30; // allow for exchange latency/outages","handlingStrategy":"retry","validationCode":"// Pre-check network reachability of the WS endpoint before connect\n// (e.g. TCP connect to the wss host with a short timeout)","typeGuard":null,"tryCatchPattern":"for attempt in 0..3 {\n    match client.connect().await {\n        Ok(()) => break,\n        Err(e) if e.to_string().contains(\"authentication timed out\") => {\n            tokio::time::sleep(Duration::from_secs(2 * (attempt + 1))).await;\n        },\n        Err(e) => return Err(e),\n    }\n}","preventionTips":["Configure a generous WS setup timeout (>=30s) for Binance spot","Monitor Binance status page / known outage windows","Ensure firewall or proxy allows outbound wss on port 443 to the correct cluster","Handle listenKey keep-alive correctly so sessions aren't dropped mid-handshake"],"tags":["binance","websocket","timeout","authentication"],"backgroundTag":"request-timeout","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}