Repository navigation
fix(command-path): list PATH detection directories with readdir on Windows - #51158
Conversation
…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
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
* 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
* 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)
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.
What Changed
Orca.exe+0x3CC70A7(write to0x8). Reporters on Windows: main process crashes with access violation (write to 0x8) in Orca.exe 1.4.280 #50997 and [Bug]: [Windows][v1.4.280] Repeated native crashes - Access Violation at Orca.exe+0x3CC70A7 #51113 found the crashing thread name in their dumps:orca-command-path-directory, the command-path detection worker.readDetectionDirectory(src/main/ipc/command-path/command-path-directory.ts) lists folders withreaddir(dir, { withFileTypes: true })instead ofopendir. Off Windows, theopendircall is the same as on main.nullat the 129th entry, so the 128-entry cap is the same.null.for awaitover theDirent[]behaves the same as one over theDirhandle.Why
Root cause, from disassembling stock electron-v45.4.1-win32-x64 at RVA
0x3CC70A7(movq %rcx, 0x8(%rax)insideDirHandle::DirHandle):opendirfrees a temporary buffer after the enumeration call fails and before it reads the OS last-error value.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
opendirthrough 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/opendirSynccall sites, and Node's internalfs.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
opendiron 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 wasC0000005, and the Windows Application log showedFaulting 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.tsfakes the platform as win32. It checks that the listed names match the opendir path and thatopendiris 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-directoryworker 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):
readdirlists the whole folder before the cap applies.macOS microbenchmark (real module with the platform toggled, median of 30 runs):
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
Agent skill upstream boundary
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)Reported by @HlONGlin