Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL-related bug with websocket send call and multithreading #174

Open
vpaeder opened this issue Sep 4, 2015 · 4 comments
Open

SSL-related bug with websocket send call and multithreading #174

vpaeder opened this issue Sep 4, 2015 · 4 comments

Comments

@vpaeder
Copy link

vpaeder commented Sep 4, 2015

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

@EternityForest
Copy link
Contributor

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:

EternityForest@f90b260

But I don't know quite enough about ws4py to be sure there's not other problems here.

@vpaeder
Copy link
Author

vpaeder commented Sep 7, 2015

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.

@dalai4git
Copy link

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 pyca/pyopenssl#176.

I just replaced sendall with send like in this example. This seems to work for me.

@dalai4git
Copy link

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:])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants