From 7d60afb9fa80ead81ec75a6240580c24ddbcb927 Mon Sep 17 00:00:00 2001 From: Dilshod Rasulov Date: Fri, 21 Aug 2026 21:06:44 +0500 Subject: [PATCH] fix(console): stop leaking termproxy sessions and orphaned terminals Opening an LXC console left a failed task on the Proxmox node: failed waiting for client: timed out TASK ERROR: command '/usr/bin/termproxy 5900 ... vzctlconsole101' failed connectTerminal() called GET /api/{sid}/container/{vid}/terminal first, which creates a termproxy session on the node, but only the `node` field of the response was used - port and ticket were discarded. The ws endpoint then created a second termproxy session and connected to that one. Nobody ever attached to the first, so Proxmox waited ~10s for a client and failed the task. The console also rendered a black screen in dev: React StrictMode mounts the effect twice, connect* are async, and the effect cleanup ran before they had assigned wsRef/termRef, so it had nothing to clean up. Two xterm instances ended up in the same container - the first one filled the viewport while the live one was pushed out of the overflow-hidden area, which is why the badge read "connected" over an empty screen. - drop the redundant REST call; the ws endpoint owns the termproxy - remove the now unused GET /api/{server_id}/container/{vmid}/terminal - add a per-session cancellation guard: a stale connect closes its own socket and disposes its own terminal without touching refs that may already point at the live session - route mount and the reconnect button through a single startSession() that cancels and tears down the previous session first - apply the same guard to the node shell page --- backend/app/api/proxmox/vms.py | 73 --------- frontend/src/features/console/ConsolePage.tsx | 143 ++++++++++++------ .../src/features/console/NodeShellPage.tsx | 73 +++++++-- 3 files changed, 156 insertions(+), 133 deletions(-) diff --git a/backend/app/api/proxmox/vms.py b/backend/app/api/proxmox/vms.py index 69633a6..754ad0a 100644 --- a/backend/app/api/proxmox/vms.py +++ b/backend/app/api/proxmox/vms.py @@ -2884,79 +2884,6 @@ def get_vm_terminal( raise HTTPException(status_code=500, detail=str(e)) -@router.get("/api/{server_id}/container/{vmid}/terminal") -def get_container_terminal( - server_id: int, - vmid: int, - node: str, - db: Session = Depends(get_db), - current_user: User = Depends(PermissionChecker("vm:console")) -): - """Получить данные для xterm.js терминального подключения к LXC контейнеру""" - import requests - - # VPS-style user isolation: check container ownership for limited users - require_vm_access(db, current_user, server_id, vmid) - - server = db.query(ProxmoxServer).filter(ProxmoxServer.id == server_id).first() - if not server: - raise HTTPException(status_code=404, detail="Proxmox server not found") - - try: - password_to_use = server.password if server.password else None - auth_username = server.api_user.split("!")[0] if "!" in server.api_user else server.api_user - - if not password_to_use: - raise HTTPException(status_code=400, detail="Terminal requires password authentication") - - # 1. Получаем auth ticket - auth_response = requests.post( - f"https://{server.ip_address}:8006/api2/json/access/ticket", - data={"username": auth_username, "password": password_to_use}, - verify=server.verify_ssl, - timeout=10 - ) - - if auth_response.status_code != 200: - raise HTTPException(status_code=401, detail="Failed to authenticate to Proxmox") - - auth_data = auth_response.json().get("data", {}) - auth_ticket = auth_data.get("ticket") - csrf_token = auth_data.get("CSRFPreventionToken") - - # 2. Создаём terminal proxy - term_response = requests.post( - f"https://{server.ip_address}:8006/api2/json/nodes/{node}/lxc/{vmid}/termproxy", - headers={"CSRFPreventionToken": csrf_token}, - cookies={"PVEAuthCookie": auth_ticket}, - verify=server.verify_ssl, - timeout=10 - ) - - if term_response.status_code != 200: - raise HTTPException(status_code=500, detail=f"Failed to create terminal proxy: {term_response.text}") - - term_data = term_response.json().get("data", {}) - - response_data = { - 'port': term_data.get('port'), - 'ticket': term_data.get('ticket'), - 'host': server.ip_address, - 'node': node, - 'vmid': vmid, - 'type': 'lxc', - 'auth_ticket': auth_ticket - } - - logger.info(f"User {current_user.username} opened terminal for LXC {vmid}") - return JSONResponse(content=response_data) - except HTTPException: - raise - except Exception as e: - logger.error(f"Error getting terminal for LXC {vmid}: {e}") - raise HTTPException(status_code=500, detail=str(e)) - - @router.post("/api/{server_id}/container/{vmid}/exec") async def exec_in_container( server_id: int, diff --git a/frontend/src/features/console/ConsolePage.tsx b/frontend/src/features/console/ConsolePage.tsx index e215717..d56960d 100644 --- a/frontend/src/features/console/ConsolePage.tsx +++ b/frontend/src/features/console/ConsolePage.tsx @@ -73,6 +73,14 @@ interface VNCData { auth_ticket?: string; } +// Сессия консоли переживает один проход эффекта: connect* асинхронны, а React +// StrictMode в dev монтирует эффект дважды. Без флага отмены вторая пара +// xterm/RFB ложится в тот же контейнер поверх первой — «осиротевший» терминал +// остаётся в DOM и занимает видимую область, из-за чего экран выглядит пустым. +interface SessionGuard { + cancelled: boolean; +} + interface RFBHandle { disconnect: () => void; sendCtrlAltDel: () => void; @@ -108,12 +116,37 @@ export default function ConsolePage() { const vid = Number(vmid); const power = usePowerAction(sid, vid, type, node); + const guardRef = useRef(null); + + // Освобождает ресурсы текущей сессии (RFB / xterm / WebSocket / keepalive). + const cleanupSession = useCallback(() => { + if (rfbRef.current) { + try { rfbRef.current.disconnect(); } catch { /* */ } + rfbRef.current = null; + } + if (wsRef.current) { + wsRef.current.close(); + wsRef.current = null; + } + if (keepaliveRef.current) { + clearInterval(keepaliveRef.current); + keepaliveRef.current = null; + } + if (termRef.current) { + const t = termRef.current as { terminal: { dispose: () => void }; cleanup: () => void }; + t.cleanup(); + t.terminal.dispose(); + termRef.current = null; + } + }, []); + // ==================== VNC Console (QEMU) ==================== - const connectVNC = useCallback(async () => { + const connectVNC = useCallback(async (guard: SessionGuard) => { try { const data = await apiClient.get( `/proxmox/api/${sid}/vm/${vid}/vnc?node=${node}` ); + if (guard.cancelled) return; // Dynamic import noVNC. The package ships CJS, so depending on bundler // interop the result can be: { default: RFB }, { default: { default: RFB, __esModule: true } }, @@ -149,7 +182,7 @@ export default function ConsolePage() { const wsUrl = `${wsProto}//${window.location.host}/proxmox/ws/vnc/${sid}/${data.node}/qemu/${vid}?${params}`; - if (!containerRef.current) return; + if (guard.cancelled || !containerRef.current) return; // Pass URL string to RFB, NOT an open WebSocket const rfb = new RFB(containerRef.current, wsUrl, { @@ -160,10 +193,12 @@ export default function ConsolePage() { rfb.resizeSession = false; rfb.addEventListener('connect', () => { + if (guard.cancelled) return; setStatus('connected'); }); rfb.addEventListener('disconnect', (e: CustomEvent) => { + if (guard.cancelled) return; if (e.detail?.clean) { setStatus('error'); setErrorMsg('Connection closed'); @@ -176,6 +211,7 @@ export default function ConsolePage() { rfbRef.current = rfb as RFBHandle; setScaleToFit(true); } catch (err) { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(err instanceof Error ? err.message : 'Failed to connect'); } @@ -183,7 +219,7 @@ export default function ConsolePage() { // ==================== xterm.js session (LXC terminal / VM serial) ==================== // Общая обвязка xterm + WebSocket. wsPath — путь до нужного ws-эндпоинта Proxmox-прокси. - const setupXtermSession = useCallback(async (wsPath: string) => { + const setupXtermSession = useCallback(async (wsPath: string, guard: SessionGuard) => { // Dynamic import xterm.js const [{ Terminal }, { FitAddon }, { WebLinksAddon }] = await Promise.all([ import('@xterm/xterm'), @@ -191,7 +227,7 @@ export default function ConsolePage() { import('@xterm/addon-web-links'), ]); - if (!containerRef.current) return; + if (guard.cancelled || !containerRef.current) return; const fitAddon = new FitAddon(); const webLinksAddon = new WebLinksAddon(); @@ -219,9 +255,12 @@ export default function ConsolePage() { const wsUrl = `${wsProto}//${window.location.host}${wsPath}${tokenParam}`; const ws = new WebSocket(wsUrl); ws.binaryType = 'arraybuffer'; - wsRef.current = ws; ws.onopen = () => { + if (guard.cancelled) { + ws.close(); + return; + } setStatus('connected'); const { cols, rows } = terminal; ws.send(`1:${cols}:${rows}:`); @@ -231,11 +270,20 @@ export default function ConsolePage() { }; ws.onmessage = (event) => { + if (guard.cancelled) return; if (event.data instanceof ArrayBuffer) terminal.write(new Uint8Array(event.data)); else terminal.write(event.data); }; - ws.onclose = () => { setStatus('error'); setErrorMsg('Terminal connection closed'); }; - ws.onerror = () => { setStatus('error'); setErrorMsg('Terminal connection error'); }; + ws.onclose = () => { + if (guard.cancelled) return; + setStatus('error'); + setErrorMsg('Terminal connection closed'); + }; + ws.onerror = () => { + if (guard.cancelled) return; + setStatus('error'); + setErrorMsg('Terminal connection error'); + }; terminal.onData((data: string) => { if (ws.readyState === WebSocket.OPEN) { @@ -249,62 +297,75 @@ export default function ConsolePage() { const handleResize = () => fitAddon.fit(); window.addEventListener('resize', handleResize); + + // Сессию могли отменить, пока грузились чанки xterm. Убираем за собой сами и + // НЕ трогаем refs: они уже могут указывать на актуальную сессию. Иначе в + // контейнере остаётся невидимый терминал поверх живого — чёрный экран. + if (guard.cancelled) { + window.removeEventListener('resize', handleResize); + terminal.dispose(); + ws.close(); + return; + } + + wsRef.current = ws; termRef.current = { terminal, fitAddon, cleanup: () => window.removeEventListener('resize', handleResize) }; }, []); // ==================== Terminal Console (LXC) ==================== - const connectTerminal = useCallback(async () => { + const connectTerminal = useCallback(async (guard: SessionGuard) => { try { - const data = await apiClient.get( - `/proxmox/api/${sid}/container/${vid}/terminal?node=${node}` - ); - await setupXtermSession(`/proxmox/ws/terminal/${sid}/${data.node}/${vid}`); + // Termproxy-сессию создаёт сам ws-эндпоинт. Отдельный REST-вызов + // (GET .../container/{vmid}/terminal) поднимал бы на ноде вторую + // termproxy-сессию, к которой никто не подключается: Proxmox ждёт клиента + // ~10 секунд и завершает задачу с «failed waiting for client: timed out». + await setupXtermSession(`/proxmox/ws/terminal/${sid}/${node}/${vid}`, guard); } catch (err) { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(err instanceof Error ? err.message : 'Failed to connect'); } }, [sid, vid, node, setupXtermSession]); // ==================== Serial Console (QEMU) ==================== - const connectSerial = useCallback(async () => { + const connectSerial = useCallback(async (guard: SessionGuard) => { try { // Убедиться, что у VM есть serial0 (добавит при отсутствии) await apiClient.post(`/proxmox/api/${sid}/vm/${vid}/serial/enable?node=${node}`); - await setupXtermSession(`/proxmox/ws/serial/${sid}/${node}/${vid}`); + if (guard.cancelled) return; + await setupXtermSession(`/proxmox/ws/serial/${sid}/${node}/${vid}`, guard); } catch (err) { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(err instanceof Error ? err.message : 'Failed to connect'); } }, [sid, vid, node, setupXtermSession]); // ==================== Connect on mount ==================== + // Старт новой сессии: старая помечается отменённой и полностью убирается, + // чтобы в контейнере всегда был ровно один живой терминал/RFB. + const startSession = useCallback(() => { + if (guardRef.current) guardRef.current.cancelled = true; + cleanupSession(); + + const guard: SessionGuard = { cancelled: false }; + guardRef.current = guard; + setStatus('connecting'); + setErrorMsg(''); + + if (isSerial) connectSerial(guard); + else if (type === 'qemu') connectVNC(guard); + else connectTerminal(guard); + }, [isSerial, type, connectSerial, connectVNC, connectTerminal, cleanupSession]); + useEffect(() => { - if (isSerial) { - connectSerial(); - } else if (type === 'qemu') { - connectVNC(); - } else { - connectTerminal(); - } + startSession(); return () => { - // Cleanup - if (rfbRef.current) { - try { (rfbRef.current as { disconnect: () => void }).disconnect(); } catch { /* */ } - } - if (wsRef.current) { - wsRef.current.close(); - } - if (keepaliveRef.current) { - clearInterval(keepaliveRef.current); - } - if (termRef.current) { - const t = termRef.current as { terminal: { dispose: () => void }; cleanup: () => void }; - t.cleanup(); - t.terminal.dispose(); - } + if (guardRef.current) guardRef.current.cancelled = true; + cleanupSession(); }; - }, [type, isSerial, connectVNC, connectTerminal, connectSerial]); + }, [startSession, cleanupSession]); // ==================== Fullscreen ==================== const toggleFullscreen = () => { @@ -574,13 +635,7 @@ export default function ConsolePage() { diff --git a/frontend/src/features/console/NodeShellPage.tsx b/frontend/src/features/console/NodeShellPage.tsx index ff25a86..3fcda45 100644 --- a/frontend/src/features/console/NodeShellPage.tsx +++ b/frontend/src/features/console/NodeShellPage.tsx @@ -25,10 +25,30 @@ export default function NodeShellPage() { const wsRef = useRef(null); const keepaliveRef = useRef | null>(null); const termRef = useRef<{ terminal: { dispose: () => void }; cleanup: () => void } | null>(null); + // Флаг отмены: connect() асинхронен, а React StrictMode в dev монтирует эффект + // дважды — без него второй xterm ложится в контейнер поверх первого и видимой + // остаётся «осиротевшая» пустая сессия. + const guardRef = useRef<{ cancelled: boolean } | null>(null); const sid = Number(serverId); - const connect = useCallback(async () => { + const cleanupSession = useCallback(() => { + if (wsRef.current) { + wsRef.current.close(); + wsRef.current = null; + } + if (keepaliveRef.current) { + clearInterval(keepaliveRef.current); + keepaliveRef.current = null; + } + if (termRef.current) { + termRef.current.cleanup(); + termRef.current.terminal.dispose(); + termRef.current = null; + } + }, []); + + const connect = useCallback(async (guard: { cancelled: boolean }) => { try { const [{ Terminal }, { FitAddon }, { WebLinksAddon }] = await Promise.all([ import('@xterm/xterm'), @@ -36,7 +56,7 @@ export default function NodeShellPage() { import('@xterm/addon-web-links'), ]); - if (!containerRef.current) return; + if (guard.cancelled || !containerRef.current) return; const fitAddon = new FitAddon(); const webLinksAddon = new WebLinksAddon(); @@ -65,9 +85,12 @@ export default function NodeShellPage() { const wsUrl = `${wsProto}//${window.location.host}/proxmox/ws/node-shell/${sid}/${node}?${params}`; const ws = new WebSocket(wsUrl); ws.binaryType = 'arraybuffer'; - wsRef.current = ws; ws.onopen = () => { + if (guard.cancelled) { + ws.close(); + return; + } setStatus('connected'); const { cols, rows } = terminal; ws.send(`1:${cols}:${rows}:`); @@ -77,6 +100,7 @@ export default function NodeShellPage() { }; ws.onmessage = (event) => { + if (guard.cancelled) return; if (event.data instanceof ArrayBuffer) { terminal.write(new Uint8Array(event.data)); } else { @@ -85,10 +109,12 @@ export default function NodeShellPage() { }; ws.onclose = () => { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(t('console.terminal_closed', 'Соединение с терминалом закрыто')); }; ws.onerror = () => { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(t('console.terminal_error', 'Ошибка соединения с терминалом')); }; @@ -106,24 +132,43 @@ export default function NodeShellPage() { const handleResize = () => fitAddon.fit(); window.addEventListener('resize', handleResize); + + // Сессию могли отменить, пока грузились чанки xterm. Убираем за собой сами + // и не трогаем refs — они уже могут указывать на актуальную сессию. + if (guard.cancelled) { + window.removeEventListener('resize', handleResize); + terminal.dispose(); + ws.close(); + return; + } + + wsRef.current = ws; termRef.current = { terminal, cleanup: () => window.removeEventListener('resize', handleResize) }; } catch (err) { + if (guard.cancelled) return; setStatus('error'); setErrorMsg(err instanceof Error ? err.message : 'Failed to connect'); } }, [sid, node, cmd, t]); + const startSession = useCallback(() => { + if (guardRef.current) guardRef.current.cancelled = true; + cleanupSession(); + + const guard = { cancelled: false }; + guardRef.current = guard; + setStatus('connecting'); + setErrorMsg(''); + connect(guard); + }, [connect, cleanupSession]); + useEffect(() => { - connect(); + startSession(); return () => { - if (wsRef.current) wsRef.current.close(); - if (keepaliveRef.current) clearInterval(keepaliveRef.current); - if (termRef.current) { - termRef.current.cleanup(); - termRef.current.terminal.dispose(); - } + if (guardRef.current) guardRef.current.cancelled = true; + cleanupSession(); }; - }, [connect]); + }, [startSession, cleanupSession]); const toggleFullscreen = () => { if (!document.fullscreenElement) { @@ -182,11 +227,7 @@ export default function NodeShellPage() {