Skip to content

Error SSL: BAD_LENGTH, setting enable_multithread would fix #451

@agavignet

Description

@agavignet

Hi,

We are often seeing the following SSL error with our application that uses socketio. It happens when stressing out (with parallel processes) the connection on the same server, unfortunately error can not be easily catched from the application.

Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/foo/.local/lib/python3.6/site-packages/engineio/client.py", line 665, in _write_loop
    self.ws.send(encoded_packet)
  File "/home/foo/.local/lib/python3.6/site-packages/websocket/_core.py", line 253, in send
    return self.send_frame(frame)
  File "/home/foo/.local/lib/python3.6/site-packages/websocket/_core.py", line 278, in send_frame
    l = self._send(data)
  File "/home/foo/.local/lib/python3.6/site-packages/websocket/_core.py", line 448, in _send
    return send(self.sock, data)
  File "/home/foo/.local/lib/python3.6/site-packages/websocket/_socket.py", line 157, in send
    return _send()
  File "/home/foo/.local/lib/python3.6/site-packages/websocket/_socket.py", line 139, in _send
    return sock.send(data)
  File "/usr/lib/python3.6/ssl.py", line 944, in send
    return self._sslobj.write(data)
  File "/usr/lib/python3.6/ssl.py", line 642, in write
    return self._sslobj.write(data)
ssl.SSLError: [SSL: BAD_LENGTH] bad length (_ssl.c:2162)

It appears websocket.create_connection has an option enable_multithread. This ensures multithreading is correctly handled. See https://github.com/websocket-client/websocket-client/blob/master/websocket/_core.py#L462

So problem is solved when adding enable_multithread=True here https://github.com/miguelgrinberg/python-engineio/blob/master/engineio/client.py#L377

Which is called by socket.io for example here https://github.com/miguelgrinberg/python-socketio/blob/master/socketio/client.py#L276

One way of handling this would be to expose in eio a ws_options so we could set it:

self.client = socketio.Client(...)
self.client.eio.ws_options = {"enable_multithread": True}

Then engineio could create the websocket connection this way:

ws = websocket.create_connection(
    websocket_url + self._get_url_timestamp(), header=headers,
    cookie=cookies, **self.ws_options)

What do you think? Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions