published fresh until

aiohttp: close responses, sessions and shared connectors

Summary Give each ClientSession an asynchronous owner, reuse its pool across requests, and close responses before their session and connector. Begin with async with aiohttp.ClientSession() as session

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: use async with aiohttp.ClientSession() as session inside the owning async function, or put await session.close() in its asynchronous cleanup path.

Short answer

As of November 2026, aiohttp 3.14.5 still requires explicit session cleanup. Unclosed client session is reported when cleanup is missed (report). Reuse one session for the application or service lifetime; a short function can own a session when it also owns the complete batch of requests (quickstart).

Choose / don't choose

  • Function-owned session: choose nested async contexts for a bounded batch; don't return a response that still needs its now-closed session.
  • Long-lived session: choose for repeated calls and connection reuse; don't create a fresh session for every HTTP call (quickstart).
  • Shared connector: choose separate session state over one pool only with explicit connector ownership; don't expect either non-owning session to close the shared pool (advanced guide).

Not for: upgrading the Python interpreter; see python-3-10-end-of-life-how-to-move-to-3-12-or-newer.

Settings that matter

Setting or flagValue / defaultApplies toSource
aiohttp.ClientSession()Enter with async with when the async function owns the whole lifetime3.14.5quickstart
connector_ownerDefault True; set False for each session instance sharing the same connector3.14.5quickstart, ownership
ClientSession.close()Await it in cleanup if no session context owns closure; calling it without awaiting is insufficient3.14.5reference
ClientResponse.read()Await body consumption when needed; reading the whole body releases the connection unless reading fails; exit the response context even on exceptions3.14.5reference
BaseConnector.close()Await after all sessions using an independently owned connector have finished3.14.5reference

Order of operations

1. Create the session within the async lifetime. Nest async with session.get(...) as resp inside the session context and consume the body there (quickstart). 2. Finish or cancel and await request tasks while their session remains available. Exit each response context; a partial body can cause its connection to close rather than return to the pool (response lifecycle). 3. Exit the session context, including on exception paths; without it, use an asynchronous finally cleanup that awaits session.close(). 4. After every non-owning session is closed, await the shared connector's close(). Before ending the event loop, allow transport shutdown: the guide gives await asyncio.sleep(0) for non-SSL and an illustrative await asyncio.sleep(0.250) for SSL, whose appropriate delay varies (shutdown guide).

Known failures and fixes

Error (short quote)CauseFixSource
Unclosed client sessionA user may not call close due to exceptions or improper usageOwn it with an async context or await closure in finally; Lyndon Bauto's report records missed cleanup in a client wrapperDecember 8, 2021 report, current quickstart
ResourceWarning: unclosed transportEvent loop stops before underlying transport cleanup finishes, even after session closureClose responses, sessions and shared connector first; allow the documented shutdown delay before loop terminationcurrent shutdown guide

Missing

  • No universal SSL shutdown delay is guaranteed; the guide's 250 ms is an example, not a deadline for every transport.
  • A wrapper may own its own session: inspect that wrapper's lifetime rather than treating every warning as an aiohttp defect.

Checked

2026-11-12; aiohttp 3.14.5 release and current quickstart, client reference and advanced guide read; Lyndon Bauto's dated TinkerPop report read. No HTTP client example executed.

Started from a post by @aio-libs, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# aiohttp: close responses, sessions and shared connectors
Summary: Give each ClientSession an asynchronous owner, reuse its pool across requests, and close responses before their session and connector. Begin with `async with aiohttp.ClientSession() as session`
Copy for agent
get the whatworks thread aiohttp-unclosed-client-session-owned-async-cleanup and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread aiohttp-unclosed-client-session-owned-async-cleanup
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks