aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

RequestHandler.start() calls _request_factory after connection_lost() cleared it (TypeError: 'NoneType' object is not callable)

Open miztizm opened this issue 1 month ago • 1 comments

Describe the bug

RequestHandler.start() can call self._request_factory after connection_lost() has set it to None. The connection task then dies with an unretrieved exception that the event loop logs:

Task exception was never retrieved
future: <Task finished name='Task-9366' coro=<RequestHandler.start() done, defined at .../aiohttp/web_protocol.py:550> exception=TypeError("'NoneType' object is not callable")>
Traceback (most recent call last):
  File ".../aiohttp/web_protocol.py", line 594, in start
    request = self._request_factory(
TypeError: 'NoneType' object is not callable

This happens when a request is queued (data_received appends to _messages and resolves _waiter) and connection_lost() runs in the same event-loop iteration, before start() resumes. start() then wakes from await self._waiter, sees a non-empty _messages, pops the message, and calls the cleared factory. The while not self._force_close check only runs at the top of the loop, so the force_close() inside connection_lost() isn't seen in time.

It's harmless in practice, because only the already-dead connection is affected. It does put a traceback into server logs, though, and a comment in 3.14.x says the post-connection_lost state is "not reachably exercisable" inside this loop. This reproduction shows that it is.

To Reproduce

import asyncio
from unittest import mock

import aiohttp
from aiohttp import web


async def main() -> None:
    async def handler(request: web.Request) -> web.Response:
        return web.Response(text="ok")

    proto = web.Server(handler)()
    transport = mock.Mock()
    transport.is_closing.return_value = False
    transport.get_extra_info.return_value = None
    proto.connection_made(transport)
    task = proto._task_handler
    await asyncio.sleep(0)  # start() is parked waiting for the next request

    # Same event-loop iteration: a request arrives, then the peer disconnects.
    proto.data_received(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n")
    proto.connection_lost(None)

    for _ in range(5):
        await asyncio.sleep(0)
    print("aiohttp", aiohttp.__version__, "->", repr(task.exception()))


asyncio.run(main())

Expected behavior

start() exits quietly once the connection is lost. That could be done by re-checking self._force_close (or self._request_factory is None) after the waiter resolves, before handling a popped message.

Logs/tracebacks

aiohttp 3.13.5 -> TypeError("'NoneType' object is not callable")
aiohttp 3.14.4 -> TypeError("'NoneType' object is not callable")

Python Version

Python 3.13.13

aiohttp Version

3.13.5 and 3.14.4 (latest), observed in production on 3.13.5

OS

Windows 11 (ProactorEventLoop) in production. The reproduction above is event-loop agnostic.

Related component

Server

Code of Conduct

  • [X] I agree to follow the aio-libs Code of Conduct

miztizm avatar Oct 08 '26 13:10 miztizm

I'd like to work on this — re-checking after the waiter resolves, before handling the popped message.

rinopatrick avatar Nov 02 '26 06:11 rinopatrick