Skip to content

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

Merged
robhogan merged 1 commit into
react:mainfrom
robhogan:native-watcher-event-order
Sep 25, 2026
Merged

robhogan merged 1 commit into
react:mainfrom
robhogan:native-watcher-event-order

Conversation

@robhogan

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.

…flaky integration tests

## 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`:

- https://github.com/facebook/metro/actions/runs/33424298809/job/99606632037
- https://github.com/facebook/metro/actions/runs/33294203461/job/99215091027
- https://github.com/facebook/metro/actions/runs/33294203461/job/99216040984
- https://github.com/facebook/metro/actions/runs/33179954116/job/98882723792
- https://github.com/facebook/metro/actions/runs/33050438507/job/98453108534
- https://github.com/facebook/metro/actions/runs/33050438507/job/98444336335
- https://github.com/facebook/metro/actions/runs/32872930071/job/97884392723
- https://github.com/facebook/metro/actions/runs/32867305967/job/97865968129

## 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 25, 2026
@robhogan

Copy link
Copy Markdown
Collaborator Author

I've opened this as a new PR since #1923 seemed to have got lost in the depths of Phabricator. I just need a GitHub stamp now @cortinico / @vzaidman / @GijsWeterings .

@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 25, 2026
@robhogan
robhogan merged commit 88f95b0 into react:main Sep 25, 2026
15 checks passed
robhogan added a commit that referenced this pull request Oct 15, 2026
…flaky integration tests (#1974)

Adapted for 0.84.x: Resolved a conflict with the `Promise<void>` annotation added in 0.84.6.

(cherry picked from commit 88f95b0)
robhogan added a commit that referenced this pull request Oct 25, 2026
`NativeWatcher.startWatching` resolves as soon as `fs.watch` returns, but on macOS that can be before any changes are reported. libuv only signals another thread to (re)create the FSEvents stream, and the stream reports only changes made after it starts:

https://github.com/libuv/libuv/blob/2b4b918d3381100854250c89d5159d4206daafb7/src/unix/fsevents.c#L362-L372

So a change made straight after startup can be silently missed, and since #2054 started non-Watchman backends before the crawl, so can one made during the crawl. On a slow host the gap is long enough to hit regularly - it's the remaining flake in the Native watcher integration tests, where the suite's first event never arrives. 14 of 297 macOS test jobs since #1974 hit it, across all three Node versions.

This has `Watcher` confirm each root's watch before relying on it. Once every backend has started, it writes a health check file into each root and waits for it to be reported, as `checkHealth()` already does for the project root. If a watch can't be confirmed within 10s, Metro warns and carries on. Roots added later with `addRoot` are confirmed the same way.

A backend opts in by returning `false` from a new static `isLiveOnStart()`, which only `NativeWatcher` does - Watchman subscribes from the crawl's clock, and `FallbackWatcher`'s watches are live as soon as `fs.watch` returns. It's done in `Watcher`, after all the backends have started, because `NativeWatcher`s for multiple roots share one FSEvents stream, which restarts as each root is added.

This adds ~10ms to a watch-mode start on macOS without Watchman (145ms -> 155ms on our benchmark app).

Changelog:
```
 - **[Fix]**: Fix changes made immediately after startup sometimes being missed on macOS
```

Test plan:
New `Watcher-test.js` cases cover confirming each root before the crawl, warning and carrying on when a watch can't be confirmed, confirming a root added later, and leaving backends that are live on start alone. The backend integration tests now wait for a cookie of their own, as `Watcher` does.

To reproduce the race, I patched `NativeWatcher` to drop all events for its first 300ms. A `FileMap` in watch mode with two roots, writing a file to each as soon as `build()` resolves, sees neither change without this PR (3/3 runs) and both with it (3/3).

The `metro-file-map` suite passes on macOS, Linux and Windows. Benchmark (AI-driven): the harness from #2104, 8 interleaved rounds on an M4 Pro Mac mini, n=32 warm watch-mode starts per variant (+6.7%, 95% CI +5.2% to +8.0%).
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.

2 participants