singingwolfboy / flask-sse

Server-Sent Events for Flask
https://flask-sse.readthedocs.org
MIT License
312 stars 46 forks source link

Too many open files due to generator not closing connection. #32

Open sciuridae0603 opened 3 years ago

sciuridae0603 commented 3 years ago

I find a problem when the client opens a stream and the server doesn't push anything, it will cause connection to stuck and won't close, then too many open files error happened.

So I edit some code to send some dummy messages (like ping) to prevent that happen.

And here's my workaround :

        def generator():
            pubsub = self.redis.pubsub()
            pubsub.subscribe(channel)
            try:
                while True:
                    message = pubsub.get_message()
                    if message == None:
                        yield "event:ping\ndata:{}\n\n"
                        time.sleep(0.4)
                    elif message['type'] == 'message':
                        msg_dict = json.loads(message['data'])
                        yield str(Message(**msg_dict))
            except GeneratorExit:
                pass
            except:
                traceback.print_exc()
            finally:
                pubsub.unsubscribe(channel)
faruqsandi commented 2 years ago

Hi @sciuridae0603! It's already months since this issue, is there any problem after you fix this with your workaround? I face the same error. image

sciuridae0603 commented 2 years ago

@faruqsandi I think my workaround won't cause any problem, except it will send a ping every 0.4 secs, which may cost more internet bandwidth.