1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 11:02:42 +03:00
parent 139e43e4c4
commit ae3496ac50
2 changed files with 19 additions and 0 deletions

View file

@ -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

View file

@ -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