unbit / uwsgi

uWSGI application server container
http://projects.unbit.it/uwsgi
Other
3.45k stars 688 forks source link

Websocket server does not receive all sent messages #1241

Open michaelimfeld opened 8 years ago

michaelimfeld commented 8 years ago

Hi, I'm coding websocket webapps with python web.py and uwsgi (2.0.12) and I'm currently having the following issue: When I send two messages quickly after eachother from the javascript, my backend only receives the first message but not the second. Why do I only receive the first message?

ws.send("message1");
ws.send("message2");

In my backend I do a select on the websocket_fd:

websocket_fd = uwsgi.connection_fd()
inputs = [websocket_fd]
readable, _, _ = select.select(inputs, [], [], 1.0)

And if the socket becomes readable I read the data from the websocket client:

data = uwsgi.websocket_recv_nb()

When I refresh the page in my browser the backend receives the rest of the messages (in this case message2). So for me it looks like there's something like a buffer which gets flushed on a page refresh.

If I send the second message with a delay everything works as expected. I assume the browser does send the two messages as one and uwsgi can somehow not handle it.

I appreciate any help.

xrmx commented 8 years ago

Thanks for reporting. Could you please upload a reproducer somewhere?

michaelimfeld commented 8 years ago

I created a little demo project to reproduce this bug: https://github.com/michaelimfeld/uwsgi-ws-bug-demo

michaelimfeld commented 8 years ago

Any news on this?

timofurrer commented 8 years ago

Is there any progress on this issue?

unbit commented 8 years ago

Can you try looping this part until it returns None:

data = uwsgi.websocket_recv_nb()
timofurrer commented 8 years ago

Seems to work.

However, usually if you don't get data any more the connection on the other end was closed. In this case None means that no more data for the readable socket is available - which is another use case.

Any solution for that?

unbit commented 8 years ago

in such a case if you call select() again it will suddenly return and you will get another None.

Not the best solution (albeit easy to implement), unfortunately this is an edge triggered system.

Maybe raising an exception on closed connection would simplify things ? (so the user does not even need to manage the disconnection)

stredani commented 7 years ago

Hi,

we are having this issue too. Looping over websocket_recv_nb does not help. Sometimes it returns the message, sometimes empty strings followed by a message again.

Will there be any progress on this issue?

kylemacfarlane commented 6 years ago

I'm having this problem when multiple messages are sent in very quick succession but looping over websocket_rev_nb retrieves them all. However it seems like doing this also picks up some PONG responses so uWSGI never receives them internally and kicks the client off.

1533 could be the same problem.

kylemacfarlane commented 6 years ago

Forget what I said about pongs. Apparently I was relying on the delayed messages to act as keepalives and once they were fixed it revealed a client side bug where I wasn't responding to pings in the correct order.