BaseConnector: keepalive_timeout=None crashes on connection reuse (TypeError comparing float and NoneType)
BaseConnector.__init__ type-hints keepalive_timeout as _SENTINEL | None | float, and only rejects None when force_close=True is also set. With the default force_close=False, TCPConnector(keepalive_timeout=None) is accepted with no error, but crashes as soon as a second request tries to reuse a pooled connection to the same host:
File ".../aiohttp/connector.py", line 739, in _get
if proto.is_connected() and t1 - t0 <= self._keepalive_timeout:
TypeError: '<=' not supported between instances of 'float' and 'NoneType'Repro (tested on 3.14.3 and current master):
import asyncio
from aiohttp import web, ClientSession, TCPConnector
async def handler(request):
return web.Response(text="ok")
async def main():
app = web.Application()
app.router.add_get("/", handler)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "127.0.0.1", 8765)
await site.start()
async with ClientSession(connector=TCPConnector(keepalive_timeout=None)) as session:
async with session.get("http://127.0.0.1:8765/") as resp:
print(resp.status) # 200
async with session.get("http://127.0.0.1:8765/") as resp: # crashes here
print(resp.status)
asyncio.run(main())Found #786 from 2016, which looks like the same bug and got closed as fixed by #907. But #907 only handles the opposite case, it raises when force_close=True and keepalive_timeout is also set explicitly. It never touches force_close=False with keepalive_timeout=None, which is what was actually reported back then, and there's still no test for it. The cast(float, keepalive_timeout) in the current constructor casts away this exact None case for the type checker instead of handling it, probably why it's gone unnoticed this long.
Both _get()'s reuse check and _cleanup()'s deadline math need a None guard. Cleanup only happens to be safe right now because weakref_handle() silently skips scheduling when the delay is None, not something worth relying on.
Have a fix and regression tests ready, opening a PR shortly.
Source: aio-libs/aiohttp