Skip to content

fix(command-path): list PATH detection directories with readdir on Windows - #51158

Merged
nwparker merged 1 commit into
mainfrom
bugbash/issue-50875
Nov 27, 2026
Merged

nwparker merged 1 commit into
mainfrom
bugbash/issue-50875

Conversation

@nwparker

@nwparker nwparker commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

On Windows, Orca checks the folders on your PATH to find installed agent CLIs. For folders that don't exist, Windows' "open folder" call sometimes says it worked when it didn't. The next step then writes through an empty pointer and Orca crashes. For this check, Orca now uses a different "list folder" call that reports the error properly.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​57 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​57
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​8 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​4

What Changed

Why

Root cause, from disassembling stock electron-v45.4.1-win32-x64 at RVA 0x3CC70A7 (movq %rcx, 0x8(%rax) inside DirHandle::DirHandle):

  1. The runtime's Windows opendir frees a temporary buffer after the enumeration call fails and before it reads the OS last-error value.
  2. With the 45.x allocator, that free can reset last-error to 0.
  3. The failed open, for example a missing PATH entry, then comes back as success with a NULL dir.
  4. The Dir handle constructor writes its entry count (offset 8) through NULL, which is the write to 0x8.

readdir (scandir) reads the error right after the open call, so it reports the error correctly. The detection worker hits this often: it opens the cwd plus every PATH entry, and PATH entries that don't exist are common.

I chose to change only this one call site because it is the crashing thread named in the reports. An earlier version routed every Windows-reachable opendir through a shared helper with a lint rule. It was held because the change was too broad, so it was narrowed to this site.

Linked Issue

Refs #50875
Refs #50997
Refs #51113

This is a partial fix. It covers the crashing thread named in the reports. Other opendir/opendirSync call sites, and Node's internal fs.cp, keep main's behaviour and are a tracked follow-up. Filing the bug upstream with libuv/Electron is also still open.

Visual Proof

N/A. This fixes a native crash in a background worker thread. There is no UI or interaction change to capture.

Testing

  • Repro (Windows low-spec box, official Electron 45.4.1 runtime, run windowless as a main process): a stress script ran 6 worker threads, each calling opendir on 32 missing folders in a loop, while 6 other workers churned malloc.

    • opendir: crashed in 4 of 4 runs (about 65 s each). The exit code was C0000005, and the Windows Application log showed Faulting module electron.exe 45.4.1, Fault offset 0x0000000003cc70a7, the same offset users report.
    • readdir, same stress: no crash after 120 s, and every rejection was ENOENT.
  • Unit test: src/main/ipc/command-path/command-path-directory.test.ts fakes the platform as win32. It checks that the listed names match the opendir path and that opendir is never called. Forcing the opendir branch makes it fail.

  • vitest src/main/ipc/command-path/: 9/9 passed.

  • oxlint, oxfmt, the anti-slop and code-quality oxlint configs, ast-grep, and check-changed-code-quality.mjs: all clean on the touched files.

  • Platforms: Windows (repro and stress), macOS (unit tests and microbenchmark). Linux and SSH run the unchanged opendir path.

  • I manually tested these changes locally

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

  • Every added test fails without this change and avoids the test anti-patterns

Performance

  • Where it runs: only on the orca-command-path-directory worker thread, not the main thread or the renderer.

  • How often: only on demand (preflight, new-workspace agent detection, skill install, doctor, mobile requests), once per PATH entry, the same as on main. There is no polling, nothing runs per keystroke, and there is no per-tab fanout. No sync I/O or subprocesses were added.

  • Windows (low-spec box, 45.4.1):

    • A full 33-directory PATH scan, including System32 with 4.8k entries, takes 11.2 ms median, down from 22.3 ms.
    • System32 alone went from 0.7 ms to 3.8 ms, because readdir lists the whole folder before the cap applies.
  • macOS microbenchmark (real module with the platform toggled, median of 30 runs):

    Entries in one PATH dir opendir (main) readdir (this PR)
    20 2.83 ms 0.39 ms
    128 1.49 ms 0.50 ms
    5,000 1.86 ms 5.68 ms
    50,000 (adversarial) 4.71 ms 42.06 ms
    200 missing dirs 181 ms 185 ms
  • Trade-off: a single PATH directory with tens of thousands of entries now costs a full listing on Windows. That cost lands on the worker thread and delays one detection result. It does not block the UI. The typical case is faster.

