SSL-related bug with websocket send call and multithreading
hello everyone,
I bumped into a problem with the send method of ws4py.websocket.WebSocketwhen SSL is enabled.
I have a CherryPy server to which I associate some WebSocket servers. Messages are sent from a few independent threads through the same socket. This works fine without SSL.
However, with SSL enabled and more than one thread per socket, some messages end up being packed together and this makes the socket terminate.
If I open separate sockets for each thread, this works fine though.
I suspect there's something similar in essence as with this bug, i.e. difference on how SSL and non-SSL sockets treat pending data (correct me if I didn't get it right).
Any clues on why this may happen and how to fix it?
EDIT: I should add that I'm on OSX, didn't try on another system yet. EDIT2: forgot also - CherryPy 3.8.0, ws4py 0.3.4, python 2.7.10 or 3.4.3
Yeah, I'm on linux and didn't have much trouble testing this. It shows up only on SSL for me and goes away if I wrap part of send in a lock. I have a branch on my fork here:
https://github.com/EternityForest/WebSocket-for-Python/commit/f90b260710554d621f343300c7ac0cf7237be2bb
But I don't know quite enough about ws4py to be sure there's not other problems here.
Ok yes, this makes sense. I was trying to fiddle with the SSL_WANT_WRITE flag but didn't quite figure out how to integrate it cleanly at that point. Thanks for the patch. Works here too.
Not sure if I was experiencing the same problem, but locking didn't help in my case. For me the problem seemed to be that after the SSL_WANT_WRITE error, no retry was done. This seems to be a bug in sendall used in ws4py.websocket.WebSocket._write. See also https://github.com/pyca/pyopenssl/issues/176.
I just replaced sendall with send like in this example. This seems to work for me.
The solution above still has a problem when sending large messages (>16KB). Replacing the _write method in websocket with the following seems to work.
def _write(self, b):
if self.terminated or self.sock is None:
raise RuntimeError("Cannot send on a terminated websocket")
left_to_send = len(b)
total_sent = 0
while True:
try:
total_sent += self.sock.send(b)
break
except WantWriteError:
pass
left_to_send -= total_sent
if left_to_send:
self._write(b[total_sent:])