python runtime_error ai_generated true

RuntimeError: Session is closed

ID: python/aiohttp-session-client-closed

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-05-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8+ active
3.9+ active
3.10+ active
3.11+ active

Root Cause

An aiohttp.ClientSession was closed (via async with or session.close()) but a coroutine still attempted to use it to make a request.

generic

中文

aiohttp.ClientSession 已被关闭(通过 async with 或 session.close()),但仍有协程尝试用它发起请求。

Workarounds

  1. 95% success
    session = aiohttp.ClientSession()
    try:
        await fetch_all(session)
    finally:
        await session.close()
  2. 93% success
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            data = await resp.text()

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Reassigning does not resurrect the closed object; any references held elsewhere still point to the closed session.

  2. 85% fail

    The connector is also closed with the session; manual connect raises the same error.