aio-libs / aio-libs/aiohttp

websocket connection is closing.

Offen
#1,768 16 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Python
Sterne
16.5k
Forks
2.4k
Ø Merge
17 Std. 22 Min.
Gemergte PRs (30 T.)
212

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.