Make sure that client response life cycle is shorter than client session
未关闭
client
enhancement
- 主要语言
- Python
- 星标
- 16.5k
- 派生
- 2.4k
- 平均合并
- 17 小时 22 分钟
- 30 天内合并 PR
- 212
描述
I observed a code like this on stack overflow:
```python
async def fetch(url, data):
async with ClientSession() as session:
response = await session.post(url, data=data)
return response
```
The function is really wrong (while it may work for short response body).
The problem is: a session is destroyed *before* response destruction.
If the response did not come as short data and IO is needed for `await response.read()` and family the code fails with a very cryptic exception.
`ClientResponse` should have a weak reference to `ClientSession`. When the session is gone weakref callback should raise an exception.
贡献指南
评估
这个 Issue 还没有评估数据。