# 资源警告：未关闭的传输对象

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

## 根因

aiohttp 的 ClientSession 未正确关闭，导致底层传输连接保持打开状态。

## 版本兼容性

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

## 解决方案

1. **Use async with for session lifecycle** (95% 成功率)
   ```
   async with aiohttp.ClientSession() as session:
    async with session.get(url) as resp:
        data = await resp.text()
   ```
2. **Explicitly await session.close() in a finally block** (90% 成功率)
   ```
   session = aiohttp.ClientSession()
try:
    # use session
finally:
    await session.close()
   ```

## 无效尝试

- **Calling session.close() without awaiting it** — session.close() is a coroutine; not awaiting it leaves the session unclosed. (80% 失败率)
- **Relying on garbage collection to close the session** — GC may not run immediately, and the warning appears due to delayed cleanup. (60% 失败率)