Risk

Level: Low

  • Only Windows behaviour changes, and only in command-path detection.
  • Riskiest part: a very large PATH directory now gets a full listing on the worker. If something went wrong, the result would be slower or missed agent detection on Windows, not a crash.
  • Non-Windows platforms, SSH, mobile, and older remotes are unaffected.
  • Back out: a plain revert.

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.

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)
  • I stated the risk: what could break, for whom, and how to back it out
  • 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)

Reported by @HlONGlin

…ndows

On Windows, opendir can report a failed open (for example a missing
PATH entry) as success with no directory handle, and the handle
constructor then writes through null (Orca.exe+0x3CC70A7, WRITE 0x8)
on the orca-command-path-directory worker. readdir reads the error
right after the open call, so it reports ENOENT/ENOTDIR correctly.

readDetectionDirectory now uses readdir(withFileTypes) on Windows and
keeps opendir elsewhere. The shared entry loop still returns null at
the 129th entry, so the 128-entry cap is unchanged. Other opendir call
sites keep main's behaviour and remain a tracked follow-up.

Refs #50875
@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

readDetectionDirectory now uses readdir with file types on Windows and retains opendir on other platforms. A test compares the directory results across platforms and checks that the Windows path does not call opendir.

Merge Risk: Low · up to 0b9a9

On Windows, command-path detection now lists a directory fully before applying its entry cap. This avoids a reported worker crash, but PATH directories with very many entries will be slower to scan. The slowdown is off the UI thread, so merge risk is low, though it conflicts with the repository's zero-performance-regression rule and should be accepted explicitly.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: on Windows, command-path detection lists PATH directories with readdir instead of opendir. It also names the affected module and the fix type. The changeset matches th…
Description check Passed The description follows the repository template. It covers ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, the upstream boundary checkbox,…
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.
Full details: Title check

Explanation

The title names the main change: on Windows, command-path detection lists PATH directories with readdir instead of opendir. It also names the affected module and the fix type. The changeset matches this, since readDetectionDirectory switches to readdir on Windows and a test is added.

Full details: Description check

Explanation

The description follows the repository template. It covers ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, the upstream boundary checkbox, and the checklist. It gives concrete before/after behaviour, root cause, reproduction steps, unit test details, and benchmark tables. Minor gaps: the template's AI Disclosure and Review sections are not filled in, and the Linked Issue section uses 'Refs' lines while the template asks for 'Fixes #' with an actual issue. The Notes section also has no explicit cross-platform confirmation beyond the testing bullets.

  • 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

Recent review info

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: de9f7cb1-397b-4d8f-9ed5-1d4ea4465cde
Commits

Reviewing files that changed from the base of the PR and between 20f3a16 and 0b9a9f6.

Files selected for processing: 2

  • src/main/ipc/command-path/command-path-directory.test.ts
  • src/main/ipc/command-path/command-path-directory.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@nwparker
nwparker merged commit d4ed6c2 into main Nov 27, 2026
54 checks passed
@nwparker
nwparker deleted the bugbash/issue-50875 branch November 27, 2026 21:57
brennanb2025 added a commit that referenced this pull request Nov 28, 2026
* fix(fs): open folders with readdir on Windows everywhere in main and shared

