From 3b499ef184ce6da0cd5b35572cbb74e37aab0103 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Mon, 23 May 2022 23:06:38 +1000 Subject: [PATCH] Properly catched websocket CancelledError in websocket handler in Python 3.7. In python 3.8 `CancelledError` is subclass of `BaseException`, but in Python 3.7 and earlier, it is subclass of `Exception`. That means in python 3.7, the `CancelledError` gets caught by the general `Exception` handler, and does not correctly handle the case of a cancelled websocket task. Fixes #2462 --- sanic/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index b79d16e4eb..70d7b0b51e 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -992,10 +992,10 @@ async def _websocket_handler( cancelled = False try: await fut - except Exception as e: - self.error_handler.log(request, e) except (CancelledError, ConnectionClosed): cancelled = True + except Exception as e: + self.error_handler.log(request, e) finally: self.websocket_tasks.remove(fut) if cancelled: