flask-uwsgi-websocket icon indicating copy to clipboard operation
flask-uwsgi-websocket copied to clipboard

Strange empty string messages (not PING/PONGS) coming from .receive()

Open fobdy opened this issue 9 years ago • 2 comments

i have the following configuration on mac:

Flask (0.10.1)
Flask-uWSGI-WebSocket (0.4.4)
gevent (1.0.2)
greenlet (0.4.9)
uWSGI (2.0.11.2)

my app code is:

from flask import Flask
from flask.ext.uwsgi_websocket import GeventWebSocket

app = Flask(__name__)
websocket = GeventWebSocket(app)

@websocket.route('/echo')
def echo(ws):
    while True:
        msg = ws.recv()
        if msg is None:
            return
        print type(msg)
        print 'MESSAGE RECEIVED!!! ', msg
        ws.send(msg)

The ws.recv() call periodically receives some empty string '' messages from nowhere. Client doesn't send them.

When i've plugged off the flask and flask-uwsgi-websocket the following code worked fine:

import uwsgi


def app(env, start_response):
    # complete the handshake
    uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'],
                              env.get('HTTP_ORIGIN', ''))
    while True:
        msg = uwsgi.websocket_recv()
        uwsgi.websocket_send(msg)

My uWSGI config is:

[uwsgi]
http-socket = 127.0.0.1:9090
wsgi-file = app.py
callable = app
gevent = 100
processes = 4

fobdy avatar Oct 14 '15 13:10 fobdy

+1

adamjaso avatar Feb 22 '17 02:02 adamjaso

What appears to be happening here is that either the WebSocketMiddleware or perhaps the underlying uwsgi web socket implementation periodically pushes an empty string onto the receive queue, rather than using the "timeout" function of the queue.

If I had to guess, I'd say that this is a rather kludgy attempt to replicate the "timeout" functionality without raising the "Empty" exception that the timeout would raise. So an empty string is functionally equivalent to an "Empty" exception.

I could be missing something though - this is just my conclusion based on observed behavior and a light perusing of the code (where I never did find the actual code pushing the empty string). Would be nice if it was documented though, along with the fact that a return value of None means "connection closed"...

ibrewster avatar May 03 '17 19:05 ibrewster