Repository navigation
metro-file-map: Don't exit when a watcher reports an error #2151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,6 +329,7 @@ export class Watcher extends EventEmitter { | |
| MAX_WAIT_TIME, | ||
| ); | ||
|
|
||
| watcher.onError(error => this.#reportBackendError(root, error)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit · NativeWatcher's own FSWatcher errors bypass this subscription and stay uncaught The description names I couldn't trigger one with real filesystem operations on macOS, so this is a robustness gap and not a known user-facing crash. Forwarding it to Evidence: |
||
| watcher.onFileEvent(change => { | ||
| const basename = path.basename(change.relativePath); | ||
| if (basename.startsWith(this.#options.healthCheckFilePrefix)) { | ||
|
|
@@ -410,6 +411,17 @@ export class Watcher extends EventEmitter { | |
| } | ||
| } | ||
|
|
||
| #reportBackendError(root: Path, error: Error): void { | ||
| if (this.#closed) { | ||
| debug('Ignoring an error reported after close for %s: %s', root, error); | ||
| return; | ||
| } | ||
| this.#options.console.error( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should fix · Hitting the watch limit logs once per directory, then starts normally with those directories unwatched
Each message only says the watcher The description's "this doesn't add noise for routine file churn" is true of churn, but not of this case. Suggested fix: report resource-limit errors ( Reproduced: |
||
| `metro-file-map: The watcher of ${root} reported an error: ` + | ||
| error.message, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Watch a root in addition to those given at construction, reporting its | ||
| * changes to the `onChange` passed to `watch()`. Does nothing outside watch | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ export class AbstractWatcher implements WatcherBackend { | |
| readonly doIgnore: (path: string) => boolean; | ||
|
|
||
| #emitter: EventEmitter = new EventEmitter(); | ||
| #stopped: boolean = false; | ||
|
|
||
| /** | ||
| * Whether a watch reports every change made after `startWatching` resolves. | ||
|
|
@@ -73,6 +74,7 @@ export class AbstractWatcher implements WatcherBackend { | |
| } | ||
|
|
||
| async stopWatching(): Promise<void> { | ||
| this.#stopped = true; | ||
| this.#emitter.removeAllListeners(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe To keep the emitter from not having any listeners anymore? |
||
| } | ||
|
|
||
|
|
@@ -84,6 +86,11 @@ export class AbstractWatcher implements WatcherBackend { | |
| } | ||
|
|
||
| emitError(error: Error) { | ||
| // Work still in flight after stopWatching() can report an error, when | ||
| // nothing listens any more and emitting 'error' would throw. | ||
| if (this.#stopped) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should fix · Late errors still throw when the Watchman client's connect is in flight at close The description says late errors from in-flight work no longer throw, but this guard only covers the backend's own emitter. Failure case: Watchman drops, The PR does fix a neighboring case. On base, the same sequence fails earlier, with an unhandled rejection of Suggested fix: in Reproduced: |
||
| return; | ||
| } | ||
| this.#emitter.emit('error', error); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| * @oncall react_native | ||
| */ | ||
|
|
||
| import {AbstractWatcher} from '../AbstractWatcher'; | ||
| import {join} from 'node:path'; | ||
|
|
||
| const options = { | ||
| included: null, | ||
| ignored: null, | ||
| watchmanDeferStates: [], | ||
| }; | ||
|
|
||
| describe('AbstractWatcher errors', () => { | ||
| test('reports an error to its listeners while watching', async () => { | ||
| const watcher = new AbstractWatcher(join('/', 'project'), options); | ||
| const onError = jest.fn<[Error], void>(); | ||
| watcher.onError(onError); | ||
| await watcher.startWatching(); | ||
|
|
||
| const error = new Error('watch failed'); | ||
| watcher.emitError(error); | ||
|
|
||
| expect(onError.mock.calls).toEqual([[error]]); | ||
| }); | ||
|
|
||
| test('does not throw for an error reported after stopWatching', async () => { | ||
| const watcher = new AbstractWatcher(join('/', 'project'), options); | ||
| watcher.onError(() => {}); | ||
| await watcher.startWatching(); | ||
| await watcher.stopWatching(); | ||
|
|
||
| expect(() => watcher.emitError(new Error('late error'))).not.toThrow(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should fix · A failed Watchman reconnect now leaves a permanently dead watcher behind one log line
WatchmanWatcher.#initreconnects once onend. If that connection fails, the error goes toemitErrorand nothing retries, because a failed connect never emits anotherend.On base, this ended the process. With this subscription, Metro stays up and receives no more file events for that root for the rest of the session. That happens on any Watchman restart or upgrade where the socket isn't back in time for the single attempt. The log line doesn't say that changes are no longer being seen, and the health check that could expose it is opt-in.
Failure case: Watchman goes down, the reconnect gets
ECONNREFUSED, and Watchman comes back. Metro makes no further attempts and keeps serving old code until restarted.Suggested fix: retry with backoff in
WatchmanWatcher. At a minimum, say in the message that watching has stopped.Watcher.js:348rejectsrecrawlevents under Watchman, so a recrawl on recovery would need more work.Reproduced:
__repro__/watchman.js reconnect(realfb-watchmanclient, fake in-process socket). Head:connection attempts after Watchman returned: 0 ; live connections: 0 ; errors logged: 1. Base:UNCAUGHT: connect ECONNREFUSED. · id324c2660