Fix broken websocket console with Python 3.11

This commit is contained in:
grossmj 2023-02-18 15:32:57 +08:00
parent 30c85703c8
commit 1f85abb036
2 changed files with 17 additions and 1 deletions

View File

@ -469,7 +469,18 @@ class BaseNode:
try: try:
# keep forwarding websocket data in both direction # keep forwarding websocket data in both direction
await asyncio.wait([ws_forward(telnet_writer), telnet_forward(telnet_reader)], return_when=asyncio.FIRST_COMPLETED) if sys.version_info >= (3, 11, 0):
# Starting with Python 3.11, passing coroutine objects to wait() directly is forbidden.
aws = [asyncio.create_task(ws_forward(telnet_writer)), asyncio.create_task(telnet_forward(telnet_reader))]
else:
aws = [ws_forward(telnet_writer), telnet_forward(telnet_reader)]
done, pending = await asyncio.wait(aws, return_when=asyncio.FIRST_COMPLETED)
for task in done:
if task.exception():
log.warning(f"Exception while forwarding WebSocket data to Telnet server {task.exception()}")
for task in pending:
task.cancel()
finally: finally:
log.info("Client has disconnected from console WebSocket") log.info("Client has disconnected from console WebSocket")
if not ws.closed: if not ws.closed:

View File

@ -29,6 +29,9 @@ from gns3server.schemas.node import (
NODE_DUPLICATE_SCHEMA NODE_DUPLICATE_SCHEMA
) )
import logging
log = logging.getLogger(__name__)
class NodeHandler: class NodeHandler:
""" """
@ -500,6 +503,8 @@ class NodeHandler:
await ws.send_bytes(msg.data) await ws.send_bytes(msg.data)
elif msg.type == aiohttp.WSMsgType.ERROR: elif msg.type == aiohttp.WSMsgType.ERROR:
break break
except ConnectionResetError:
log.info("Websocket console connection with compute disconnected")
finally: finally:
if not ws.closed: if not ws.closed:
await ws.close() await ws.close()