aio-libs / aio-libs/aiohttp

websocket connection is closing.

Đang mở
#1,768 16 bình luận 4 reaction 0 người được giao Xem trên GitHub
enhancement
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ả

## websocket connection is closing. but no Exception raise

I can't know whether a connection is available?

```bash
websocket connection is closing.
socket.send() raised exception.
```

```python
async def worker(url, msg_queue, loop, on_message=None):
async with aiohttp.ClientSession(loop=loop) as session,\
session.ws_connect(url) as ws:
close_flag = False
async def bg_sending():
nonlocal close_flag
try:
while True:
if close_flag is True:
break
msg = await msg_queue.get()
if isinstance(msg, dict):
await ws.send_json(msg)
elif msg == "close":
print("WS: GOT SELF CLOSE")
await ws.close()
close_flag = True
break
except Exception as e:
close_flag = True
raise e
async def bg_receive():
nonlocal close_flag
try:
async for msg in ws:
if close_flag is True:
break
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
close_flag = True
print("WS: ACCEPT CLOSE")
_thread.interrupt_main() #kill is not work here
break
else:
if on_message is not None:
res = on_message(msg.data)
if res is not None:
ws.send_str(res)
elif msg.type == aiohttp.WSMsgType.CLOSED:
print("WS: CONNECTION IS CLOSED")
close_flag = True
break
elif msg.type == aiohttp.WSMsgType.ERROR:
print(f"WS: ERROR:{msg}")
close_flag = True
raise Exception(str(msg))
except Exception as e:
close_flag = True
raise e
bg_send_task = asyncio.ensure_future(bg_sending(), loop=loop)
bg_receive_task = asyncio.ensure_future(bg_receive(), loop=loop)
while close_flag is False:
await asyncio.sleep(1)
if not bg_send_task.done():
bg_send_task.cancel()
if not bg_receive_task.done():
bg_receive_task.cancel()
if not bg_receive_task.cancelled() and bg_receive_task.exception() is not None:
print("WS: RECEIVE EXCEPTION")
raise bg_receive_task.exception()
if not bg_send_task.cancelled() and bg_send_task.exception() is not None:
print("WS: SEND EXCEPTION")
raise bg_send_task.exception()
```

## Expected behaviour

If there is a error, an Exception should be raise.

## Actual behaviour

It is a warning? not Exception raised

## Steps to reproduce

wait a long time, and websocket send_json failed with this warning

## Your environment

Ubuntu 16.04
Anaconda 4.3.1
Python 3.6.1
aiohttp 2.0.4

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.