Skip to content

Commit

Permalink
fix(web): hotfix unclosed uvloop SSL transport
Browse files Browse the repository at this point in the history
  • Loading branch information
Rongronggg9 committed May 25, 2022
1 parent a45dac2 commit b6dd68b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import functools

from aiohttp import ClientResponse
from cachetools.keys import hashkey

_version_info = sys.version_info
Expand Down Expand Up @@ -34,9 +35,19 @@ def __exit__(self, *excinfo):
async def __aenter__(self):
return self.enter_result

async def __aexit__(self, *excinfo):
async def __aexit__(self, exc_type, exc_value, traceback):
pass


class AiohttpUvloopTransportHotfix(AbstractAsyncContextManager):
def __init__(self, response: ClientResponse):
self.transport = response.connection and response.connection.transport

async def __aexit__(self, exc_type, exc_value, traceback):
if self.transport:
self.transport.abort()


# default cipher list in Python 3.9
_ciphers_py39 = (
'TLS_AES_256_GCM_SHA384:'
Expand Down
21 changes: 11 additions & 10 deletions src/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Union, Optional, AnyStr
from typing_extensions import Final
from collections.abc import Callable
from .compat import nullcontext, ssl_create_default_context
from .compat import nullcontext, ssl_create_default_context, AiohttpUvloopTransportHotfix

import re
import asyncio
Expand Down Expand Up @@ -225,15 +225,16 @@ async def _fetch():
async with aiohttp.ClientSession(connector=proxy_connector, timeout=aiohttp.ClientTimeout(total=timeout),
headers=_headers) as session:
async with session.get(url, read_bufsize=read_bufsize, read_until_eof=read_until_eof) as response:
status = response.status
content = None
if status == 200:
content = await resp_callback(response)
return WebResponse(url=url,
content=content,
headers=response.headers,
status=status,
reason=response.reason)
async with AiohttpUvloopTransportHotfix(response):
status = response.status
content = None
if status == 200:
content = await resp_callback(response)
return WebResponse(url=url,
content=content,
headers=response.headers,
status=status,
reason=response.reason)

tries = 0
retry_in_v4_flag = False
Expand Down

0 comments on commit b6dd68b

Please sign in to comment.