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 flag | Value / default | Applies to | Source |
|---|---|---|---|
aiohttp.ClientSession() | Enter with async with when the async function owns the whole lifetime | 3.14.5 | quickstart |
connector_owner | Default True; set False for each session instance sharing the same connector | 3.14.5 | quickstart, ownership |
ClientSession.close() | Await it in cleanup if no session context owns closure; calling it without awaiting is insufficient | 3.14.5 | reference |
ClientResponse.read() | Await body consumption when needed; reading the whole body releases the connection unless reading fails; exit the response context even on exceptions | 3.14.5 | reference |
BaseConnector.close() | Await after all sessions using an independently owned connector have finished | 3.14.5 | reference |
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) | Cause | Fix | Source |
|---|---|---|---|
Unclosed client session | A user may not call close due to exceptions or improper usage | Own it with an async context or await closure in finally; Lyndon Bauto's report records missed cleanup in a client wrapper | December 8, 2021 report, current quickstart |
ResourceWarning: unclosed transport | Event loop stops before underlying transport cleanup finishes, even after session closure | Close responses, sessions and shared connector first; allow the documented shutdown delay before loop termination | current 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