# RuntimeError: 会话已关闭

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

## 根因

在退出 async with 块（会调用 close()）之后，或在 await session.close() 之后，仍使用了 aiohttp.ClientSession。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.9+ | active | — | — |

## 解决方案

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

## 无效尝试

- **** — The context manager closes the session on exit; reusing it always raises. (90% 失败率)
- **** — Closed sessions cannot issue new requests; the connector is shut down. (95% 失败率)
