fix(autostart): wait for the VNC server before starting web-vnc

autostart-run.bat checked 5900 once at startup; at boot the UltraVNC service may
not be up yet, so web-vnc either spawned a 2nd VNC server (conflict) or started
serving on :8080 before the VNC server was ready, breaking browser connections.

Now autostart-run.bat polls 127.0.0.1:5900 for up to ~30s (one PowerShell loop:
TcpClient + Start-Sleep 1, exits on first accepted connection) and only then
launches web-vnc.exe -- without --spawn when 5900 already listens, or spawning a
local server (portable vnc\winvnc.exe / tvnserver.exe, else auto-detect) if it
never came up. Docs (AGENTS.md, README.md) updated.
This commit is contained in:
Codex
2026-08-05 16:14:39 +03:00
parent 9b8ee94dd5
commit f315618bfc
3 changed files with 43 additions and 21 deletions
+32 -13
View File
@@ -2,10 +2,12 @@
REM ============================================================
REM autostart-run.bat - non-interactive launcher used by the
REM scheduled task created by install-autostart.bat.
REM Reads the password from webvnc.conf, finds a LOCAL VNC server
REM (does NOT download anything), and starts web-vnc.exe in the
REM foreground. The task keeps the process alive and restarts it
REM on next boot.
REM Reads the password from webvnc.conf, WAITS for the VNC server
REM on 127.0.0.1:5900 (the UltraVNC service may still be starting
REM at boot), and only then starts web-vnc.exe in the foreground.
REM If no VNC server comes up, it spawns a local one (portable
REM vnc\winvnc.exe / tvnserver.exe, or auto-detected) so web-vnc
REM does not serve before the VNC server is ready.
REM Exit codes: 0 = server ran and stopped; 1 = no binary;
REM 2 = password not configured; other = web-vnc error.
REM ============================================================
@@ -33,20 +35,37 @@ if "%VNC_PW%"=="" exit /b 2
set "WEBVNC_VNC_PASSWORD=%VNC_PW%"
set "VNC_PW="
REM --- 3. VNC server (detect local only; never download at boot) ---
set "SPAWNARGS="
REM --- 3. Wait for the VNC server on 127.0.0.1:5900 ---
REM At boot the UltraVNC service (uvnc_service) may still be starting.
REM Poll up to ~30 s (one PowerShell process loops internally) so we do
REM NOT spawn a 2nd server and do NOT serve before the VNC server is
REM ready. Exits 0 as soon as 5900 accepts a connection, 1 on timeout.
echo Waiting for VNC server on 127.0.0.1:5900 (up to 30 s) ...
powershell -NoProfile -Command "for($i=0;$i -lt 30;$i++){try{$c=New-Object Net.Sockets.TcpClient('127.0.0.1',5900);$c.Close();exit 0}catch{};Start-Sleep -Seconds 1};exit 1"
if not errorlevel 1 goto vnc_up
:vnc_down
echo VNC server is not up after the wait.
set "SPAWN_CMD="
if exist "vnc\winvnc.exe" set "SPAWN_CMD=vnc\winvnc.exe -run"
if exist "vnc\tvnserver.exe" if not defined SPAWN_CMD set "SPAWN_CMD=vnc\tvnserver.exe -run"
if defined SPAWN_CMD set "SPAWNARGS=--spawn-command "%SPAWN_CMD%""
if defined SPAWN_CMD goto have_spawn
set "SPAWNARGS="
set "SPAWNFLAG=--spawn"
powershell -NoProfile -Command "try{$c=New-Object Net.Sockets.TcpClient('127.0.0.1',5900);$c.Close();exit 0}catch{exit 1}"
if not errorlevel 1 (
set "SPAWNFLAG="
set "SPAWNARGS="
)
echo No local VNC server; web-vnc will auto-detect and spawn one.
goto launch
:have_spawn
set "SPAWNARGS=--spawn-command "%SPAWN_CMD%""
set "SPAWNFLAG=--spawn"
echo Starting local VNC server: %SPAWN_CMD%
goto launch
:vnc_up
echo VNC server is up - connecting to it without --spawn.
set "SPAWNARGS="
set "SPAWNFLAG="
:launch
REM --- 4. Launch (foreground; the task host keeps it alive) ---
web-vnc.exe --password-hash "%HASH%" %SPAWNARGS% %SPAWNFLAG% --listen :8080
exit /b %errorlevel%