# RuntimeError: 会话已关闭

- **ID:** `python/aiohttp-session-closed`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

aiohttp.ClientSession 在关闭后被使用，通常是因为其所在的 async with 块已退出，或在 aclose() 之后会话属性被跨请求复用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   session = aiohttp.ClientSession()
try:
    await session.get(url)
finally:
    await session.close()
   ```
2. **** (92% 成功率)
   ```
   async with aiohttp.ClientSession() as session:
    async with session.get(url) as resp:
        data = await resp.text()
   ```

## 无效尝试

- **** — The old session object is still closed; reassigning may leave background connectors attached to the closed session. (70% 失败率)
- **** — The session is closed, not slow; the request cannot even be queued. (90% 失败率)
