Skip to content

NativeWatcher: Emit events in the order fs.watch reported them - fix flaky integration tests - #1923

Open
robhogan wants to merge 1 commit into
react:mainfrom
robhogan:fix/native-watcher-preserves-order
Open

NativeWatcher: Emit events in the order fs.watch reported them - fix flaky integration tests#1923
robhogan wants to merge 1 commit into
react:mainfrom
robhogan:fix/native-watcher-preserves-order

Conversation

@robhogan

@robhogan robhogan commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Problem

Each raw fs.watch callback was dispatched fire-and-forget, and every handler awaits an lstat before deciding what to emit. Those calls complete in whatever order the libuv thread pool finishes them, so the order we emitted in could differ from the order the OS reported. Over a recursive delete of app/moved-in/file.js, the raw callback order was correct in 250/250 runs but emission diverged in 5/250 - most often delete app overtaking delete app/moved-in.

In particular, this failed integration tests, necessitating retries, eg 8 recent examples that failed on the first attempt and relied on retries: 3:

Fix

#handleEvent now resolves a raw event into the event to emit rather than emitting it directly, and the fs.watch callback appends the result to a promise chain. The lstat calls still start immediately and run concurrently - only emission is sequenced - so there is no throughput cost.

Also fixes a missing call in the constructor's isSupported guard, which made the check always truthy.

Impact

No user-facing bug is known to result from the old behaviour. NativeWatcher is the default backend on macOS when Watchman is unavailable, but each handler stats the path at handling time rather than trusting the event, so a reordered pair still tends to settle on the correct final state. And though watchers report directory events from the OS, we don't actually use them in metro-file-map - we prefer to synthesise directory events as files make directories (non-)empty.

Changelog: Internal

Test plan

250/250 emissions correctly ordered after the change, against 245/250 before. packages/metro-file-map/src/watchers/__tests__/integration-test.js passed 50/50 runs with this change alone and the tests unmodified, against a 5-10% baseline failure rate on macOS.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 10, 2026
**Problem**

Each raw `fs.watch` callback was dispatched fire-and-forget, and every handler awaits an `lstat` before deciding what to emit. Those calls complete in whatever order the libuv thread pool finishes them, so the order we emitted in could differ from the order the OS reported. Over a recursive delete of `app/moved-in/file.js`, the raw callback order was correct in 250/250 runs but emission diverged in 5/250 - most often `delete app` overtaking `delete app/moved-in`.

**Fix**

`#handleEvent` now resolves a raw event into the event to emit rather than emitting it directly, and the `fs.watch` callback appends the result to a promise chain. The `lstat` calls still start immediately and run concurrently - only emission is sequenced - so there is no throughput cost.

Also fixes a missing call in the constructor's `isSupported` guard, which made the check always truthy.

**Impact**

No user-facing bug is known to result from the old behaviour. `NativeWatcher` is the default backend on macOS when Watchman is unavailable, but each handler stats the path at handling time rather than trusting the event, so a reordered pair still tends to settle on the correct final state. The demonstrated effect is intermittent failure of the watcher integration tests, where a straggling deletion arriving after the one the test waited on desynchronised every later assertion.

Ordering is worth guaranteeing regardless - `WatcherBackend` consumers reasonably assume events on a path and its ancestors arrive as the OS reported them, and `recrawl` in particular asks the file map to re-crawl a subtree that later events may still refer to.

**Test plan**

250/250 emissions correctly ordered after the change, against 245/250 before. `packages/metro-file-map/src/watchers/__tests__/integration-test.js` passed 50/50 runs with this change alone and the tests unmodified, against a 5-10% baseline failure rate on macOS.
@robhogan
robhogan force-pushed the fix/native-watcher-preserves-order branch from ad16021 to 414c96e Compare September 10, 2026 11:00
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 10, 2026
@meta-codesync

meta-codesync Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

@GijsWeterings has imported this pull request. If you are a Meta employee, you can view this in D119493126.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant