Skip to content

Isolate macOS file watchers in child processes - #5527

Closed
AmethystLiang wants to merge 1 commit into
mainfrom
orca-quit-after-computer-wake-up
Closed

AmethystLiang wants to merge 1 commit into
mainfrom
orca-quit-after-computer-wake-up

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Run macOS file explorer and runtime file watchers in forked Node child processes so native @parcel/watcher/FSEvents crashes after sleep-wake do not take down Electron's main process.
  • Add watcher child/host restart, overflow refresh, unsubscribe, and teardown handling while keeping Linux on worker threads, Windows on existing native watch, and WSL on its in-distro watcher path.
  • Extract watcher event normalization/coalescing and update packaged app config so forked watcher entries and dynamic runtime deps are included/unpacked.

Testing

  • Added unit coverage for watcher event normalization, macOS process-isolated watcher routing, child-process restart/dispose behavior, Linux worker/direct-watch paths, and electron-builder packaging checks.

Made with Orca 🐋


Open in Stage

@parcel/watcher/FSEvents can produce a SIGTRAP after macOS sleep-wake
that previously killed Electron's main process. Both filesystem-watcher
and runtime-file-explorer now fork a disposable child process on macOS,
so a native addon crash tears down only the child and the host can
auto-restart it with an overflow event rather than going down entirely.

Co-authored-by: Orca <[email protected]>
@ghost

ghost commented Jun 16, 2026

Copy link
Copy Markdown

Ready to review this PR? Stage has broken it down into 5 individual chapters for you:

Title
1 Extract and test watcher event normalization
2 Implement macOS process-isolated file explorer watcher
3 Update runtime file watcher for subprocess support
4 Update integration tests for platform-specific routing
5 Configure build and packaging for forked entries
Open in Stage

Chapters generated by Stage for commit 2afa3a5 on Jun 16, 2026 9:18pm UTC.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This pull request introduces an out-of-process filesystem watcher for macOS. A new filesystem-watcher-child.ts child process script is added, which runs @parcel/watcher, debounces and batches raw events, and communicates with its host over Node IPC. A new filesystem-watcher-process-host.ts module spawns and manages this child, handling restart-on-crash and disposal. A shared filesystem-watcher-event-normalization.ts module is extracted for event coalescing and path normalization, consumed by both the child script and the existing filesystem-watcher.ts. The filesystem-watcher.ts and orca-runtime-files.ts are updated to route macOS through the out-of-process path. The existing file-watcher-worker.ts is refactored to support dual-mode IPC communication via a shared env var. Build configuration is updated to include the new child entrypoints as unpacked asar assets.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description includes a clear summary of changes, testing notes, and attribution, but is missing required sections from the template: Screenshots, detailed Testing checklist results, AI Review Report with cross-platform compatibility verification, Security Audit, and Notes on platform-specific behavior. Add the missing template sections: Screenshots (or "No visual change"), detailed testing results with checkmarks/crosses, AI Review Report confirming cross-platform compatibility checks, Security Audit summary covering IPC and path handling risks, and platform-specific behavior notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 19.05% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title "Isolate macOS file watchers in child processes" accurately and clearly summarizes the main objective of this PR - moving macOS file watchers to child processes to prevent native addon crashes from terminating the Electron main process.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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 and usage tips.

@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

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8e912e23-4e57-44c1-acf1-445c9665b11c

📥 Commits

Reviewing files that changed from the base of the PR and between 00951b9 and 2afa3a5.

📒 Files selected for processing (19)
  • config/electron-builder.config.cjs
  • config/packaged-runtime-node-modules.cjs
  • config/scripts/electron-builder-config.test.mjs
  • electron.vite.config.ts
  • src/main/ipc/filesystem-watcher-child.ts
  • src/main/ipc/filesystem-watcher-event-normalization.test.ts
  • src/main/ipc/filesystem-watcher-event-normalization.ts
  • src/main/ipc/filesystem-watcher-large-batch.test.ts
  • src/main/ipc/filesystem-watcher-local-unsubscribe.test.ts
  • src/main/ipc/filesystem-watcher-process-host.test.ts
  • src/main/ipc/filesystem-watcher-process-host.ts
  • src/main/ipc/filesystem-watcher-real.test.ts
  • src/main/ipc/filesystem-watcher.ts
  • src/main/runtime/file-watcher-host.test.ts
  • src/main/runtime/file-watcher-host.ts
  • src/main/runtime/file-watcher-worker.ts
  • src/main/runtime/orca-runtime-files-watch.test.ts
  • src/main/runtime/orca-runtime-files.test.ts
  • src/main/runtime/orca-runtime-files.ts

