Repository navigation
fix(pty): keep runtime dirs a surviving daemon still uses - #7463
Conversation
The node-pty native runtime is relocated to a per-app-version dir under userData so live terminals survive NSIS updates. On every launch, removeStaleRuntimeVersions() deleted every *other* version's dir, gated on a "renameSync succeeds => unused => safe to delete" heuristic. That heuristic is false on Windows: running .exe images and mapped .dll files are opened with FILE_SHARE_DELETE, so both the directory rename and the recursive delete succeed while a daemon adopted across the update is still using them. Existing PTYs keep streaming via deferred-delete handles, but the next spawn re-loads conpty.dll from the now-unlinked version dir and fails with ERROR_PATH_NOT_FOUND (code 3) -- the "Cannot find conpty.dll ... error code: 3" new-tab failure. Gate cleanup on real daemon liveness instead: collectInUseRuntimeVersions() reads the daemon-v<N>.pid files under userData/daemon and, for each live daemon, protects the appVersion (runtime dir) it recorded. Deletion now skips the current version and any in-use version, dropping the rename dance. Every failure mode leaks a dir rather than deleting one in use. Rewrites the misleading cleanup test (which modeled no live daemon) and adds coverage for surviving/dead daemons, appVersion:null pin files, multiple protocol versions, and malformed/non-pid files.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis change adds daemon-liveness-aware protection for node-pty native runtime version directories on Windows. A new Changes
Sequence Diagram(s)sequenceDiagram
participant Installer as installRelocatedNodePtyNativeRuntime
participant Ensure as ensureRelocatedNodePtyNativeRuntime
participant Collector as collectInUseRuntimeVersions
participant FS as Filesystem
Installer->>Ensure: call with destRoot, daemonRuntimeDir
Ensure->>Collector: collectInUseRuntimeVersions(daemonRuntimeDir, isDaemonPidAlive)
Collector->>FS: read daemon-v<N>.pid files
Collector->>Collector: check process liveness per pid
Collector-->>Ensure: return inUseVersions set
Ensure->>FS: removeStaleRuntimeVersions(destRoot, keepVersion, inUseVersions)
FS-->>Ensure: stale, unpinned directories removed
Related PRs: None specified. Suggested labels: area/main-process, pty, windows Suggested reviewers: None specified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
) (#7505) * Revert "Preload the daemon windowsHide shim via --require; wrap promisify custom (#7499)" This reverts commit 8f396ba. * Revert "Hide console windows for children of the node.exe-hosted daemon (#7486)" This reverts commit f0fdd3a. * Revert "fix(daemon): relocate daemon host image out of the install-dir kill zone (#7473)" This reverts commit f4faafa. * Revert "fix(pty): keep runtime dirs a surviving daemon still uses (#7463)" This reverts commit 3cd23a1. * Revert "Relocate node-pty ConPTY runtime outside the Windows install dir (fixes update-time terminal loss) (#7421)" This reverts commit 509c41e.
) The node-pty native runtime is relocated to a per-app-version dir under userData so live terminals survive NSIS updates. On every launch, removeStaleRuntimeVersions() deleted every *other* version's dir, gated on a "renameSync succeeds => unused => safe to delete" heuristic. That heuristic is false on Windows: running .exe images and mapped .dll files are opened with FILE_SHARE_DELETE, so both the directory rename and the recursive delete succeed while a daemon adopted across the update is still using them. Existing PTYs keep streaming via deferred-delete handles, but the next spawn re-loads conpty.dll from the now-unlinked version dir and fails with ERROR_PATH_NOT_FOUND (code 3) -- the "Cannot find conpty.dll ... error code: 3" new-tab failure. Gate cleanup on real daemon liveness instead: collectInUseRuntimeVersions() reads the daemon-v<N>.pid files under userData/daemon and, for each live daemon, protects the appVersion (runtime dir) it recorded. Deletion now skips the current version and any in-use version, dropping the rename dance. Every failure mode leaks a dir rather than deleting one in use. Rewrites the misleading cleanup test (which modeled no live daemon) and adds coverage for surviving/dead daemons, appVersion:null pin files, multiple protocol versions, and malformed/non-pid files. Co-authored-by: Neil <[email protected]>
…#7421→stablyai#7499) (stablyai#7505) * Revert "Preload the daemon windowsHide shim via --require; wrap promisify custom (stablyai#7499)" This reverts commit b5710ee. * Revert "Hide console windows for children of the node.exe-hosted daemon (stablyai#7486)" This reverts commit 081cf2f. * Revert "fix(daemon): relocate daemon host image out of the install-dir kill zone (stablyai#7473)" This reverts commit 38c2e2e. * Revert "fix(pty): keep runtime dirs a surviving daemon still uses (stablyai#7463)" This reverts commit 01ad86a. * Revert "Relocate node-pty ConPTY runtime outside the Windows install dir (fixes update-time terminal loss) (stablyai#7421)" This reverts commit 24d599f.
Problem
After an app update, creating a new terminal tab fails with:
Existing terminals keep streaming output but new spawns can't find
conpty.dll.Root cause
The node-pty native runtime (
conpty.node/conpty.dll/OpenConsole.exe) is relocated to a per-app-version dir underuserData/node-pty-runtime/<version>/(#7421) so live terminals survive the NSIS updater's kill-zone. Daemons deliberately survive app updates and are keyed byPROTOCOL_VERSION, so an old-version daemon gets adopted by the new app and keeps loadingconpty.dllfrom its own version dir on every spawn.On every launch,
removeStaleRuntimeVersions()deleted every other version's runtime dir, gated on arenameSyncsucceeding — the assumption being "rename succeeds ⇒ nothing open ⇒ safe to delete."That assumption is false on Windows. Running
.exeimages and mapped.dllfiles are opened withFILE_SHARE_DELETE, so both the directory rename and the recursive delete succeed while the adopted daemon is still using them. Already-open PTYs keep streaming via deferred-delete handles (why existing terminals seem alive), but the next spawn re-loadsconpty.dllfrom the now-unlinked version dir →GetFileAttributesWfails withERROR_PATH_NOT_FOUND(code 3, distinct from code 2ERROR_FILE_NOT_FOUNDwhich would mean a never-copied file — confirming whole-dir deletion).Fix
Gate cleanup on actual daemon liveness instead of a filesystem side-effect:
collectInUseRuntimeVersions(daemonRuntimeDir)reads thedaemon-v<N>.pidfiles underuserData/daemonand, for each live daemon (process.kill(pid, 0)+startTimeMatches), protects theappVersionit recorded — which is exactly the name of the runtime dir it pins.removeStaleRuntimeVersions()now skips the current version or any in-use version andrmSyncs the rest directly, dropping the rename dance entirely.Every failure mode now leaks a dir rather than deleting one in use — the safe direction (a leaked dir is reclaimed on a later launch once its daemon exits). Reuses
parseDaemonPidFile/startTimeMatchesfromdaemon-health.ts, so there's no new pid-parsing logic.Tests
Rewrites the misleading cleanup test (which modeled no live daemon and so was green while asserting the buggy behavior was correct) and adds coverage:
appVersion: null(pre-relocation / legacy bare-int pid) protects nothingLocal verification:
tsgo(node/cli/web) ✅,oxlint✅, relocation test file 17 passed (+9 vs baseline). The single remaining failure in the file is a pre-existing, environment-only issue —node_modules/node-ptyis unpatched in the dev checkout so the loader-override test can't exerciseORCA_NODE_PTY_NATIVE_DIR; confirmed identical on origin/main with this change stashed.Scope / platform notes
ORCA_FORCE_CONPTY_RELOCATION=1). The liveness probe is platform-neutral (startTimeMatchesshort-circuits where process start time is unavailable, e.g. win32).