miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
272 stars 24 forks source link

Error on shutdown when using threads #60

Closed tschesnok closed 1 year ago

tschesnok commented 1 year ago

I require the use of a 2nd thread inside the connected function. When the client closes the connection the thread exits in an ungraceful way:

xception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/home/ubuntu/TestServer/wsdemo.py", line 22, in feedThread ws.send(text2) File "/usr/local/lib/python3.8/dist-packages/simple_websocket/ws.py", line 90, in send raise ConnectionClosed(self.close_reason, self.close_message) simple_websocket.ws.ConnectionClosed: Connection closed: 1005

Is there a way to get a notice when the client is closing the connection so that we can do some housekeeping and shut things down on the server side?

miguelgrinberg commented 1 year ago

Yes. The proper way would be to add a try/except block to catch the connection closed error and deal with it gracefully in your thread (for the main websocket route, Flask-Sock does it for you). Have you tried that?

tschesnok commented 1 year ago

I'll look into it. Also I see from the examples that I can just create a custom shutdown message and take a more active approach from the client side as well..

tschesnok commented 1 year ago

That works! Thank you.