Skip to content

Commit

Permalink
fix(web.req): regression: IPV6_PRIOR|R_PROXY causes AssertionError
Browse files Browse the repository at this point in the history
Happy eyeballs, available since aiohttp 3.10, cause AssertionError when
setting both proxy and socket family.

Make them mutually exclusive (proxy takes precedence) since it is always
meaningless to set them together.

TODO: Is it time to deprecate IPV6_PRIOR and completely rely on Happy
Eyeballs (RFC 8305)?

Fixes the regression introduced in
88fdb2c.

Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Aug 14, 2024
1 parent 0015f29 commit 354c6ea
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/web/req.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,22 @@ async def _request(
host = urlparse(url).hostname
semaphore_to_use = locks.hostname_semaphore(host, parse=False) if semaphore in (None, True) \
else (semaphore or nullcontext())
v6_rr_set = None
try:
v6_rr_set = (await asyncio.wait_for(resolve(host, 'AAAA', lifetime=1), 1.1)).rrset if env.IPV6_PRIOR else None
except DNSException:
pass
except asyncio.TimeoutError:
pass
except Exception as e:
logger.debug(f'Error occurred when querying {url} AAAA:', exc_info=e)
socket_family = AF_INET6 if v6_rr_set else 0

# Happy Eyeballs, available since aiohttp 3.10, cause AssertionError when setting both proxy and socket family.
# Make them mutually exclusive (proxy takes precedence) since it is always meaningless to set them together.
# TODO: Is it time to deprecate IPV6_PRIOR and completely rely on Happy Eyeballs (RFC 8305)?
use_proxy: bool = PROXY and proxy_filter(host, parse=False)
socket_family = 0
if env.IPV6_PRIOR and not use_proxy:
try:
if (
await asyncio.wait_for(resolve(host, 'AAAA', lifetime=1), 1.1)
).rrset:
socket_family = AF_INET6
except (DNSException, asyncio.TimeoutError):
pass
except Exception as e:
logger.debug(f'Error occurred when querying {url} AAAA:', exc_info=e)

_headers = HEADER_TEMPLATE.copy()
if headers:
Expand Down Expand Up @@ -215,7 +221,7 @@ async def _fetch():
socket_family = AF_INET
proxy_connector = (
ProxyConnector.from_url(PROXY, family=socket_family, ssl=__SSL_CONTEXT)
if (PROXY and proxy_filter(host, parse=False))
if use_proxy
else aiohttp.TCPConnector(family=socket_family, ssl=__SSL_CONTEXT)
)

Expand Down

0 comments on commit 354c6ea

Please sign in to comment.