Merge pull request #2189 from GNS3/fix/2188

Fix broken websocket console with Python 3.11
This commit is contained in:
Jeremy Grossmann 2023-02-18 15:44:06 +08:00 committed by GitHub
commit 3040e87a7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -469,7 +469,18 @@ class BaseNode:
try:
# 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:
log.info("Client has disconnected from console WebSocket")
if not ws.closed:

View File

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