File: check_for_request_leak.py

package info (click to toggle)
python-aiohttp 3.11.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,156 kB
  • sloc: python: 51,898; ansic: 20,843; makefile: 395; javascript: 31; sh: 3
file content (41 lines) | stat: -rw-r--r-- 1,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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())