Comment on lines +240 to 242
if (!existsSync(filePath)) {
continue
}

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Fail fast if required unpacked runtime files are missing.

On Line 240, silently continue-ing lets packaging regressions ship undetected. If out/main/filesystem-watcher-child.js is absent, packaged macOS watcher forking will fail at runtime instead of failing the build.

Suggested fix
   const unpackedRoot = join(resourcesDir, 'app.asar.unpacked')
   for (const file of PACKAGED_UNPACKED_MAIN_RUNTIME_FILES) {
     const filePath = join(unpackedRoot, ...file.split('/'))
     if (!existsSync(filePath)) {
-      continue
+      throw new Error(`Packaged main file ${file} was not found in ${unpackedRoot}`)
     }
     addMissingPackagedRuntimeDeps(resourcesDir, readFileSync(filePath, 'utf8'), missing)
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!existsSync(filePath)) {
continue
}
const unpackedRoot = join(resourcesDir, 'app.asar.unpacked')
for (const file of PACKAGED_UNPACKED_MAIN_RUNTIME_FILES) {
const filePath = join(unpackedRoot, ...file.split('/'))
if (!existsSync(filePath)) {
throw new Error(`Packaged main file ${file} was not found in ${unpackedRoot}`)
}
addMissingPackagedRuntimeDeps(resourcesDir, readFileSync(filePath, 'utf8'), missing)
}

Comment on lines +113 to +119
function reportWatchError(err: unknown): void {
postToHost({
type: 'error',
message: err instanceof Error ? err.message : String(err)
})
reportOverflow()
}

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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add a short “why” comment for dual error+overflow emission.

Lines 113-119 intentionally emit both an IPC error and an overflow event, which is a non-obvious recovery contract. Please add a 1–2 line rationale inline so this behavior isn’t removed inadvertently.

As per coding guidelines, “When writing or modifying code driven by a design doc or non-obvious constraint, add a comment explaining why the code behaves the way it does.”

Source: Coding guidelines

Comment on lines +35 to +38
if (prev.type === 'create' && evt.type === 'delete') {
lastByPath.delete(p)
deleteBeforeCreate.delete(p)
continue

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Preserve terminal deletes for delete → create → delete sequences.

This branch currently drops the path entirely, which can erase an earlier delete marker and coalesce a terminal delete to nothing. That can leave stale entries visible until a later refresh.

Proposed fix
-      if (prev.type === 'create' && evt.type === 'delete') {
-        lastByPath.delete(p)
-        deleteBeforeCreate.delete(p)
-        continue
-      }
+      if (prev.type === 'create' && evt.type === 'delete') {
+        if (deleteBeforeCreate.has(p)) {
+          // Collapse delete->create->delete to a final delete.
+          deleteBeforeCreate.delete(p)
+          lastByPath.set(p, { type: 'delete', path: p, index: i })
+        } else {
+          // Plain create->delete cancels out.
+          lastByPath.delete(p)
+        }
+        continue
+      }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (prev.type === 'create' && evt.type === 'delete') {
lastByPath.delete(p)
deleteBeforeCreate.delete(p)
continue
if (prev.type === 'create' && evt.type === 'delete') {
if (deleteBeforeCreate.has(p)) {
// Collapse delete->create->delete to a final delete.
deleteBeforeCreate.delete(p)
lastByPath.set(p, { type: 'delete', path: p, index: i })
} else {
// Plain create->delete cancels out.
lastByPath.delete(p)
}
continue
}

@OrcaWin

OrcaWin commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closing as superseded by the process-isolated Parcel watcher implementation already on main (including the ownership refactors #33079 and #33401).

I checked both paths this PR addresses: desktop filesystem/watchers/filesystem-watcher-local-events.ts subscribes through subscribeViaWatcherProcess, and runtime files/watch/file-watches.ts / file-watcher-host.ts use subscribeViaRuntimeWatcherProcess. Both ultimately fork parcel-watcher-process-entry.js with ELECTRON_RUN_AS_NODE=1; native FSEvents crashes are already outside Electron/main. The supervisor resubscribes roots after child failure, reports interruptions/overflow, and owns bounded teardown. The child entry is already unpacked by the packaging configuration.

That implements the core crash-isolation and recovery goal here across the desktop and runtime paths. Adding the two separate macOS-only child hosts from this PR would duplicate and bypass the current shared supervisor/pool lifecycle. This closure is based on the implemented replacement, not the age or rebase conflicts.

Current implementation evidence:

@OrcaWin OrcaWin closed this Oct 26, 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.

2 participants