From ae3496ac50416f975bc1a5b89fe6b7fc5ffc4f19 Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 10 Nov 2026 11:02:42 +0300 Subject: [PATCH] fix(console): a VNC session ends when its host drops off the network novncd connects to a VM's VNC server directly when it listens on a reachable address, with no timeout once connected: a console may stay quiet for long. When the network to the host dropped without a word, nothing noticed it; the browser kept a frozen console and novncd its proxy process until the user closed the tab or the kernel gave up after many minutes. The socket now has TCP keepalive (30 s idle, then 3 probes 10 s apart) and TCP_USER_TIMEOUT (60 s for data left unanswered). Measured against compute1 with the port's packets dropped: quiet and busy sockets both ended after 61 s; a socket without these options still waited after 200 s. SSH-tunnelled consoles already have SSH's keepalive. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- console/novncd | 8 ++++++++ console/tests/test_novncd.py | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/console/novncd b/console/novncd index 5bd030b4..8b7b2f50 100755 --- a/console/novncd +++ b/console/novncd @@ -78,6 +78,10 @@ def get_connection_infos(instance): WILDCARD = (None, "", "0.0.0.0", "::") +# A host that drops off the network sends nothing to say so: TCP keepalive +# notices it on a quiet console, TCP_USER_TIMEOUT on one with data unanswered, +# and the session ends within about a minute (Linux has every option). +KEEPALIVE = {"TCP_KEEPIDLE": 30, "TCP_KEEPINTVL": 10, "TCP_KEEPCNT": 3, "TCP_USER_TIMEOUT": 60000} def bounded_connect(host, port, connect=True): @@ -85,6 +89,10 @@ def bounded_connect(host, port, connect=True): waits for one that does not answer as long as the OS does (some 2 minutes).""" tsock = socket.create_connection((host, port), timeout=CONNECT_TIMEOUT) tsock.settimeout(None) # a console may stay quiet for long once connected + tsock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) + for name, value in KEEPALIVE.items(): + if hasattr(socket, name): + tsock.setsockopt(socket.IPPROTO_TCP, getattr(socket, name), value) return tsock diff --git a/console/tests/test_novncd.py b/console/tests/test_novncd.py index 38c52818..ff7ec535 100644 --- a/console/tests/test_novncd.py +++ b/console/tests/test_novncd.py @@ -4,6 +4,7 @@ port it cannot listen on ends it with a failure status, which systemd restarts." import socket import subprocess import sys +import unittest from pathlib import Path from django.test import SimpleTestCase @@ -234,6 +235,16 @@ class NovncdDaemonLogicTestCase(TestCase): create_connection.assert_called_once_with(("192.0.2.40", 5900), timeout=novncd_mod.CONNECT_TIMEOUT) tsock.settimeout.assert_called_once_with(None) + @unittest.skipUnless(hasattr(socket, "TCP_USER_TIMEOUT"), "Linux socket options") + def test_a_vnc_server_gone_from_the_network_is_noticed(self): + listener = socket.create_server(("127.0.0.1", 0)) + self.addCleanup(listener.close) + tsock = novncd_mod.bounded_connect(*listener.getsockname()) + self.addCleanup(tsock.close) + self.assertEqual(tsock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE), 1) + self.assertEqual(tsock.getsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE), 30) + self.assertEqual(tsock.getsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT), 60000) + def test_a_token_without_its_users_session_is_refused_and_logged(self): from logs.models import Logs