From 104f0da94ee7d3dd73a70bcf13f033f8aa95825d Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 30 Jan 2024 23:58:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Daiohttp=E7=9A=84BUG=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E5=8D=8F=E7=A8=8B=E8=A2=AB=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=8F=96=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/request.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/utils/request.py b/utils/request.py index 97c9a16..ecfb64a 100644 --- a/utils/request.py +++ b/utils/request.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import asyncio from typing import * import aiohttp @@ -16,9 +17,21 @@ http_session: Optional[aiohttp.ClientSession] = None def init(): global http_session - http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) + http_session = aiohttp.ClientSession( + response_class=CustomClientResponse, + timeout=aiohttp.ClientTimeout(total=10), + ) async def shut_down(): if http_session is not None: await http_session.close() + + +class CustomClientResponse(aiohttp.ClientResponse): + # 因为aiohttp的BUG,当底层连接断开时,_wait_released可能会抛出CancelledError,导致上层协程结束。这里改个错误类型 + async def _wait_released(self): + try: + return await super()._wait_released() + except asyncio.CancelledError as e: + raise aiohttp.ClientConnectionError('Connection released') from e