1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +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