{"record":{"id":"fefeab91476dae6e","repo":"nautechsystems/nautilus_trader","slug":"failed-to-connect-binance-futures-public-websocket","errorCode":null,"errorMessage":"failed to connect Binance Futures public WebSocket: {e}","messagePattern":"failed to connect Binance Futures public WebSocket: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/adapters/binance/src/futures/data.rs","lineNumber":1540,"sourceCode":"            &self.ws_client,\n            &self.ws_public_client,\n            &self.data_sender,\n            self.clock,\n            false,\n        )\n        .await?;\n\n        log::info!(\"Connecting to Binance Futures market WebSocket...\");\n        self.ws_client.connect().await.map_err(|e| {\n            log::error!(\"Binance Futures market WebSocket connection failed: {e:?}\");\n            anyhow::anyhow!(\"failed to connect Binance Futures market WebSocket: {e}\")\n        })?;\n        log::info!(\"Binance Futures market WebSocket connected\");\n\n        log::info!(\"Connecting to Binance Futures public WebSocket...\");\n        self.ws_public_client.connect().await.map_err(|e| {\n            log::error!(\"Binance Futures public WebSocket connection failed: {e:?}\");\n            anyhow::anyhow!(\"failed to connect Binance Futures public WebSocket: {e}\")\n        })?;\n        log::info!(\"Binance Futures public WebSocket connected\");\n\n        // Spawn market stream handler\n        let stream = self.ws_client.stream();\n        let sender = self.data_sender.clone();\n        let insts = self.instruments.clone();\n        let ws_insts = self.ws_client.instruments_cache();\n        let buffers = self.book_buffers.clone();\n        let book_subs = self.book_subscriptions.clone();\n        let l1_book_subs = self.l1_book_subscriptions.clone();\n        let force_order_refs = self.force_order_refs.clone();\n        let ticker_refs = self.ticker_refs.clone();\n        let force_order_all_market_refs = self.force_order_all_market_refs.clone();\n        let force_order_all_market_stream_active =\n            self.force_order_all_market_stream_active.clone();\n        let book_epoch = self.book_epoch.clone();\n        let http = self.http_client.clone();","sourceCodeStart":1522,"sourceCodeEnd":1558,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/a4b06ed870971b5671d12754ea138a3ab99b1dec/crates/adapters/binance/src/futures/data.rs#L1522-L1558","documentation":"Raised during `connect()` when the second WebSocket — the Binance Futures public market-data client — fails to connect, immediately after the market client succeeded. The underlying error is wrapped with anyhow and logged at ERROR level (`Binance Futures public WebSocket connection failed: {e:?}`). A failure here still fails the entire data client connect, leaving the node with no streams even though the first socket opened.","triggerScenarios":"Same connect sequence as the market client, but the public stream host fails: transient network drop between the two handshakes, firewall/DNS issues specific to the public stream endpoint, Binance connection-limit or rate-limit rejections when too many sockets open rapidly, or region-blocked IPs.","commonSituations":"Flaky networking where the first connect succeeds and the second times out; hitting Binance's per-IP WebSocket connection limits during reconnect storms; CI environments with restricted egress; testnet public stream endpoints under maintenance.","solutions":["Inspect the wrapped `{e}` to distinguish timeout vs handshake rejection vs DNS failure","Retry the full connect with backoff — both sockets are re-established on each attempt","Reduce concurrent Binance WS connections (other clients/tabs/scripts) if hitting connection limits","If persistent, verify the public stream host resolves and a plain TLS connection to port 443 succeeds from the same host"],"exampleFix":"# before: no tolerance for the second handshake failing\nawait node.run()  # raises: failed to connect Binance Futures public WebSocket: {e}\n\n# after: backoff around the whole connect sequence (both sockets)\nfor attempt in range(6):\n    try:\n        await node.run()\n        break\n    except Exception as e:\n        if 'failed to connect Binance Futures' not in str(e):\n            raise\n        await asyncio.sleep(min(2 ** attempt, 30))","handlingStrategy":"retry","validationCode":"import socket\n\ndef binance_ws_reachable(host: str = 'fstream.binance.com', port: int = 443, timeout: float = 5.0) -> bool:\n    try:\n        with socket.create_connection((host, port), timeout=timeout):\n            return True\n    except OSError:\n        return False","typeGuard":null,"tryCatchPattern":"# public client connects right after the market client; retry the whole sequence\nfor attempt in range(6):\n    try:\n        await node.run()\n        break\n    except Exception as e:\n        if 'failed to connect Binance Futures public WebSocket' not in str(e):\n            raise\n        await asyncio.sleep(min(2 ** attempt, 30))\nelse:\n    raise RuntimeError('Binance Futures public WebSocket unreachable after retries')","preventionTips":["Keep concurrent Binance WebSocket connections low to avoid per-IP connection limits during reconnect storms","Reuse a single data client for many instruments (combined streams) instead of many clients","Treat back-to-back connect failures as environmental: test with a plain TLS handshake to the stream host"],"tags":["binance","futures","websocket","connection","network"],"backgroundTag":"websocket-connection-failed","analyzedSha":"a4b06ed870971b5671d12754ea138a3ab99b1dec","analyzedAt":"2026-08-16T22:54:50.089Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}