aio-libs / aio-libs/aiohttp

WebSocketResponse requires closing/closed checks before sending something

Đang mở
#3,391 3 bình luận 1 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
17 giờ 22 phút
Pull request đã merge (30 ngày)
212

Mô tả

## Long story short

In the server-side websocket handlers written with aiohttp, `WebSocketResponse`'s `send_xxx()` methods "ignores" closing/closed state of the connection.

So in my code, I had to add `if ws.closed: break` everywhere that calls `send_xxx()`.

Maybe this issue is related to #2025.

## Expected behaviour

The `send_xxx()` methods should raise an explicit exception so that a server-side task can notice if the connection is closed.

## Actual behaviour

The `send_xxx()` methods just emit an warning to the logger and continue.
This behavior leads to a memory leak as the unsent messages accumulates in the buffer.
I get a stream of log messages like:
```
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
websocket connection is closing.
socket.send() raised exception.
...
```
until my server-side task finishes (which runs without noticing the connection is closed!).

## Steps to reproduce

1. Write a simple websocket handler like:

```python
async def handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
for _ in range(10):
await asyncio.sleep(1)
await ws.send_str('test')
return ws
```

2. Open this handler in a web browser using Javascript that reads only one message and closes the connection actively.
(NOTE: closing the page abruptly in the browser makes aiohttp to raise `asyncio.CancelledError`)

## Your environment

Linux & macOS.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.