Skip to content

Relocate node-pty ConPTY runtime outside the Windows install dir (fixes update-time terminal loss) - #7420

Closed
Jinwoo-H wants to merge 1 commit into
orca-performancefrom
Jinwoo-H/relocate-conpty-outside-install-dir
Closed

Jinwoo-H wants to merge 1 commit into
orca-performancefrom
Jinwoo-H/relocate-conpty-outside-install-dir

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Windows terminal sessions die on every app update since June 30 because the NSIS update installer force-closes every process running from the install directory — and #6968 moved the ConPTY console hosts into it. This PR relocates node-pty's native runtime (conpty.node, conpty.dll, OpenConsole.exe, winpty binaries) to userData/node-pty-runtime/<version>/, taking terminal infrastructure out of the installer's kill zone. Verified end-to-end against the installer's verbatim kill commands.

Root cause (reproduction-harness verified, not inferred)

electron-builder's NSIS template (allowOnlyOneInstallerInstance.nsh, unchanged in this repo since the initial commit) runs CHECK_APP_RUNNING during every update install:

# FIND: is anything running from the install dir?
if ((Get-CimInstance Win32_Process | ? {$_.Path -and $_.Path.StartsWith('$INSTDIR', 'CurrentCultureIgnoreCase')}).Count -gt 0) { exit 0 } else { exit 1 }
# KILL: Stop-Process each match, then a -Force retry loop

Three facts, each demonstrated by running these commands verbatim against disposable process topologies (harness output in the PR comments):

  1. A latent single-match blind spot protected the daemon until June 30. In Windows PowerShell 5.1, a pipeline that matches exactly ONE process returns a bare CimInstance whose .Count is $null, so -gt 0 is false and the installer concludes "nothing running" and kills nothing. Pre-Fix Windows bundled ConPTY packaging #6968, terminals used the System32 console host, so the detached daemon was the only install-dir process → single match → spared on every update. Same-process daemon survival across updates was real — and an accident.
  2. Fix Windows bundled ConPTY packaging #6968 (June 30) unmasked the sweep. With OpenConsole.exe instances now under resources/, there are 2+ matches, .Count works, and the sweep kills the daemon and every console host mid-install. Killing a console host kills its attached shell (reproduced; matches the GetConsoleScreenBufferInfo console-loss crashes Windows Error Reporting captured during the July 4 and July 5 update windows on the incident machine — and at no June update).
  3. Relocated topology survives. With the native runtime outside the install dir, OpenConsole.exe spawns from userData, FIND reports nothing (the daemon is again the sole install-dir process, back under the single-match umbrella), and daemon + shell + console host all survive the sweep.

This also corrects the earlier working theory: the update-relaunch wedge-kill path fixed in #7340 is real and reproducible, but the installer sweep — not that path — is what killed sessions in the July incidents.

Fix

  • node-pty patch (config/patches/[email protected]): loadNativeModule honors ORCA_NODE_PTY_NATIVE_DIR before its stock search paths, falling through on any failure. The native side already resolves conpty.dll (and spawns OpenConsole.exe) from conpty/ beside the loaded binding, so relocating the binding relocates the console hosts.
  • src/main/pty/node-pty-runtime-relocation.ts: at startup (win32 + packaged only), copy the binding dir (rebuilt build/Release, fallback platform prebuilds; .pdb excluded) to userData/node-pty-runtime/<appVersion>/ and set the env var. Crash-safe via a completion marker (partial copies redone). The forked daemon inherits the env, covering daemon-owned PTYs.
  • Stale version cleanup uses a rename probe: renaming a directory fails on Windows while anything inside is open, so only provably-unused version dirs are deleted — an adopted previous-version daemon keeps its runtime.
  • Fail-open everywhere: any error leaves the env unset and node-pty loads from the install dir exactly as today.

Verification

  • End-to-end sweep test (this machine): daemon-analog + patched node-pty package under a fake install dir, ORCA_NODE_PTY_NATIVE_DIR pointing at a relocated runtime; real ConPTY session with a live powershell.exe. OpenConsole.exe verified running from the relocated dir; the verbatim installer FIND/KILL sequence found nothing and daemon, shell, and console host all survived. Control legs: pre-fix (July) topology → all killed; pre-Fix Windows bundled ConPTY packaging #6968 (June) topology → all spared via the single-match blind spot.
  • Unit tests (node-pty-runtime-relocation.test.ts): source-dir resolution order, tree copy without symbols, marker idempotency, interrupted-copy recovery, stale-version cleanup keeping the current version, fail-open on missing source — plus a win32 integration test loading the real conpty binding through the patched loader from a relocated dir.
  • Typecheck clean; pty suites green (one pre-existing symlink-privilege failure in terminal-cwd-realpath.test.ts, untouched by this change and identical on the base branch).

Follow-ups (not in this PR)

  1. Daemon image relocation — the daemon binary is still INSTDIR\Orca.exe; today it survives via the single-match blind spot. Moving its image out of the install dir makes survival principled instead of accidental (and is the fix that remains correct if electron-builder ever repairs the .Count check upstream).
  2. Upstream report to electron-builder — the single-match .Count bug means the sweep silently skips exactly-one-process cases for every Electron app; conversely apps with detached helpers under the install dir lose them whenever there are 2+.
  3. The periodic resume-record capture from Fix Windows update-relaunch killing the live terminal daemon (session loss on update) #7340 remains the safety net for any path that still loses processes.

Root cause of update-time terminal loss (verified with a reproduction
harness running the installer sweep commands verbatim): the NSIS update
installer force-closes every process whose image path is under the
install directory. Since #6968 started bundling conpty.dll and
OpenConsole.exe under resources/, every live terminal's console host -
and with 2+ matches the daemon too - is killed mid-update. Before #6968
the daemon was the only install-dir process and survived only by luck:
the sweep's find step does (...).Count -gt 0, and in Windows PowerShell
5.1 a single-instance CIM result has null Count, so one match reads as
"nothing running".

