Skip to content

Fix a Windows crash when a folder can't be opened - #51332

Merged
brennanb2025 merged 12 commits into
mainfrom
brennanb2025/win-opendir-readdir
Nov 28, 2026
Merged

brennanb2025 merged 12 commits into
mainfrom
brennanb2025/win-opendir-readdir

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

On Windows, one way of opening a folder sometimes says "opened fine" when the folder is actually missing or unreadable, and the app crashes on the next step. The PATH scanner already stopped using it. This PR stops every other place in the app from using it on Windows too.

Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​252 $\color{#cf222e}{\Huge{\mathbf{−}}}$​57 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​195
Prod 33 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​359 $\color{#cf222e}{\Huge{\mathbf{−}}}$​80 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​279

What Changed

  • New src/shared/open-directory.ts with openDirectory and openDirectorySync. On macOS and Linux they return Node's real opendir handle, so nothing changes there. On Windows they list the folder with readdir and return a small handle with the same read, readSync, close, closeSync and for await behavior.
  • Every opendir/opendirSync call in src/main and src/shared (19 files, including the PATH scanner from fix(command-path): list PATH detection directories with readdir on Windows #51158) now goes through it.
  • New src/shared/copy-path.ts with copyPath. Node's fs.cp opens every source folder with opendir while it walks a tree, so a folder that can be stat'd but not listed, or one removed mid-copy, hits the same crash. On macOS and Linux, and for single files and links everywhere, copyPath is plain fs.cp. On Windows it walks folders with readdir and keeps fs.cp's filter order, merge behavior and error codes. Every recursive fs.cp in src/main goes through it: plugin install, browser extension load, worktree linked-path copy, skill bundle, package and placement copies.
  • src/shared/open-directory.test.ts checks the Windows path lists the same entries as opendir, reports ENOENT/ENOTDIR for missing or non-folder paths, and fails if new code in src/main or src/shared calls opendir or fs.cp directly. src/shared/copy-path.test.ts checks that the Windows walk copies the same tree as fs.cp, with the same errors. Two scripts that run in child processes are allowed by name.
  • Ratchet baselines: the two opendirSync allowances from history-reader.ts and agent-hook-endpoint-temp-cleanup.ts move to open-directory.ts (same total). Those two callers now each count one more blocking-helper reference, with a reason note; this needs reviewer sign-off.

Why

The crash in #50997/#50875 (0xC0000005, write to 0x8 at Orca.exe+0x3cc70a7) comes from the runtime's Windows folder-open: when the open fails, the error code can be lost after a memory free, so the failure comes back as success with no handle, and building the handle writes through null. It first appeared in 1.4.280, the first release on Electron 45 (#43018); no opendir callers changed between 1.4.279 and 1.4.280. readdir takes a different path in the runtime that reads the error right after the open, so it reports the failure. #51158 fixed only the PATH scanner thread; the other callers (codex session listing, workspace space scan, worktree removal check, theme scan, skill staging, temp staging cleanup, provider journal, terminal history restore and others) could hit the same crash on a missing or unreadable folder.

One helper rather than per-caller branches keeps the Windows choice in one place, and the test keeps new callers from reintroducing it.

Linked Issue

Fixes #50997
Fixes #50875

Linked Issue: STA-19415

Visual Proof

N/A: no UI change; this removes a crash.

Testing

  • src/shared/open-directory.test.ts (Windows path forced on macOS/Linux CI, plus the source ban).

  • pnpm check:changed and CI.

  • Windows QA on a packaged build: PATH with missing folders, an unreadable folder, and the normal flows of the changed callers.

  • 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

macOS/Linux: no change (same opendir handle). Windows: each folder is listed in one call instead of streamed, so entry caps stop early iteration but not the listing itself. Measured with Node 24: 200,000 entries cost about 340 ms of background I/O, one 32 ms main-thread gap and 18 MB held until the listing is dropped. Folders these callers read are normally small (session day folders, pack folders, staging roots, skill folders); the largest is the one-shot legacy clipboard sweep of the OS temp folder 30 s after launch, which pays that cost once per launch on a very large temp folder. No new timers, polls or wakeups.

Risk

Level: Low. macOS and Linux keep the same handle. On Windows the riskiest part is handle behavior: a second close no longer throws and read after close returns null; every caller already tolerates both. A plain revert restores the previous behavior (and the crash).

AI Disclosure

Review

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

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Other places that open folders, checked:

  • cpSync copies folders in native code (no opendir) unless given a filter. The only filtered call is the Windows daemon staging copy in daemon-host-manifest.ts; Stop the app freezing while Windows stages the terminal daemon runtime #51222 replaces it with a readdir walk plus copyFile, so it is left to that PR. Until then its source folders are install folders checked with existsSync just before the copy.
  • orcad-windows-host-state-ops.ts calls fs.promises.cp inside script text that runs in the remote host's own Node process, not in the app.
  • Production dependencies: of the packages whose code calls opendir, fs.cp or cpSync, only ssh2 runs in the main process, and its opendir is the remote SFTP request, not a local folder open. The rest (app-builder-lib, electron-builder-squirrel-windows, @electron/get, @electron/osx-sign, playwright-core, tsx) are build or dev tools not loaded by the app.

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)

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

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds shared asynchronous and synchronous directory-opening helpers. On Windows, the helpers read directory entries into a listed handle; on other platforms, they use Node directory handles. Directory scans across the main and shared code now use the helpers. The change also adds a shared path-copy helper and migrates copy operations to use it. Tests cover both helpers and the Windows filtered manifest copy. Filesystem ratchet baselines are updated.

Merge Risk: Moderate · up to 9c176

This change routes Windows folder listings through a full-listing read to avoid the folder-open crash. Very wide folders can briefly use more memory, and the endpoint cleanup can briefly block while listing them. Results stay the same, but this is a performance cost on Windows, and the author reports no Windows-host validation. Confirm the cost is acceptable before merging.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Linked issues #50997 and #50875 are open and direct, so both apply. #50997 expects the Windows main process to survive a failed allocation or spawn failure, or to write an error span. The PR does not … Provide evidence that the linked crash fault is the Windows opendir path. Options: a symbolized stack from the Crashpad dump for #50997 that shows the opendir/readdir call, or a Windows QA run on a packaged build with missing and unreadable…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title "Fix a Windows crash when a folder can't be opened" is specific and matches the main change. The PR adds shared openDirectory helpers that route Windows folder opens around the failing opend…
Description check Passed The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #50997 and #50875), Visual Proof (N/A with reason), Testing with checked boxes, Performance…
Out of Scope Changes check Passed The changed production files all route directory listing or recursive copy through the two new helpers, or update the ratchet baselines that track those calls. The PATH scanner from #51158 and the oth…
Full details: Title check

Explanation

The title "Fix a Windows crash when a folder can't be opened" is specific and matches the main change. The PR adds shared openDirectory helpers that route Windows folder opens around the failing opendir path, and it targets crashes (#50997, #50875) tied to missing or unreadable folders. It is concise and does not list files. The title does not mention the copyPath helper, which is also a significant part of the change, but it does not need to cover every detail.

Full details: Description check

Explanation

The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #50997 and #50875), Visual Proof (N/A with reason), Testing with checked boxes, Performance with a concrete measurement and reasoning, Risk with a level and revert path, and the Notes and Checklist sections. The main gaps are the empty AI Disclosure section and the empty Risk level justification is present but the template's 'Review' heading is left empty. The Testing section also claims Windows QA steps that the objectives say are not reported as results. These gaps are minor and do not make the description largely incomplete.

Resolution

Consider filling in the empty AI Disclosure section or stating that it does not apply. Consider stating the Windows QA results, or noting that Windows QA is still pending, so reviewers can see what was verified.

Full details: Out of Scope Changes check

Explanation

The changed production files all route directory listing or recursive copy through the two new helpers, or update the ratchet baselines that track those calls. The PATH scanner from #51158 and the other opendir callers are the same folder-open path described in the PR. Every recursive fs.cp in src/main now goes through copyPath, and the PR says the same crash path reaches fs.cp through its opendir walk. The daemon-host-manifest.ts custom copier is a behavior change on the filtered Windows path. It sits in the same crash path and is disclosed in the PR body and the baseline changes. Its dereference and force handling differ from cpSync, which is a risk but not an unrelated change. The 32-entry setImmediate yield in the async handle supports the listing change and does not add new timers. The changed-file summaries show no unrelated feature work.

Full details: Linked Issues check

Explanation

Linked issues #50997 and #50875 are open and direct, so both apply. #50997 expects the Windows main process to survive a failed allocation or spawn failure, or to write an error span. The PR does not add that behavior. It routes folder opens in src/main and src/shared through openDirectory/openDirectorySync, which avoids the Windows opendir path. The PR attributes the Crashpad fault (0xC0000005, write to 0x8 in Orca.exe+0x3cc70a7) to that path, but the summary gives no reproduction or dump symbolization that confirms it. The added tests check directory listing parity, ENOENT/ENOTDIR errors, and a source ban on direct opendir/fs.cp calls. They do not test crash survival. #50875 expects a stylus click to keep Orca open. The issue states the cause is unconfirmed. No changed code touches stylus or pointer input. The PR body says 'Fixes #50875', but the only link is the unverified opendir hypothesis. Windows QA results are not reported. Whether the change meets either expected behavior cannot be established from the evidence.

Resolution

Provide evidence that the linked crash fault is the Windows opendir path. Options: a symbolized stack from the Crashpad dump for #50997 that shows the opendir/readdir call, or a Windows QA run on a packaged build with missing and unreadable folders that shows no access violation. For #50875, provide a Windows reproduction or dump analysis that ties the stylus click to a folder-open call. Otherwise, change the PR text so it does not claim to fix #50875, and state that #50997 is addressed only for the folder-open path.

  • 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: 847ea3f1-4298-41af-a0fb-62e6c09f792d
Commits

Reviewing files that changed from the base of the PR and between 54fbb3e and 9c17665.

Files selected for processing: 8

  • config/main-blocking-helper-baseline.txt
  • config/main-sync-fs-baseline.txt
  • src/main/daemon/executable-host/windows/daemon-host-manifest.test.ts
  • src/main/daemon/executable-host/windows/daemon-host-manifest.ts
  • src/shared/copy-path.test.ts
  • src/shared/copy-path.ts
  • src/shared/open-directory.test.ts
  • src/shared/open-directory.ts

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

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: de4fbcd3-b115-445a-bf20-2f5ea702b714
Commits

Reviewing files that changed from the base of the PR and between 730cf3c and 6822b07.

Files selected for processing: 25

  • config/main-blocking-helper-baseline.txt
  • config/main-sync-fs-baseline.txt
  • src/main/claude/command-catalog/claude-command-folder-scan.ts
  • src/main/codex/history/disk-usage/codex-history-disk-usage-worker-entry.ts
  • src/main/codex/history/rollouts/codex-session-file-listing.ts
  • src/main/daemon/history/restoration/history-reader.ts
  • src/main/daemon/permissions/directory-enumeration-probe.ts
  • src/main/filesystem/owned-temp-staging-root.ts
  • src/main/git/refs/maintenance/repo-pack-index-state.ts
  • src/main/git/worktrees/removal/worktree-at-risk-local-files.ts
  • src/main/hang-watchdog/native-hang-capture-store.ts
  • src/main/ipc/command-path/command-path-directory.test.ts
  • src/main/ipc/command-path/command-path-directory.ts
  • src/main/omp/omp-session-cwd-lookup.ts
  • src/main/provider-process/provider-ownership-journal.ts
  • src/main/skills/package-archive/skill-package-identity.ts
  • src/main/skills/runtime-upload/skill-upload-staging-ownership.ts
  • src/main/ssh/system-transport/file-transfer/system-ssh-file-transfer.ts
  • src/main/warp-themes/theme-file-scanner.ts
  • src/main/workspaces/space-analysis/workspace-space-local-scan.ts
  • src/shared/agent-hook-listener/providers/grok/grok-session-paths.ts
  • src/shared/agent-hook-listener/transport/agent-hook-endpoint-temp-cleanup.ts
  • src/shared/open-directory.test.ts
  • src/shared/open-directory.ts
  • src/shared/process-observation/darwin/darwin-terminal-names.ts
Files with no reviewable changes: 1

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

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

… 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.
…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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

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: 5e0fe039-7acf-493f-b9df-49ba7a9ea812
Commits

Reviewing files that changed from the base of the PR and between 6822b07 and 54fbb3e.

Files selected for processing: 12

  • src/main/browser/extensions/installation/browser-extension-load-directory.ts
  • src/main/ipc/worktrees/linked-paths/worktree-symlinks.ts
  • src/main/opencode/opencode-instructions-config-dir.ts
  • src/main/plugins/installation/plugin-install-staging.ts
  • src/main/skills/bundle-archive/skill-bundle-creation.ts
  • src/main/skills/package-archive/skill-package-creation.ts
  • src/main/skills/provider-placement/skill-placement-copy.ts
  • src/main/speech/model-cache-path.ts
  • src/shared/copy-path.test.ts
  • src/shared/copy-path.ts
  • src/shared/open-directory.test.ts
  • src/shared/open-directory.ts

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

Comment thread src/shared/copy-path.ts Resolved
Comment thread src/shared/copy-path.ts Resolved
Comment thread src/shared/open-directory.test.ts Resolved
… 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.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan: 9926087 (clean)

Verdict: clean. No P0 or P1. macOS and Linux run the same code as before (openDirectory calls opendir, and copyPath calls cp). On Windows, a listing that used to stop early now reads the whole folder first. I measured that cost and checked how often each caller runs. Every caller stays far below the stall and steady-load bars at the reference fleet. Paying this cost is how the PR fixes the crash.

What changes on Windows

  • openDirectory/openDirectorySync call readdir(withFileTypes) and keep the entries in memory. Callers that stop at a cap (1 entry, 64, 128, 1,000, 1,024 or 65,536) still stop early in JS, but the full listing has already been read.
  • copyPath with recursive walks folders one file at a time in JS, using about the same per-file calls as fs.cp: filter, stat of source and destination, copyFile, chmod.
  • executeManifest uses a sync readdirSync/statSync/copyFileSync walk for filtered folder ops. It replaces cpSync, which was also sync, and does the same amount of work.

Measurements (Node 26.5, macOS APFS, one folder, median of 2 runs)

entries readdir total / longest event-loop gap / heap opendir total / gap / heap opendir stopping at 64
10k 11–36 ms / 2.8 ms / 1.2 MB 21 ms / 3 ms / 1.7 MB 0.7 ms / 0.1 MB
100k 86–390 ms / 15–17 ms / 11.5 MB 104–128 ms / 2 ms / 2.3 MB 1 ms / 0.1 MB
200k 158–1041 ms / 27–33 ms / 23 MB 208–223 ms / 2 ms / 2.3 MB 0.6 ms / 0.1 MB
  • On the event loop, readdir costs about 0.15 ms per 1k entries plus about 115 B per entry kept in memory. It still does that when the caller stops at 64 entries.
  • A 2M-entry folder (10× the widest row) would stall about 300 ms. No caller below lists folders anywhere near that wide.
  • Copy test: 4,000 files in 100 folders, 4 runs each, run twice in alternating order. The Windows walk (forced on via process.platform) took 1.2–5.2 s and fs.cp took 2.0–5.5 s. The longest event-loop gap was under 60 ms for both. Per file they cost about the same (~0.6 ms per file on this disk), so the copy walk is no regression.
  • Not measured: a Windows host. The scandir thread-pool time there is untested, but the time spent on the event loop is mostly Dirent creation, which depends little on the OS.

Callers (Windows unless noted; work per trigger and its bound)

caller trigger and how often thread work per trigger on Windows grade
worktree-at-risk-local-files readDirectoryVerdict user removes a worktree (also batch removal) main lists each visited ignored folder in full; at most 1,000 entries visited overall; 8 s deadline typical ignored folders hold under 1k entries, so under 1 ms. A 100k-entry folder costs about 15 ms once per removal. No repeated trigger. clean
repo-pack-index-state probe pack maintenance, only after the pack folder's stamp changes main full listing of objects/pack (cap 65,536) a few thousand packs cost under 1 ms; 65k entries about 10 ms; stamp-gated. clean
workspace-space-local-scan user opens space analysis main, 48 at a time full listing per folder before the 100k-entry / 64 MB budget check the scan walks the whole tree anyway. The budget can now be passed by one folder before it triggers (at most 48 × folder width). One-shot, user-started. clean
codex-session-file-listing, omp lookup, grok session paths, claude command folder scan, skill package identity, warp theme scan, ssh upload plan history indexing, lookups, user imports and uploads main full listing; most callers iterate it all anyway dated or per-cwd folders hold tens to hundreds of entries. clean
codex disk-usage worker disk-usage request worker thread full walk either way off main. clean
history-reader listRestorable daemon cold restore daemon, sync iterates every entry either way and reads meta for each same work. clean
agent-hook endpoint temp sweep endpoint publication (hook server start) main, sync full readdirSync of the endpoint folder (cap 1,024) folder holds a few files; about 15 ms only if tampered to 100k. Rare trigger. clean
owned-temp-staging sweep, skill upload staging, native hang captures (64), command-path detection (128) startup, clipboard and staging sweeps main full listing of Orca-owned folders small folders that Orca sweeps. Command-path detection already used readdir on Windows before this PR. clean
directory-enumeration-probe TCC probes daemon / main lists only 1 entry reaches Windows from none of its callers: the session publish and daemonReadsFolder checks are darwin-only, inspectMacProcessCodeIdentity returns probe-failed off darwin, and folder-access reset is macOS TCC. not applicable
darwin-terminal-names process observation main unchanged (darwin) not applicable
copyPath: worktree linked-path copy, browser extension, plugin install, skill bundle / package / placement user actions such as worktree creation, install or upload main (async) serial walk at the same per-file cost as fs.cp (measured) clean
copyPath, non-recursive: opencode config, speech model cache one-shot main plain fs.cp unchanged
executeManifest filtered folder ops daemon host relocation, once per app version main, sync readdirSync/statSync/copyFileSync instead of cpSync same work. clean

Teardown: ListedDirectory.close()/closeSync() drop the listing, and the async iterator closes it in finally (on break as well). read() callers (hang store, enumeration probe) close it in finally. Nothing outlives its handle. An abandoned readdir (worktree-at-risk deadline) cannot be cancelled, but it finishes in a single scandir.

Regression classes

  • Sync fs on Electron main: matched. The sync sites stay the same in number: they moved into open-directory.ts and the ratchet baselines were updated to match. The triggers are rare (endpoint publish, cold restore, relocation). Disproved.
  • Expensive code past a loosened bound: matched. On Windows, early-stop caps no longer limit the listing itself. Priced in the table above: no repeated trigger at fleet scale, and the cost is needed to avoid the crash. Disproved.
  • Eager startup imports: only node:fs/node:path built-ins. No matching code.
  • New idle wakeups, polls, store or render changes: no matching code. Wakeups per second are unchanged (0 → 0) on main, renderer and daemon.

Six questions

  1. Unused work before a gate: on Windows the listing happens before the cap is checked. Priced above (about 0.15 ms per 1k entries). Small at the widths these callers see.
  2. Owners multiplying work: no new owners, listeners or caches.
  3. Which thread pays: Dirent creation runs on main or the daemon. Measured: 15 ms per 100k entries. No sync work added on a hot path.
  4. Growth: work per trigger now grows with folder width on Windows. No caller runs repeatedly over fleet-sized folders.
  5. Host differences: macOS and Linux are unchanged. Windows was not measured on a Windows host; the gap is noted above.
  6. Teardown: handles drop their listings when closed or when iteration breaks. A readdir cannot be cancelled mid-call, but it is a single bounded call.

Not covered

  • No measurements on a Windows host, so NTFS scandir time and Defender scanning are unmeasured.
  • Space analysis on a worktree with a 100k+ entry folder on Windows has not been run end to end.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan: 9926087..9c17665: clean

What changed: on Windows, the readdir-backed handle's read() and async iterator now wait for one setImmediate after every 32 entries. macOS and Linux still get Node's real Dir and are unaffected.

  • How many yields: ceil(N/32) per listing, plus at most one more when N is an exact multiple of 32. That is 3,125 for 100k entries. A plain Dir already does a real I/O round trip about every 32 entries, so this restores the same pacing rather than adding new work.
  • Cost (Node 26, 100k-entry in-memory listing, 3 runs): without yields, 13–17 ms with the event loop blocked the whole time. With yields, 55–73 ms of wall time, and the longest gap between loop turns was under 2 ms. That is about 15 µs per yield. On Windows the readdir that fills the listing costs far more than this.
  • Idle wakeups: none added (0/s before and after on main and the daemon). setImmediate runs only inside read() during an active iteration. There are no timers, and nothing is left scheduled after close() or a break.
  • Wide folders iterated often: none. The widest repeated walk is the Codex session listing (tens of thousands of transcripts in per-day folders). It already pauses with setTimeout every batch, so about 1.5k extra yields at fleet size add roughly 25 ms of wall time to a background walk and block nothing. At 10× fleet that is about 0.2 s, still non-blocking. The other callers (worktree removal, space scan, skills, themes, journals, SSH transfer) are user-triggered or one-shot. The /dev scan in darwin-terminal-names is macOS-only.
  • Sync callers: readSync()/openDirectorySync are unchanged.
  • Ratchet baselines: these only record the already-graded Windows daemon-host staging walk, which runs once per staging. No new runtime path.

Regression classes checked: no matching code for fixed-cadence polls, idle wakeups, hidden owners, or per-item subprocesses. Hot-path rescans and sync fs on main were checked and disproved (read() does O(1) work per entry and adds no sync fs).

@brennanb2025

brennanb2025 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Review status — head 9c17665

Problem. On Windows, Orca's main process crashed with an access violation (#50997, #50875). When a folder can't be opened (for example a missing folder on PATH), the runtime's folder-open can report success with no handle, and the next step writes through null. It first appeared in 1.4.280, the first release on Electron 45. #51158 fixed only the PATH scanner.

Change. On Windows, every folder listing in the main and shared code now uses readdir through openDirectory/openDirectorySync. Recursive copies use copyPath, a readdir walk with the same semantics as fs.cp. The daemon staging copy uses a small sync readdir walk. macOS and Linux keep Node's opendir, fs.cp and cpSync. A test fails on any new direct opendir or fs.cp in src/main or src/shared.

Fixed during review

  • A local variable named copyPath shadowed the import in skill placement and broke that copy.
  • copyPath now treats a missing force as true, the same as fs.cp.
  • Files are copied inline in the Windows walk instead of one fs.cp per file, which repeated a stat of every destination ancestor for each file.
  • Copying into the source through a junction is now rejected, as fs.cp does.
  • Links are handed to fs.cp with recursive: false.
  • A closed Windows handle drops its listing.
  • Iterating a Windows listing yields to the event loop every 32 entries, matching Node's buffered reads.
  • The daemon staging cpSync with a filter, which walks with opendirSync, now uses a sync readdir walk on Windows. Stop the app freezing while Windows stages the terminal daemon runtime #51222 replaces this walk.
  • The test now also runs on a Windows host.

Accepted trade-off. On Windows a folder is listed in full before callers' entry and memory caps apply. Node has no other safe listing, because opendir is the call that crashes. Measured cost: about 115 bytes per entry, so 100k entries is 11.5 MB and one 15 ms gap.

  • Audit's concern: a folder with a million entries could use about 100–200 MB of memory before the scan or upload refuses it.
  • The other side: a second reviewer ruled it not a must-fix. The caps still limit what is kept, macOS and Linux are unchanged, and the alternative is the crash.

Waived by Brennan: "Accept, ship it". The regression audit on this head had no other finding, so it counts as safe with this waiver.

Deferred

  • If a worktree copy source contains a subfolder that can't be read, the copy fails partway with EPERM. The error is logged but not shown in the UI. Worktree creation already handled copy errors this way.
  • Node's cpSync without a filter copies in native code and doesn't use opendir, so it is left as is.

Verified

  • CI green on 9c17665.
  • pnpm check:changed: 42 of 42 checks pass.
  • Claude review: clean on 9c17665.
  • Regression audit on 9c17665, merged onto main: its only finding is the trade-off above, which Brennan accepted. It found nothing else across callers, platforms, SSH, restart and quit, and upgrade.
  • Perf scan on 9c17665: clean. There are no new idle wakeups; costs are in the PR comments above.
  • Windows QA on a packaged build:
    • PATH with 12 missing folders and one folder that can't be read: 11 agents were still detected at start, after idle, after 19 minutes and after a restart. Tested on 6822b07.
    • A disk-space scan and worktree removal with a folder that can't be read: both worked. Tested on 6822b07.
    • Codex history listing and a real Claude agent: both worked. Tested on 6822b07.
    • Copy flows on fab5e2b. A later commit changes only the listing yields and tests.
      • Worktree linked-path copy with nested files: passed.
      • A missing source folder is skipped.
      • A source with a subfolder that can't be read: no crash.
      • Load an unpacked extension: passed.
      • Skill share package: passed.
      • Skill placement into 11 agents: passed.
    • Daemon staging on a fresh profile: 2,026 files staged, and the terminal daemon ran from the staged folder.
    • The app kept the same process for the whole run, with no crash reports.
  • QA of the OMP and opencode agents: "omp and opencode QA waived by Brennan". For OMP, the only change is that its session lookup lists folders through the same helper QA exercised above. For opencode, the only change is a single-file copy, which is still plain fs.cp on every platform.

Load unpacked extension
Skill share package
Skill placement into agents

@brennanb2025
brennanb2025 merged commit b807f1f into main Nov 28, 2026
69 checks passed
brennanb2025 added a commit that referenced this pull request Nov 28, 2026
… temporary sync walk with the async tree walk
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

1 participant