fix(desktop): wait for the local server to exit when quitting #22996
main fromrefs/pull/22996/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsvia-triage
juliusmarminge added labelsvia-triage:needs-julius
juliusmarminge removed labelsvia-triage
juliusmarminge added labelsbugfix
juliusmarminge removed labelsvia-triage:needs-julius
juliusmarminge added labelspr:needs-julius
juliusmarminge removed labelspr:needs-julius
juliusmarminge added labelspr:gate-failed
juliusmarminge added labelspr:in-progress
juliusmarminge removed labelspr:gate-failed
- @@ -0,0 +138,21 @@138+ # Electron's setproctitle clobbers its own /proc/<pid>/environ, so also list the wrapper's tree.139+ leftover=$( { descendants "$WRAPPER"; for p in /proc/[0-9]*; do [ "$(home_of "${p#/proc/}")" = "$H" ] && echo "${p#/proc/}"; done; } | sort -un | paste -sd, -)140+ log "leftover processes with T3CODE_HOME=$H: ${leftover:-none}"141+}142+143+cdp_close() {144+ node -e '145+const port = process.argv[1];146+(async () => {147+ const targets = await (await fetch(`http://127.0.0.1:${port}/json`)).json();148+ const page = targets.find((t) => t.type === "page" && !t.url.startsWith("devtools"));149+ console.log("CDP page:", page.url);150+ const ws = new WebSocket(page.webSocketDebuggerUrl);151+ ws.onopen = () => ws.send(JSON.stringify({ id: 1, method: "Runtime.evaluate", params: { expression: "window.close()" } }));152+ ws.onmessage = () => { console.log("CDP window.close() sent"); process.exit(0); };153+ ws.onclose = () => { console.log("CDP socket closed"); process.exit(0); };154+ setTimeout(() => process.exit(0), 3000);155+})();' "$CDP_PORT"156+}157+158+echo "fixture=$FIXTURE checkout=$CHECKOUT head=$(git -C "$CHECKOUT" rev-parse HEAD) run=$RUN"
- @@ -0,0 +4,21 @@4+#5+# Usage: apps/desktop/scripts/quit-lock-evidence.sh <checkout> <fixture> <run-dir>6+# checkout repo root with `vp run build:desktop` done (apps/server/dist + apps/desktop/dist-electron)7+# fixture F1 SIGTERM to the Electron main process, idle backend8+# F2 UI quit: window.close() over CDP (window-all-closed -> app.quit)9+# F3 backend SIGSTOPped (ignores SIGTERM), then SIGTERM to Electron10+# F4 backend SIGSTOPped, SIGTERM to Electron, SIGCONT after $F4_DELAY (3s)11+# F12 backend SIGKILLed before quit, then SIGTERM to Electron12+# run-dir fresh directory for the isolated T3CODE_HOME and XDG dirs (never ~/.t3)13+#14+# Needs headless-desktop (xvfb-run + dbus + keyring), lsof, fuser, node >= 22 (node:sqlite).15+# After Electron exits it records the lock holders, a BEGIN EXCLUSIVE probe, and then16+# relaunches on the same home to check the new backend acquires the lock. It only signals17+# PIDs it spawned or found under its own wrapper whose environ has this run's T3CODE_HOME.18+set -uo pipefail19+20+CHECKOUT=$(cd "${1:?checkout}" && pwd)21+FIXTURE=${2:?fixture}22+RUN=$(mkdir -p "${3:?run-dir}" && cd "$3" && pwd)23+F4_DELAY=${F4_DELAY:-3}24+CDP_PORT=${CDP_PORT:-9333}@@ -0,0 +152,21 @@152+ ws.onmessage = () => { console.log("CDP window.close() sent"); process.exit(0); };153+ ws.onclose = () => { console.log("CDP socket closed"); process.exit(0); };154+ setTimeout(() => process.exit(0), 3000);155+})();' "$CDP_PORT"156+}157+158+echo "fixture=$FIXTURE checkout=$CHECKOUT head=$(git -C "$CHECKOUT" rev-parse HEAD) run=$RUN"159+echo "host=$(hostname) kernel=$(uname -r) node=$(node -v) electron=v$(cat "$(dirname "$ELECTRON")/version") cpus=$(nproc)"160+launch primary || { [ -n "$MAIN" ] && kill -TERM "$MAIN"; sleep 20; cleanup_orphans; exit 1; }161+[ -n "${SCREENSHOT:-}" ] && {162+ sleep 4163+ # Electron's environ is clobbered, so take the display and auth file from Xvfb's argv.164+ xvfb=$(for p in $(descendants "$WRAPPER"); do [ "$(cat /proc/$p/comm 2>/dev/null)" = Xvfb ] && tr '\0' ' ' < /proc/$p/cmdline; done)165+ DISPLAY=$(awk '{print $2}' <<< "$xvfb") XAUTHORITY=$(sed -n 's/.*-auth \([^ ]*\).*/\1/p' <<< "$xvfb") \166+ import -window root "$SCREENSHOT" && log "screenshot: $SCREENSHOT"167+}168+log "running state:"; ptable; lockstate169+170+case "$FIXTURE" in171+ F1) t0=$(now); log "QUIT: SIGTERM electron main $MAIN"; kill -TERM "$MAIN" ;;172+ F2) t0=$(now); log "QUIT: CDP window.close()"; cdp_close 2>&1 | sed 's/^/ /' ;;@@ -0,0 +166,21 @@166+ import -window root "$SCREENSHOT" && log "screenshot: $SCREENSHOT"167+}168+log "running state:"; ptable; lockstate169+170+case "$FIXTURE" in171+ F1) t0=$(now); log "QUIT: SIGTERM electron main $MAIN"; kill -TERM "$MAIN" ;;172+ F2) t0=$(now); log "QUIT: CDP window.close()"; cdp_close 2>&1 | sed 's/^/ /' ;;173+ F3) log "SIGSTOP backend $BACKEND"; kill -STOP "$BACKEND"; sleep 0.3174+ t0=$(now); log "QUIT: SIGTERM electron main $MAIN"; kill -TERM "$MAIN" ;;175+ F4) log "SIGSTOP backend $BACKEND"; kill -STOP "$BACKEND"; sleep 0.3176+ t0=$(now); log "QUIT: SIGTERM electron main $MAIN"; kill -TERM "$MAIN"177+ (sleep "$F4_DELAY"; log "SIGCONT backend $BACKEND (t0+${F4_DELAY}s)"; kill -CONT "$BACKEND") & ;;178+ F12) log "SIGKILL backend $BACKEND before quit"; kill -KILL "$BACKEND"179+ for _ in $(seq 1 40); do kill -0 "$BACKEND" 2>/dev/null || break; sleep 0.1; done180+ sleep 2; log "after backend kill:"; ptable181+ for p in $(descendants "$MAIN"); do182+ [[ "$(tr '\0' ' ' 2>/dev/null < /proc/$p/cmdline)" == *apps/server/dist/bin.mjs* ]] && ours "$p" \183+ && { log "backend respawned as pid=$p"; SEEN_PIDS+=("$p"); BACKEND_PGID=$(ps -o pgid= -p "$p" | tr -d ' '); }184+ done185+ t0=$(now); log "QUIT: SIGTERM electron main $MAIN"; kill -TERM "$MAIN" ;;186+ *) echo "unknown fixture $FIXTURE" >&2; exit 2 ;;
juliusmarminge added labelspr:gate-failed
juliusmarminge removed labelspr:in-progress
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
juliusmarminge merged this6e21f81
juliusmarminge closed this
Sign in to join this conversation.


Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — Desktop quit and update installation now wait for backend exit even after the shutdown warning timeout. Unresolved evidence-script concerns require attention: its shared CDP endpoint may close an unrelated window, and failed close requests can leave the run waiting indefinitely.
Needs human approval because:
- 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).