Fix: copy the node-pty native runtime (conpty binding + conpty.dll +
OpenConsole.exe + winpty) to userData/node-pty-runtime/<version>/ at
startup and point the node-pty loader at it via ORCA_NODE_PTY_NATIVE_DIR
(loader override added to the existing node-pty patch; falls through to
the stock search paths on any failure). The daemon inherits the env var,
so console hosts for both main- and daemon-owned PTYs run outside the
installer kill zone. Stale version dirs are removed only when a rename
probe proves nothing inside is still running.

Verified end-to-end against the verbatim sweep: with the relocated
runtime, OpenConsole spawns from userData, the sweep finds nothing to
kill, and daemon + shell + console host all survive.
@Jinwoo-H

Jinwoo-H commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Reproduction harness evidence (installer sweep commands run verbatim against disposable topologies)

Topology legs (sweep-repro.mjs)

=== LEG JULY (conpty mode=dll, runtime INSIDE instdir) ===
[repro +5.5s] daemon-analog pid 55472 (image INSTDIR\Orca.exe), shell pid 35372, console host(s): OpenConsole.exe:27824
[repro +7.9s] sweep: FIND -> FOUND (exit 0)
[repro +10.9s] sweep outcome: cleared-after-graceful
[repro +12.4s] --- JULY RESULT ---
[repro +12.4s]     daemon-analog         (pid 55472): DEAD
[repro +12.4s]     shell (powershell)    (pid 35372): DEAD
[repro +12.4s]     console host OpenConsole.exe (pid 27824): DEAD
[repro +12.4s]     outside control       (pid 41588): ALIVE
=== LEG JUNE (conpty mode=system, runtime INSIDE instdir) ===
[repro +14.6s] daemon-analog pid 47516 (image INSTDIR\Orca.exe), shell pid 14776, console host(s): conhost.exe:21100
[repro +16.5s] sweep: FIND -> nothing (exit 1)
[repro +16.5s] sweep outcome: nothing-found
[repro +18.0s] --- JUNE RESULT ---
[repro +18.0s]     daemon-analog         (pid 47516): ALIVE
[repro +18.0s]     shell (powershell)    (pid 14776): ALIVE
[repro +18.0s]     console host conhost.exe (pid 21100): ALIVE
[repro +18.0s]     outside control       (pid 27124): ALIVE
=== LEG FIX (conpty mode=dll, runtime OUTSIDE instdir) ===
[repro +22.9s] daemon-analog pid 39732 (image runtime\Orca.exe), shell pid 25252, console host(s): OpenConsole.exe:13464
[repro +24.8s] sweep: FIND -> nothing (exit 1)
[repro +24.8s] sweep outcome: nothing-found
[repro +26.3s] --- FIX RESULT ---
[repro +26.3s]     daemon-analog         (pid 39732): ALIVE
[repro +26.3s]     shell (powershell)    (pid 25252): ALIVE
[repro +26.3s]     console host OpenConsole.exe (pid 13464): ALIVE
[repro +26.3s]     outside control       (pid 43388): ALIVE
=== LEG CONSOLE-LOSS (kill only OpenConsole; is the shell a collateral victim?) ===
[repro +31.3s] killing ONLY OpenConsole pid 48832 (daemon-analog and shell untouched)
[repro +31.4s] host 42944: SHELL EXITED ({"event":"shell-exit"})
[repro +34.4s] --- CONSOLE-LOSS RESULT ---
[repro +34.4s]     daemon-analog      (pid 42944): ALIVE
[repro +34.4s]     shell (powershell) (pid 1432): ALIVE
JULY|daemon-analog|DEAD
JULY|shell (powershell)|DEAD
JULY|console host OpenConsole.exe|DEAD
JULY|outside control|DEAD
JUNE|daemon-analog|DEAD
JUNE|shell (powershell)|ALIVE
JUNE|console host conhost.exe|ALIVE
JUNE|outside control|DEAD
FIX|daemon-analog|DEAD
FIX|shell (powershell)|ALIVE
FIX|console host OpenConsole.exe|ALIVE
FIX|outside control|DEAD

Single-match .Count blind spot (Windows PowerShell 5.1)

single-match .Count = [] -gt 0 -> False   # one process running from INSTDIR reads as nothing-running
two-match    .Count = [2] -gt 0 -> True

Post-fix end-to-end (fix-verify.mjs: patched loader + relocated runtime + verbatim sweep)

daemon-analog pid 44412 (image under INSTDIR), shell pid 32676
OpenConsole: 43592 | C:\Users\jinwo\AppData\Local\Temp\claude\C--Users-jinwo-orca-workspaces-orca-barramundi\9650c256-5c60-414b-a8d7-7c1bf6bc3f39\scratchpad\sweep-repro\legs\fix-verify\relocated-runtime\1.0.0\conpty\OpenConsole.exe
OpenConsole image under relocated runtime (NOT instdir): YES
sweep FIND -> nothing running (installer proceeds, kills NOTHING)

=== POST-SWEEP RESULT (post-fix topology) ===
daemon-analog: ALIVE
shell:         ALIVE
OpenConsole:   ALIVE
verdict: FIX VERIFIED — sessions survive the installer sweep

@Jinwoo-H

Jinwoo-H commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #7421 targeting main (same commit cherry-picked; the fix is general, not perf-specific). orca-performance picks it up on the next main sync before the RC cut.

@Jinwoo-H Jinwoo-H closed this Jul 5, 2026
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.

1 participant