python
runtime_error
ai_generated
true
RuntimeError: 会话已关闭
RuntimeError: Session is closed
ID: python/aiohttp-session-closed
80%修复率
86%置信度
0证据数
2024-06-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
aiohttp.ClientSession 在关闭后被使用,通常是因为其所在的 async with 块已退出,或在 aclose() 之后会话属性被跨请求复用。
English
An aiohttp.ClientSession was used after being closed, typically because it was created in an async with block that exited, or the session attribute was reused across requests after aclose().
解决方案
-
90% 成功率
session = aiohttp.ClientSession() try: await session.get(url) finally: await session.close() -
92% 成功率
async with aiohttp.ClientSession() as session: async with session.get(url) as resp: data = await resp.text()
无效尝试
常见但无效的做法:
-
70% 失败
The old session object is still closed; reassigning may leave background connectors attached to the closed session.
-
90% 失败
The session is closed, not slow; the request cannot even be queued.