Skip to content

fix(pty): keep runtime dirs a surviving daemon still uses - #7463

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-conpty-runtime-delete-in-use
Jul 6, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-conpty-runtime-delete-in-use

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Problem

After an app update, creating a new terminal tab fails with:

DaemonProtocolError: Daemon failed to spawn shell ... Cannot find conpty.dll
at C:\Users\...\node-pty-runtime\1.4.124-rc.1\conpty\conpty.dll, error code: 3

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 under userData/node-pty-runtime/<version>/ (#7421) so live terminals survive the NSIS updater's kill-zone. Daemons deliberately survive app updates and are keyed by PROTOCOL_VERSION, so an old-version daemon gets adopted by the new app and keeps loading conpty.dll from its own version dir on every spawn.

On every launch, removeStaleRuntimeVersions() deleted every other version's runtime dir, gated on a renameSync succeeding — the assumption being "rename succeeds ⇒ nothing open ⇒ safe to delete."

That assumption 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 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-loads conpty.dll from the now-unlinked version dir → GetFileAttributesW fails with ERROR_PATH_NOT_FOUND (code 3, distinct from code 2 ERROR_FILE_NOT_FOUND which 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 the daemon-v<N>.pid files under userData/daemon and, for each live daemon (process.kill(pid, 0) + startTimeMatches), protects the appVersion it recorded — which is exactly the name of the runtime dir it pins.
  • removeStaleRuntimeVersions() now skips the current version or any in-use version and rmSyncs 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/startTimeMatches from daemon-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:

  • surviving daemon protects its version dir; dead daemon's dir reclaimed
  • appVersion: null (pre-relocation / legacy bare-int pid) protects nothing
  • multiple protocol versions each protected; malformed and non-pid files ignored
  • default liveness probe against the current process

Local 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-pty is unpatched in the dev checkout so the loader-override test can't exercise ORCA_NODE_PTY_NATIVE_DIR; confirmed identical on origin/main with this change stashed.

Scope / platform notes

  • Behavior is unchanged off-Windows: relocation only runs on packaged win32 (or ORCA_FORCE_CONPTY_RELOCATION=1). The liveness probe is platform-neutral (startTimeMatches short-circuits where process start time is unavailable, e.g. win32).
  • Does not address the separate frozen-keyboard-input symptom seen on same-protocol updates; that has no legacy adapter and is still under investigation.

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.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7ceb25fc-058c-4b23-9de3-9437e66f3791

📥 Commits

Reviewing files that changed from the base of the PR and between f3f67a5 and 0a2ac16.

📒 Files selected for processing (2)
  • src/main/pty/node-pty-runtime-relocation.test.ts
  • src/main/pty/node-pty-runtime-relocation.ts

📝 Walkthrough

Walkthrough

This change adds daemon-liveness-aware protection for node-pty native runtime version directories on Windows. A new collectInUseRuntimeVersions function reads daemon pid files to determine which app-version directories are still pinned by live daemon processes. removeStaleRuntimeVersions is rewritten to delete only unpinned, non-current version directories, replacing the prior rename-based approach. ensureRelocatedNodePtyNativeRuntime gains daemonRuntimeDir and isDaemonPidAlive options, and installRelocatedNodePtyNativeRuntime is updated accordingly. Corresponding tests are added and updated.

Changes

Cohort / File(s) Summary
Runtime version tracking (src/main/pty/node-pty-runtime-relocation.ts) Adds collectInUseRuntimeVersions, rewrites stale-version deletion, extends ensureRelocatedNodePtyNativeRuntime options, updates installRelocatedNodePtyNativeRuntime wiring
Tests (src/main/pty/node-pty-runtime-relocation.test.ts) Adds daemon pid file helpers, new collectInUseRuntimeVersions test suite, updates existing relocation tests with daemonRuntimeDir and liveness scenarios

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
Loading

Related PRs: None specified.

Suggested labels: area/main-process, pty, windows

Suggested reviewers: None specified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description covers the problem, fix, and tests, but it does not follow the required template sections. Add the required Summary, Screenshots, Testing checklist, AI Review Report, Security Audit, and Notes sections, including the cross-platform review and screenshot status.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main fix: preserving runtime directories still used by surviving daemons.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@brennanb2025
brennanb2025 merged commit 3cd23a1 into main Jul 6, 2026
2 checks passed
Jinwoo-H added a commit that referenced this pull request Jul 6, 2026
) (#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.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
)

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]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants