Skip to content

fix(windows): keep Orca's std handles out of every spawned child - #35678

Merged
nwparker merged 3 commits into
mainfrom
nwparker/windows-std-handles-noinherit
Oct 29, 2026
Merged

nwparker merged 3 commits into
mainfrom
nwparker/windows-std-handles-noinherit

Conversation

@nwparker

@nwparker nwparker commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​178 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​177
Prod 11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​83 $\color{#cf222e}{\Huge{\mathbf{−}}}$​8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75

ELI5

When Orca starts a helper program on Windows, Windows hands that helper copies of some of Orca's own connections. Orca's standard input, output and error were among them. A helper that runs for a long time (the program that keeps a WSL distro awake, or the terminal daemon) then keeps those connections open after Orca exits. Whatever launched Orca (a script, a test runner, an installer) keeps waiting for output that will never come. This change tells Windows, once and at the top of startup, not to hand those three connections to any helper.

What Changed

  • Before: On Windows, any long-lived child of Orca's main process held Orca's standard input, output and error. A launcher reading Orca's output could hang until that child exited, which can be hours later for the WSL holder or the daemon.
  • After: Those three handles are marked non-inheritable before Orca spawns anything, so children no longer receive them by accident. A child that is explicitly given Orca's output (stdio: 'inherit') still writes to it, because Node gives that child its own inheritable copy.

Mechanism:

  • Node launches every Windows child with "inherit all inheritable handles" switched on, and Orca's standard handles start out inheritable. stdio: 'ignore' only affects which handles the child uses as its own standard streams. It does not stop other inheritable handles from being copied.
  • A new native export, clearStdHandleInheritance() in native/windows-security, clears the inherit flag on the three standard handles. It skips handles that are absent or invalid. It never throws: it returns any handle Windows refused, with the error code.
  • src/main/startup/windows-std-handle-inheritance.ts calls it as the first statement in src/main/index.ts, before the uninstall preflights and anything else that can spawn. Every failure, including a missing or stale addon, is logged and startup continues. It does nothing off Windows.
  • Stale-binary guard: the native load probes in ensure-native-runtime.mjs (install and CI check-only) and rebuild-native-deps.mjs (the Electron probe) now call the new export. A .node built from older source still loads but lacks the export, so the probe fails and triggers a rebuild instead of passing. The CI native-build cache already keys on addon.cc, so x64 and arm64 release builds compile the new source.

Why

  • The alternative is to give each spawn an explicit list of the handles it may inherit (an attribute that is set per spawn). That fixes the same leak for every inheritable handle, not just these three, and it is planned as a follow-up in the process-host layer.
  • This change is smaller and covers every spawn site at once, including ones that don't go through process-host (Electron internals, third-party modules). It also needs no per-call-site wiring.
  • Clearing a flag on our own handles adds no new process launches, no scripting hosts and no runtime code generation, so it doesn't change Orca's EDR footprint.

Linked Issue

N/A (maintainer change; part of the WSL host idle-release work).

Visual Proof

N/A. There is no UI change: this only affects which handles Windows copies into child processes.

Testing

  • src/main/startup/windows-std-handle-inheritance.test.ts unit-tests the wiring with a mocked addon:

    • no-op off Windows;
    • calls the addon on Windows;
    • logs each refused handle;
    • logs and does not throw when the addon is unavailable or its call throws (a stale binary).
  • src/main/startup/windows-std-handle-inheritance.win32.test.ts runs on the Windows CI lane (registered in pr-package-test-paths.mjs) against the real addon, with an Electron main process as the child. That is Orca's real shape: Electron opts out of the std-handle protection that plain Node applies to itself at startup.

    • The child does three things: it optionally clears its handles, runs a grandchild with stdio: 'inherit' whose output must still arrive, and leaves a detached stdio: 'ignore' holder behind.
    • With the clear, the output pipe closes while the holder is still alive.
    • Without it (control), the pipe stays open until the holder exits.
    • A plain-Node child doesn't leak even without the clear. That arm pins why Orca's main process leaks and Node scripts don't.
    • The child runs hidden: no windows, ORCA_BACKGROUND_LAUNCH=1, and ELECTRON_RUN_AS_NODE removed for the Electron child and set only for its grandchildren.
  • Local (macOS): pnpm run typecheck:ci, the touched tests and pnpm run check:code-quality:changed pass. The native code compiles and runs only on the Windows CI lane.

  • The wrapper-hang check on a real Windows device is still pending.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Review

Reviewers: fix-reviewer, e9-reviewer (touches every spawn).

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Flag-off / User-risk: This is not behind a flag; it runs on every Windows launch. If the addon is missing or Windows refuses a handle, Orca logs a warning and behaves exactly as before. Remaining risk: a child that relied on implicitly receiving Orca's standard handles without being given them explicitly would lose them. Node's stdio: 'inherit' and 'pipe' both pass handles explicitly and are unaffected. Orca's main process is a GUI app whose standard handles are usually absent unless a launcher redirected them, and that redirected case is exactly the one this fixes.

  • Security: this narrows what children receive; it grants nothing new.
  • macOS/Linux: no-op.
  • SSH and remote: unaffected (this is local process setup only).
  • Mobile: unaffected.
  • Performance: one native call at startup, using the addon Orca already loads.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 369f1db3-c2e4-4925-98d2-b96a660aae33
Commits

Reviewing files that changed from the base of the PR and between 0e9ba21 and f7f9be6.

Files selected for processing: 15
  • config/scripts/ensure-native-runtime.mjs
  • config/scripts/ensure-native-runtime.test.mjs
  • config/scripts/pr-package-test-paths.mjs
  • config/scripts/rebuild-native-deps-test-fixtures.mjs
  • config/scripts/rebuild-native-deps.mjs
  • native/windows-security/index.d.ts
  • native/windows-security/index.js
  • native/windows-security/src/addon.cc
  • src/main/index.ts
  • src/main/startup/windows-install-dir-package-acl-grant.test.ts
  • src/main/startup/windows-std-handle-inheritance.test.ts
  • src/main/startup/windows-std-handle-inheritance.ts
  • src/main/startup/windows-std-handle-inheritance.win32.test.ts
  • src/shared/windows-security-addon.test-fixture.ts
  • src/shared/windows-security-addon.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the remaining finding affects warning accuracy, not whether handles are cleared.

Findings

  1. P2 Windows error can be overwritten ▶

Summary

This PR stops Windows children from accidentally keeping Orca's standard handles open.

  • Windows child processes stop keeping Orca's output pipe open after exit.

Reviews (3) · Last reviewed commit: "test(windows): say why the std-handle te..." · Reviewed by Greptile

Comment on lines +405 to +408
if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0)) {
auto failure = Napi::Object::New(env);
failure.Set("stream", stream.name);
failure.Set("code", static_cast<double>(GetLastError()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Windows error can be overwritten

After SetHandleInformation fails, this code creates a JavaScript object and sets a field before reading GetLastError(). Those calls can overwrite the thread's last error, so startup may log the wrong code and hide why the handle stayed inheritable. Save GetLastError() immediately after the failed call, then use that saved value.

Suggested change
if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0)) {
auto failure = Napi::Object::New(env);
failure.Set("stream", stream.name);
failure.Set("code", static_cast<double>(GetLastError()));
if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0)) {
const DWORD code = GetLastError();
auto failure = Napi::Object::New(env);
failure.Set("stream", stream.name);
failure.Set("code", static_cast<double>(code));

@nwparker
nwparker force-pushed the nwparker/windows-std-handles-noinherit branch from 4b3e875 to bcf8cf5 Compare October 29, 2026 05:45
libuv spawns with bInheritHandles=TRUE, so an inheritable std handle reached
long-lived children such as the WSL holder and the daemon and held a
launcher's pipe open after Orca exited. Clear HANDLE_FLAG_INHERIT on the std
handles at the top of main startup; stdio 'inherit' still works because
libuv hands the child its own inheritable duplicate.
…heritable

Plain Node clears its std handles' inherit flag at startup, so a Node child
never reproduced the leak. Electron main opts out of that, which is why Orca
leaks and why the control arm now runs Electron; a plain-Node arm pins the
difference.
@nwparker
nwparker force-pushed the nwparker/windows-std-handles-noinherit branch from bcf8cf5 to f7f9be6 Compare October 29, 2026 06:40
@nwparker
nwparker merged commit b6ffc20 into main Oct 29, 2026
69 checks passed
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