From 20c93656d18b50c17c85b7a1e234276fb1444f27 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 8 May 2022 22:06:02 +0000 Subject: [PATCH] Fix asyncio.CancelledError again (#6719) (#6727) (#6741) * Fix asyncio.CancelledError again (#6719) `asyncio.CancelledError()` on peer disconnection have been removed by #4080, but #4415 re-introduced it silently. `self._waiter.cancel()` and `self._task_handler.cancel()` were added by #4415, but #4415 in fact only needed `self._waiter.cancel()` (proof below). So I propose to remove `self._task_handler.cancel()`, both #4080 and #4415 will be fixed. To test that I re-resolved #4080 I used: ```py async def handle(request): try: await asyncio.sleep(5) await request.text() except BaseException as e: print(f'base exception {type(e).__name__}') return web.Response(text='toto') ``` ```console curl -X POST -H "Content-Type: text/plain" --data "bouh" localhost:8080 ``` I kill the curl request before the 5 seconds of sleep. Before this PR I have the following error right after killing the curl: ```console $ python -Wall -X dev test.py ======== Running on http://0.0.0.0:8080 ======== (Press CTRL+C to quit) base exception CancelledError ``` After this commit I have the following error, but only after the 5 seconds sleep: ```console $ python -Wall -X dev test.py ======== Running on http://0.0.0.0:8080 ======== (Press CTRL+C to quit) base exception ConnectionResetError ``` To test that I didn't re-introduce #4415 I use a basic `handle` and 30 `curl localhost:8080`: - Before this commit no issue - If I remove `self._task_handler.cancel()` no issue - If I remove both `self._task_handler.cancel()` and `self._waiter.cancel()`: ```console $ python -Wall -X dev test.py ======== Running on http://0.0.0.0:8080 ======== (Press CTRL+C to quit) Task was destroyed but it is pending! task: (cherry picked from commit adeece3c1826b150f129e198f69e78a469901b5e) Co-authored-by: Hoel IRIS --- CHANGES/6719.bugfix | 1 + aiohttp/web_protocol.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) create mode 100644 CHANGES/6719.bugfix diff --git a/CHANGES/6719.bugfix b/CHANGES/6719.bugfix new file mode 100644 index 0000000000..b42ccca255 --- /dev/null +++ b/CHANGES/6719.bugfix @@ -0,0 +1 @@ +Fix regression where ``asyncio.CancelledError`` occurs on client disconnection. diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index f103b1672e..10a9608018 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -299,8 +299,6 @@ def connection_lost(self, exc: Optional[BaseException]) -> None: exc = ConnectionResetError("Connection lost") self._current_request._cancel(exc) - if self._task_handler is not None: - self._task_handler.cancel() if self._waiter is not None: self._waiter.cancel()