Skip to content

SSL thread-safety issue causes 30-55% connection failures with sync client #1568

@MichaelRipa

Description

@MichaelRipa

Describe the bug

The sync client (socketio.Client) experiences 30-55% connection failure rate over TLS/SSL when reconnection=False. The root cause appears to be concurrent send/recv operations on SSL sockets during the Socket.IO handshake (not thread-safe in Python).

The async client (socketio.AsyncClient) has 0% failure rate because it uses asyncio's single-threaded event loop. Similarly, none of this happens over HTTP.

I have a working fix, and will submit a PR with it.

To Reproduce

  • python-socketio: 5.16.1
  • python-engineio: 4.13.0
  • websocket-client: 1.9.0
  • Python: 3.12
  • OS: Linux
import socketio
import time

URL = 'https://your-server.com' # Can provide a temporary endpoint if needed
PATH = '/socket.io'

success = fail = 0
for i in range(20):
    sio = socketio.Client(reconnection=False)
    try:
        sio.connect(URL, socketio_path=PATH, wait_timeout=10, transports=['websocket'])
        success += 1
        sio.disconnect()
    except Exception as e:
        print(f'FAIL: {e}')
        fail += 1
    time.sleep(0.1)

print(f'{fail} failures out of 20 ({fail/20*100}%)')

Expected behavior
sio.connect() should reliably establish a connection to the server when the server is reachable and functioning correctly. With reconnection=False, failures should only occur due to actual network or server issues, not internal client race conditions.

Logs

Client logs with logger=True, engineio_logger=True:

Successful connection:

2026-04-21 10:31:12,887 engineio.client INFO: Attempting WebSocket connection to wss://server/socket.io/?transport=websocket&EIO=4
2026-04-21 10:31:12,996 engineio.client INFO: WebSocket connection accepted with {'sid': 'nmw9ds1J7EPXvyQ2AAWS', ...}
2026-04-21 10:31:12,996 socketio.client INFO: Engine.IO connection established
2026-04-21 10:31:12,996 engineio.client INFO: Sending packet MESSAGE data 0{}
2026-04-21 10:31:13,012 engineio.client INFO: Received packet MESSAGE data 0{"sid":"VnbrCOaTwmteZdaWAAWT"}
2026-04-21 10:31:13,012 socketio.client INFO: Namespace / is connected

Failed connection (same server, moments later):

2026-04-21 10:31:13,862 engineio.client INFO: Attempting WebSocket connection to wss://server/socket.io/?transport=websocket&EIO=4
2026-04-21 10:31:13,976 engineio.client INFO: WebSocket connection accepted with {'sid': 'hX5STzTMxKLxzwt8AAWa', ...}
2026-04-21 10:31:13,976 socketio.client INFO: Engine.IO connection established
2026-04-21 10:31:13,977 engineio.client INFO: Sending packet MESSAGE data 0{}
2026-04-21 10:31:13,977 engineio.client WARNING: WebSocket connection was closed, aborting
2026-04-21 10:31:13,979 engineio.client INFO: Waiting for write loop task to end
2026-04-21 10:31:13,979 engineio.client INFO: Exiting write loop task
2026-04-21 10:31:13,980 socketio.client INFO: Engine.IO connection dropped

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions