aio-libs / aio-libs/aiohttp

websocket connection is closing.

オープン
#1,768 コメント 16 件 リアクション 4 件 担当者 0 名 GitHub で見る
enhancement
主要言語
Python
スター
16.5k
フォーク
2.4k
平均マージ
17時間 22分
マージ済み PR(30日)
212

説明

## 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

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。