Skip to content

fix: recover events macOS drops when a watcher starts or stops - #354

Merged
alexander-akait merged 6 commits into
mainfrom
fix/macos-lost-events
Oct 15, 2026
Merged

alexander-akait merged 6 commits into
mainfrom
fix/macos-lost-events

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Draft: investigation first, fix to follow in this PR.

webpack's macOS CI loses a file deletion between watch builds (webpack/webpack#22681, restored-commonjs-reexport): no build ever reports it, and the next build reuses the stale module. On Linux the same failure appears only when the deletion event is dropped, never when it is late.

Suspected cause: on macOS, libuv (src/unix/fsevents.c) shares one FSEventStream between every directory fs.watch handle in the process. Adding or removing any handle destroys the stream and creates a new one from kFSEventStreamEventIdSinceNow, so events not yet delivered are lost. watchpack adds and removes handles whenever the set of watched directories changes.

This commit adds a temporary fsevents-probe job (macOS, Node 20/22/24) that deletes a watched file, starts another watcher 0–100 ms later, and counts lost deletions, with plain fs.watch and with watchpack. It will be removed once the numbers are in.

🤖 Generated with Claude Code

https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Directory monitoring now rescans watched folders after filesystem event streams restart on macOS. Changes and removed files can be detected even when events are missed during a restart.
    • Rescanning can also be enabled on other platforms through a configuration setting. The scan runs after a short delay and skips watchers that have been closed or are still completing their initial scan.

@changeset-bot

changeset-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 71146c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
watchpack Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codspeed

codspeed Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 36 untouched benchmarks


Comparing fix/macos-lost-events (71146c4) with main (1c00baa)

Open in CodSpeed

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.56%. Comparing base (d5ab9bf) to head (71146c4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #354      +/-   ##
==========================================
+ Coverage   92.45%   92.56%   +0.11%     
==========================================
  Files           8        8              
  Lines        1338     1359      +21     
  Branches      396      402       +6     
==========================================
+ Hits         1237     1258      +21     
  Misses         95       95              
  Partials        6        6              
Flag Coverage Δ
integration 92.56% <100.00%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

libuv shares one FSEventStream between all directory watchers of a process
and recreates it from "now" whenever one starts or stops, so events it had
not delivered yet are lost. Every directory watcher now rescans once that
settles, which reports the lost removals and changes.
Polling reports the removal before the second watcher starts; the handler
then threw on the missing watcher, and the one started after it kept jest
alive.
@alexander-akait
alexander-akait marked this pull request as ready for review October 15, 2026 23:22
@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

The event source now schedules rescan events after watcher starts or stops. Rescanning is enabled by default on macOS and can be enabled on other platforms with an environment setting. DirectoryWatcher responds to eligible rescan events with a non-initial scan. The change also adds event type declarations, a test for reporting a removed file, and a patch changeset.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 71146

The change adds rescanning after macOS watcher restarts to recover dropped events. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: recovering filesystem events that macOS may drop when a watcher starts or stops.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
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.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments: 1
lib/watchEventSource.js: 1

64-64: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Isolate listener errors in the rescan loop.

w.emit("rescan") runs inside a setTimeout callback. A throwing rescan listener propagates out of the timer callback. Node then raises an uncaught exception. The loop also stops, so the remaining watchers get no rescan. The current listener in DirectoryWatcher calls doScan, which defers its work with process.nextTick, so the risk is low today. A try/catch per watcher would make the loop robust if listeners change.

Proposed fix
-		for (const w of activeWatchers) w.emit("rescan");
+		for (const w of [...activeWatchers]) {
+			try {
+				w.emit("rescan");
+			} catch (err) {
+				process.nextTick(() => {
+					throw err;
+				});
+			}
+		}

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: be95a562-c1ce-48ba-8cf7-8009e63e4f1d
📥 Commits

Reviewing files that changed from the base of the PR and between d5ab9bf and 71146c4.

📒 Files selected for processing: 5
  • .changeset/macos-lost-events.md
  • lib/DirectoryWatcher.js
  • lib/watchEventSource.js
  • test/RestartRescan.test.js
  • types/watchEventSource.d.ts

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

@alexander-akait
alexander-akait merged commit 26f591c into main Oct 15, 2026
51 checks passed
@alexander-akait
alexander-akait deleted the fix/macos-lost-events branch October 15, 2026 23:35
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