On Windows, opendir can report a failed open (a missing folder, for
example) as success with no handle, and building the handle then writes
through null and crashes the process (#50875, #50997). The PATH scanner
already avoids it (#51158); every other main/shared caller still used
opendir or opendirSync.

openDirectory/openDirectorySync in src/shared/open-directory.ts return
the real Dir on macOS and Linux and a readdir-backed handle on Windows.
All callers go through it, and a test fails on any new direct opendir in
src/main or src/shared.

* docs(fs): note Windows lists the folder in full in the worktree removal check

* chore(ratchets): carry the moved opendirSync allowances to open-directory

* fix(fs): drop the Windows listing on close; test refuses opendir on Windows

* test(fs): type the opendir guards without casts

* fix(fs): copy folder trees with readdir on Windows instead of fs.cp's opendir walk

fs.cp opens every source folder with opendir, so a folder that can be
stat'd but not listed, or one removed mid-copy, can hit the same Windows
crash. copyPath (src/shared/copy-path.ts) keeps fs.cp on macOS/Linux and
for files and links; on Windows it walks folders with readdir. The ban
test now also rejects a direct fs.cp in src/main and src/shared.

* fix(fs): stop a local name shadowing copyPath; match fs.cp's default force

* fix(fs): copy files inline in the Windows tree walk and keep fs.cp's into-self checks

Per-file fs.cp repeated a stat of every destination ancestor; files are
now copied the way fs.cp's onFile does (overwrite unless force is false,
mode and optional timestamps). Links still go to fs.cp, now with
recursive false so they can never re-enter its opendir walk. The root
check now also walks destination ancestors by identity, catching a
destination reached through a junction into the source.

* fix(daemon): copy filtered Windows daemon-host folders with a readdir walk

cpSync with a filter walks folders with opendirSync, which goes through
the same Windows open that can report a failure as success and crash.
On Windows only, the filtered folder copies now use a small sync walk
(readdirSync, mkdirSync, copyFileSync) with the same filter and
dereferenced links; executeManifest keeps its shape. #51222 replaces
this walk with its async walker.

* test(fs): read the opendir baseline from the real module so the test also runs on Windows

* test(fs): capture the real opendir in the mock; carry the daemon staging walk's sync allowance

* fix(fs): yield to the event loop every 32 entries of a Windows listing, as Dir's buffered reads do
0xff26b9a8 pushed a commit to 0xff26b9a8/orca that referenced this pull request Nov 28, 2026
* fix(fs): open folders with readdir on Windows everywhere in main and shared

On Windows, opendir can report a failed open (a missing folder, for
example) as success with no handle, and building the handle then writes
through null and crashes the process (stablyai#50875, stablyai#50997). The PATH scanner
already avoids it (stablyai#51158); every other main/shared caller still used
opendir or opendirSync.

openDirectory/openDirectorySync in src/shared/open-directory.ts return
the real Dir on macOS and Linux and a readdir-backed handle on Windows.
All callers go through it, and a test fails on any new direct opendir in
src/main or src/shared.

* docs(fs): note Windows lists the folder in full in the worktree removal check

* chore(ratchets): carry the moved opendirSync allowances to open-directory

* fix(fs): drop the Windows listing on close; test refuses opendir on Windows

* test(fs): type the opendir guards without casts

* fix(fs): copy folder trees with readdir on Windows instead of fs.cp's opendir walk

fs.cp opens every source folder with opendir, so a folder that can be
stat'd but not listed, or one removed mid-copy, can hit the same Windows
crash. copyPath (src/shared/copy-path.ts) keeps fs.cp on macOS/Linux and
for files and links; on Windows it walks folders with readdir. The ban
test now also rejects a direct fs.cp in src/main and src/shared.

* fix(fs): stop a local name shadowing copyPath; match fs.cp's default force

* fix(fs): copy files inline in the Windows tree walk and keep fs.cp's into-self checks

Per-file fs.cp repeated a stat of every destination ancestor; files are
now copied the way fs.cp's onFile does (overwrite unless force is false,
mode and optional timestamps). Links still go to fs.cp, now with
recursive false so they can never re-enter its opendir walk. The root
check now also walks destination ancestors by identity, catching a
destination reached through a junction into the source.

* fix(daemon): copy filtered Windows daemon-host folders with a readdir walk

cpSync with a filter walks folders with opendirSync, which goes through
the same Windows open that can report a failure as success and crash.
On Windows only, the filtered folder copies now use a small sync walk
(readdirSync, mkdirSync, copyFileSync) with the same filter and
dereferenced links; executeManifest keeps its shape. stablyai#51222 replaces
this walk with its async walker.

* test(fs): read the opendir baseline from the real module so the test also runs on Windows

* test(fs): capture the real opendir in the mock; carry the daemon staging walk's sync allowance

* fix(fs): yield to the event loop every 32 entries of a Windows listing, as Dir's buffered reads do

(cherry picked from commit b807f1f)
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