Skip to content

[SPOT WebsocketStream klines] Duplicate WebSocket reconnection triggered after 24h server disconnect causing stream bugs #505

@elwitch

Description

@elwitch

When using the SPOT WebsocketStream to subscribe to multiple klines streams, after each 24-hour server disconnect, each WebSocket connection attempts to reconnect twice per connection and each reconnect recreated both sockets instead of only the one that needed reconnecting. creating duplicate connection attempts then double and so on.

logs-duplicate websocket.txt

sample Example logs

2026-03-16 13:54:08,227 | INFO | [WS DEBUG] Open candle skipped bnbusdt_2h
2026-03-16 13:54:08,434 | INFO | [WS DEBUG] Open candle skipped xrpusdt_2h
2026-03-16 13:54:10,101 | INFO | WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 closed.
2026-03-16 13:54:10,130 | INFO | [WS DEBUG] Open candle skipped ethusdt_2h
2026-03-16 13:54:10,975 | INFO | WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 closed.
2026-03-16 13:54:10,978 | INFO | WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e closed.
2026-03-16 13:54:11,895 | INFO | WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e closed.
2026-03-16 13:54:15,105 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:15,977 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:15,982 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:16,046 | INFO | Establishing Websocket connection with id 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:16,047 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:16,897 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:16,930 | INFO | Establishing Websocket connection with id d3b0f6bd-b08a-4082-9b18-85297052ff5e to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:16,931 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:16,969 | INFO | Establishing Websocket connection with id 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:16,970 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:16,972 | INFO | Establishing Websocket connection with id 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:16,973 | INFO | Sending message to WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4: {'method': 'SUBSCRIBE', 'params': ['btcusdt@kline_2h'], 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:16,974 | INFO | Sending message to WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4: {'method': 'SUBSCRIBE', 'params': ['bnbusdt@kline_2h'], 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:16,975 | INFO | Sending message to WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4: {'method': 'SUBSCRIBE', 'params': ['xrpusdt@kline_2h'], 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:16,976 | INFO | Sending message to WebSocket 0101c5c5-13d7-4c3e-80ae-173fea58f2f4: {'method': 'SUBSCRIBE', 'params': ['eigenusdt@kline_2h'], 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:17,248 | INFO | Received message: {'result': None, 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:17,249 | INFO | Received message: {'result': None, 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:17,250 | INFO | Received message: {'result': None, 'id': '0101c5c5-13d7-4c3e-80ae-173fea58f2f4'}
2026-03-16 13:54:17,823 | INFO | Establishing Websocket connection with id d3b0f6bd-b08a-4082-9b18-85297052ff5e to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:17,825 | INFO | Connecting to wss://stream.binance.com:9443/stream with proxy None
2026-03-16 13:54:17,851 | INFO | Establishing Websocket connection with id d3b0f6bd-b08a-4082-9b18-85297052ff5e to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:17,852 | INFO | Sending message to WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e: {'method': 'SUBSCRIBE', 'params': ['ethusdt@kline_2h'], 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:17,854 | INFO | Sending message to WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e: {'method': 'SUBSCRIBE', 'params': ['pendleusdt@kline_2h'], 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:17,855 | INFO | Sending message to WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e: {'method': 'SUBSCRIBE', 'params': ['bardusdt@kline_2h'], 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:17,856 | INFO | Sending message to WebSocket d3b0f6bd-b08a-4082-9b18-85297052ff5e: {'method': 'SUBSCRIBE', 'params': ['zkusdt@kline_2h'], 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:17,896 | INFO | Establishing Websocket connection with id 0101c5c5-13d7-4c3e-80ae-173fea58f2f4 to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:18,096 | INFO | Received message: {'result': None, 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:18,097 | INFO | Received message: {'result': None, 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:18,098 | INFO | Received message: {'result': None, 'id': 'd3b0f6bd-b08a-4082-9b18-85297052ff5e'}
2026-03-16 13:54:18,147 | INFO | [WS DEBUG] Open candle skipped ethusdt_2h
2026-03-16 13:54:18,149 | INFO | [WS DEBUG] Open candle skipped btcusdt_2h
2026-03-16 13:54:18,236 | INFO | [WS DEBUG] Open candle skipped bnbusdt_2h
2026-03-16 13:54:18,441 | INFO | [WS DEBUG] Open candle skipped xrpusdt_2h
2026-03-16 13:54:18,772 | INFO | Establishing Websocket connection with id d3b0f6bd-b08a-4082-9b18-85297052ff5e to: wss://stream.binance.com:9443/stream
2026-03-16 13:54:20,135 | INFO | [WS DEBUG] Open candle skipped ethusdt_2h
2026-03-16 13:54:20,138 | INFO | [WS DEBUG] Open candle skipped btcusdt_2h

Why this matters
For a small number of streams, it works correctly even with duplicates like my logs example. However, for a larger number of streams (e.g., more than 80 symbols, which is my intended use case), the client stops receiving data after a reconnect.

Environment

binance-common==3.5.0
binance-sdk-spot==7.0.0
Python 3.12.3
OS: Linux
POOL mode with pool_size = 2
Streams: multiple kline streams (ex: ethusdt@kline_2h, bnbusdt@kline_2h)

Root Cause
The root cause seems to be when WebSocket reconnection logic currently calls: await self.connect(configuration.stream_url, configuration, connection.id) in: websocket.py#L342

But connect() recreates the full pool each time when configuration.mode == WebsocketMode.POOL:

Instead, for reconnect, it should only recreate the individual connection that needs reconnecting (e.g., via init_connection() directly), not the whole pool.

Suggested Fix
Replace the connect() call inside reconnect() with init_connection():

- await self.connect(configuration.stream_url, configuration, connection.id)
+ await self.init_connection(configuration.stream_url, configuration, connection.id)

Appreciate any guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions