fix: harden web terminal paste and reconnect
- Queue bounded ordered terminal input so acknowledgements remain responsive - Prevent stale sockets and retries from replacing healthy connections - Preserve desktop scrollback behavior and add terminal regression coverage Quality gates: frontend tests (91 passed), typecheck, lint, build, Python compilation, LSP diagnostics. Backend pytest skipped by user request.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from asyncio import QueueFull
|
||||
from json import JSONDecodeError
|
||||
import uuid
|
||||
from contextlib import suppress
|
||||
|
||||
@@ -31,6 +33,9 @@ from src.services.terminal.terminal_manager import (
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_PENDING_INPUT_MESSAGES = 64
|
||||
MAX_TERMINAL_INPUT_BYTES = 1024 * 1024
|
||||
|
||||
|
||||
class SessionRef:
|
||||
"""Mutable reference to a terminal session, allowing updates during reset."""
|
||||
@@ -320,8 +325,38 @@ async def _handle_terminal_websocket(
|
||||
)
|
||||
|
||||
|
||||
async def _input_write_loop(input_queue: asyncio.Queue[tuple[object, bytes]]) -> None:
|
||||
"""Serialize PTY writes without blocking terminal control messages."""
|
||||
while True:
|
||||
session, data = await input_queue.get()
|
||||
try:
|
||||
await session.write_input(data) # type: ignore[attr-defined]
|
||||
except Exception:
|
||||
logger.debug("Terminal input write failed", exc_info=True)
|
||||
finally:
|
||||
input_queue.task_done()
|
||||
|
||||
|
||||
def _queue_terminal_input(
|
||||
input_queue: asyncio.Queue[tuple[object, bytes]], session: object, data: bytes
|
||||
) -> bool:
|
||||
"""Queue bounded terminal input without blocking control-message processing."""
|
||||
if len(data) > MAX_TERMINAL_INPUT_BYTES:
|
||||
return False
|
||||
try:
|
||||
input_queue.put_nowait((session, data))
|
||||
except QueueFull:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def _write_loop(session_ref: SessionRef, websocket, instance_id: str) -> None:
|
||||
"""Read input from WebSocket and send to container."""
|
||||
"""Receive terminal messages while a dedicated worker serializes PTY input."""
|
||||
input_queue: asyncio.Queue[tuple[object, bytes]] = asyncio.Queue(
|
||||
maxsize=MAX_PENDING_INPUT_MESSAGES
|
||||
)
|
||||
input_writer = asyncio.create_task(_input_write_loop(input_queue))
|
||||
|
||||
try:
|
||||
while True:
|
||||
session = session_ref.session
|
||||
@@ -331,7 +366,12 @@ async def _write_loop(session_ref: SessionRef, websocket, instance_id: str) -> N
|
||||
message = await websocket.receive()
|
||||
if message["type"] == "websocket.receive":
|
||||
if "bytes" in message:
|
||||
await session.write_input(message["bytes"])
|
||||
if not _queue_terminal_input(
|
||||
input_queue, session, message["bytes"]
|
||||
):
|
||||
logger.warning("Terminal input buffer exceeded for %s", instance_id)
|
||||
await websocket.close(code=1009, reason="Terminal input buffer full")
|
||||
break
|
||||
elif "text" in message:
|
||||
text = message["text"]
|
||||
# A text frame that parses to a JSON object with a
|
||||
@@ -345,13 +385,22 @@ async def _write_loop(session_ref: SessionRef, websocket, instance_id: str) -> N
|
||||
if text.startswith("{"):
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
except JSONDecodeError:
|
||||
parsed = None
|
||||
if isinstance(parsed, dict) and "type" in parsed:
|
||||
ctrl = parsed
|
||||
|
||||
if ctrl is None:
|
||||
await session.write_input(text.encode("utf-8"))
|
||||
if not _queue_terminal_input(
|
||||
input_queue, session, text.encode("utf-8")
|
||||
):
|
||||
logger.warning(
|
||||
"Terminal input buffer exceeded for %s", instance_id
|
||||
)
|
||||
await websocket.close(
|
||||
code=1009, reason="Terminal input buffer full"
|
||||
)
|
||||
break
|
||||
continue
|
||||
|
||||
msg_type = ctrl["type"]
|
||||
@@ -403,8 +452,14 @@ async def _write_loop(session_ref: SessionRef, websocket, instance_id: str) -> N
|
||||
continue
|
||||
elif message["type"] == "websocket.disconnect":
|
||||
break
|
||||
except Exception:
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
except (RuntimeError, TypeError, ValueError) as exc:
|
||||
logger.debug("Terminal WebSocket receive loop ended: %s", exc)
|
||||
finally:
|
||||
input_writer.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await input_writer
|
||||
|
||||
|
||||
async def _heartbeat_loop(websocket: WebSocket) -> None:
|
||||
|
||||
Reference in New Issue
Block a user