Repository navigation
Fix Windows update-relaunch killing the live terminal daemon (session loss on update) - #7340
Conversation
On a Windows update relaunch the daemon can be wedged past every RPC budget (final checkpoint flush + installer/AV disk pressure), so the 3s health check AND the 5s session-list hello both time out while sessions are still alive - and the launcher failed closed, killing the daemon and every terminal session it owned. - Adopt an unresponsive daemon whose pipe still accepts a raw connection; a new rejected health state keeps replacing daemons that answered and refused the handshake (never adoptable). - Give Windows pid files a real startedAtMs (daemon self-reports it in the ready IPC message) and verify it via CIM CreationDate piggybacked on the existing command-line query, so the pid-recycling guard is no longer inert on win32. - Only delete legacy daemon pid/token files when the pid-file process is provably dead; deleting a live daemon''s token made its sessions permanently unadoptable after a protocol bump. - Capture agent resume records every 60s in the renderer (skipping unchanged records) so hard kills still leave a fresh resume record.
📝 WalkthroughWalkthroughDaemon readiness now reports a self-computed startup timestamp, and daemon health checks distinguish a new "rejected" state (handshake refused) from "unreachable." Windows process identity verification switches to a combined CIM query returning command line and start time, validated with tolerance-based comparison. The daemon launcher adopts existing daemons when liveness can't be verified but the socket responds and health isn't rejected, and legacy daemon cleanup now checks process aliveness via pid-file parsing before deleting stale artifacts. Separately, the renderer adds a periodic 60-second timer to capture sleeping-agent resume sessions, with a new equivalence check preventing redundant record rewrites when nothing meaningfully changed. Sequence Diagram(s)See diagrams embedded in review stack layers above for Windows identity verification and periodic sleeping-agent capture flows. Related PRs: None specified. Suggested labels: daemon, windows, renderer, tests Suggested reviewers: None specified. 🐰 A daemon wakes and tells the time, 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Root-cause attribution correction (post-merge investigation): the July update-time session losses were caused by the NSIS installer's install-dir process sweep, unmasked by #6968 moving OpenConsole.exe under resources/ — see #7420 for the harness-verified chain. The update-relaunch wedge-kill path fixed here is a real but distinct mechanism (reproduced in isolation killing a live daemon owning a live session), and becomes the active protection layer at update-relaunch once #7420 keeps the daemon alive through installs. The two fixes are complementary layers; nothing here needs reverting. |
Summary
Windows-only terminal session loss on app update: the update relaunch could kill the live terminal daemon — and every session it owned — instead of adopting it. Root-caused on the affected machine, fixed, and verified end-to-end against a real wedged daemon.
Root cause
The launcher's unhealthy path in
daemon-init.tswas fail-closed. On an update relaunch:disconnectDaemondrains finalgetSnapshotcheckpoint RPCs for every session — with many sessions the daemon's single event loop is grinding through snapshot serialization exactly when NSIS writes the new app and Defender scans it.checkDaemonHealthgets 3s for two RPC round-trips → times out →'unreachable'.null.killStaleDaemon(), which CIM-verified the pid (powershell spawns work fine even when the daemon's event loop is wedged) and TerminateProcess'd the live daemon.The arithmetic matches the production incident exactly: 3s (health) + 5s (hello) + ~1s (CIM query) ≈ the observed 9-second gap between the updated main process starting (
2:48:34,--updated) and the replacement daemon being forked (2:48:43) on the machine where rc.1→rc.2 lost 60 sessions.Why most updates are fine: when the daemon answers within budget, the appVersion-mismatch path already preserves it ("owns N live sessions"). The kill needs the daemon to be unresponsive for ~8 continuous seconds at exactly the relaunch moment — a load-dependent race, worst on machines with many sessions and back-to-back RC installs.
Two adjacent hazards fixed in the same change:
startedAtMs: nullon Windows pid files:getProcessStartedAtMshad no win32 implementation, so the pid-recycling guard (startTimeMatches) was permanently inert on Windows. Not the kill trigger (it fails open), but a disabled safety on exactly the platform that kills by pid.Fix
daemon-init.ts): when both the health check and the session list fail, probe the pipe with a raw connect. A pipe that accepts connections proves a live daemon → adopt it; the adapter reconnects once the daemon drains. A new'rejected'health state (daemon-health.ts) distinguishes "daemon answered and refused the handshake" (never adoptable → still replaced) from "timed out" (possibly wedged → preserved).startedAtMson Windows: the daemon self-reports its start time in the ready IPC message (daemon-entry.ts); the parent falls back to it when the OS query returns null. Verification piggybacks CIMCreationDateon the existing powershell CommandLine query — zero additional process spawns — with a 10s win32 tolerance for exe-bootstrap skew (pid recycling differs by minutes-to-days, so the guard stays effective).process.kill(pid, 0)fails).App.tsx, cross-platform):sleepingAgentSessionsByPaneKeyis now captured every 60s in addition tobeforeunload, so hard kills (crash, forced install, TerminateProcess) still leave a fresh resume record. The store action skips content-unchanged records, so idle ticks write nothing and no scrollback is serialized (respects the PR fix: preserve terminal session state during periodic saves #461 lesson documented in App.tsx).Verification on the affected machine (Windows 11, the rc.1→rc.2 incident machine)
1. Discriminating experiment (live app) — quit and reopened installed Orca (no update): the relaunched app adopted the existing daemon (pid 26428 unchanged, pid file untouched, sessions reattached — including the very session this fix was authored in). Plain restarts adopt fine; the bug is update-relaunch-specific, consistent with the root cause.
2. Wedge reproduction + fix proof (real daemon, real ConPTY session, real decision functions) — forked a throwaway daemon from this branch, created a live ConPTY session, then suspended the daemon process (
NtSuspendProcess) to reproduce the wedge, and ran the branch's actualcheckDaemonHealth/DaemonClient/killStaleDaemonagainst it:This also empirically confirms the linchpin OS assumption: a fully-wedged process's named pipe still accepts raw connections (kernel-side pending instances) even after the health check and the client connect have each consumed one — so the probe is a reliable liveness signal exactly when RPCs are not.
3. Regression tests (all green, plus typecheck/lint):
startedAtMsmetadata — the pre-fix Windows pid-file state)rejected, notunreachable(realDaemonServer)Cross-platform notes
startedAtMsderivation is unchanged (ps lstart//proc); the self-report is only a fallback when the OS query returns null.