Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #10569/dfbf782b backport][3.11] Break cyclic references when there is an exception handling a request #10571

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/10569.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Break cyclic references when there is an exception handling a request -- by :user:`bdraco`.
14 changes: 11 additions & 3 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ async def start(self) -> None:
keep_alive(True) specified.
"""
loop = self._loop
handler = asyncio.current_task(loop)
assert handler is not None
manager = self._manager
assert manager is not None
keepalive_timeout = self._keepalive_timeout
Expand Down Expand Up @@ -551,7 +549,16 @@ async def start(self) -> None:
else:
request_handler = self._request_handler

request = self._request_factory(message, payload, self, writer, handler)
# Important don't hold a reference to the current task
# as on traceback it will prevent the task from being
# collected and will cause a memory leak.
request = self._request_factory(
message,
payload,
self,
writer,
self._task_handler or asyncio.current_task(loop), # type: ignore[arg-type]
)
try:
# a new task is used for copy context vars (#3406)
coro = self._handle_request(request, start, request_handler)
Expand Down Expand Up @@ -617,6 +624,7 @@ async def start(self) -> None:
self.force_close()
raise
finally:
request._task = None # type: ignore[assignment] # Break reference cycle in case of exception
if self.transport is None and resp is not None:
self.log_debug("Ignored premature client disconnection.")

Expand Down
41 changes: 41 additions & 0 deletions tests/isolated/check_for_request_leak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio
import gc
import sys
from typing import NoReturn

from aiohttp import ClientSession, web
from aiohttp.test_utils import get_unused_port_socket

gc.set_debug(gc.DEBUG_LEAK)


async def main() -> None:
app = web.Application()

async def handler(request: web.Request) -> NoReturn:
await request.json()
assert False

app.router.add_route("GET", "/json", handler)
sock = get_unused_port_socket("127.0.0.1")
port = sock.getsockname()[1]

runner = web.AppRunner(app)
await runner.setup()
site = web.SockSite(runner, sock)
await site.start()

async with ClientSession() as session:
async with session.get(f"http://127.0.0.1:{port}/json") as resp:
await resp.read()

# Give time for the cancelled task to be collected
await asyncio.sleep(0.5)
gc.collect()
request_present = any(type(obj).__name__ == "Request" for obj in gc.garbage)
await session.close()
await runner.cleanup()
sys.exit(1 if request_present else 0)


asyncio.run(main())
17 changes: 17 additions & 0 deletions tests/test_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ def test_client_response_does_not_leak_on_server_disconnected_error() -> None:
stdout=subprocess.PIPE,
) as proc:
assert proc.wait() == 0, "ClientResponse leaked"


@pytest.mark.skipif(IS_PYPY, reason="gc.DEBUG_LEAK not available on PyPy")
def test_request_does_not_leak_when_request_handler_raises() -> None:
"""Test that the Request object is collected when the handler raises.

https://github.com/aio-libs/aiohttp/issues/10548
"""
leak_test_script = pathlib.Path(__file__).parent.joinpath(
"isolated", "check_for_request_leak.py"
)

with subprocess.Popen(
[sys.executable, "-u", str(leak_test_script)],
stdout=subprocess.PIPE,
) as proc:
assert proc.wait() == 0, "Request leaked"
Loading