dpallot / simple-websocket-server

A python based websocket server that is simple and easy to use.
951 stars 321 forks source link

ssl.SSLWantReadError: The operation did not complete (read) #87

Closed altherat closed 2 years ago

altherat commented 6 years ago

I'm using OkHttpClient on Android to connect to my websocket server. When my app establishes a connection, I send a message. Shorter messages don't have an issue, but for longer messages I'm getting an exception. The message I'm testing with is 14819 characters long in JSON format, but I've tested some smaller messages and the exception starts appearing when it's around 4315 characters long. The culprit is this line.

Here's the stack trace:

  File "/usr/local/lib/python3.5/dist-packages/SimpleWebSocketServer/SimpleWebSocketServer.py", line 274, in _handleData
    data = self.client.recv(16384)
  File "/usr/lib/python3.5/ssl.py", line 914, in recv
    return self.read(buflen)
  File "/usr/lib/python3.5/ssl.py", line 791, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.5/ssl.py", line 577, in read
    v = self._sslobj.read(len)
ssl.SSLWantReadError: The operation did not complete (read) (_ssl.c:1977)

I don't know the protocol well so it's hard for me to tell whether it's the fault of this library or OkHttpClient or maybe I'm just doing something wrong. The only thing I've tried is increasing the 16384 to a higher number but it didn't help.

uzlonewolf commented 6 years ago

This looks like https://bugs.python.org/issue12343 . Try replacing the data = self.client.recv(16384) with

         try:
            data = self.client.recv(16384)
         except ssl.SSLWantReadError:
            return
dpallot commented 5 years ago

This looks like https://bugs.python.org/issue12343 . Try replacing the data = self.client.recv(16384) with

         try:
            data = self.client.recv(16384)
         except ssl.SSLWantReadError:
            return

If its working would you like to put in a pull request?

uzlonewolf commented 5 years ago

Unfortunately I have not been able to test any of this as I have not run into this problem myself. There also seems to be very little documentation about this issue, and one of the few comments I've seen says select() is not going to fire when the data does arrive, so I'm hesitant to commit code I can't verify works. I have some ideas on a test setup to try and reproduce this but have not found the time to set it up.

eaganr commented 4 years ago

This project has been really helpful. Ran into a similar problem as in this thread. The SSL websocket was closing when a large amount of data was sent. I found a solution. I got rid of the else statement at line 341. Then the while loop sending data retries until it is successful rather than closing the socket.

uzlonewolf commented 4 years ago

I don't like blindly ignoring all errors as on an unclean close the socket is going to hang and stick around forever and block all other clients while it does it. What is the exact exception thrown? Adding it to the list of try-again exceptions would be better.