python runtime_error ai_generated true

RuntimeError: 会话已关闭

RuntimeError: Session is closed

ID: python/aiohttp-session-client-closed

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-05-02首次发现

版本兼容性

版本状态引入弃用备注
3.8+ active
3.9+ active
3.10+ active
3.11+ active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

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

  2. 85% 失败

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