mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-05 01:29:32 +01:00
fix(watchers): stop a chokidar watcher whose fs.watch fails instead of retrying forever
chokidar 3 with `persistent: false` has no guard for an `fs.watch` that throws (EMFILE once the inotify instance limit is used up, ENOSPC once the watch limit is). It emits the error, then a TypeError, then retries on the parent directory, and ends up re-adding `/` forever. TeamWatcher only logged its `'error'` events, so one failed watch became an endless stream of stack traces. WorkflowRunWatcher already closed its watcher on error, but `close()` removes every listener, so the TypeError from the same pass became an unhandled rejection, one per 10 s poll, and the server exits after five of those in a minute. `closeWatcherOnError()` closes the watcher on its first error, keeps a no-op listener on it, and turns its `add()` into a no-op, since a callback chokidar still has in flight can otherwise re-add a path and reopen it. TeamWatcher logs one line per watcher and relies on its 30 s poll; WorkflowRunWatcher still drops the watcher and re-arms it on the next poll.
This commit is contained in:
1 parent
e99b42c387
commit
10e443a309
5 files changed
+465
-8
No files matched your search
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @fileoverview Close a chokidar watcher on its first error, safely.
|
||||
*
|
||||
* chokidar 3 with `persistent: false` has no guard for an `fs.watch` that
|
||||
* throws (EMFILE once the inotify instance limit is used up, ENOSPC once the
|
||||
* watch limit is): it emits the error, then a TypeError from closing the
|
||||
* watcher it never got, then retries on the parent directory, one level per
|
||||
* pass, and re-adds `/` forever, since the parent of `/` is `/`. Only a closed
|
||||
* watcher ends that loop, so a watcher whose `'error'` handler just logs logs
|
||||
* forever.
|
||||
*
|
||||
* Closing from the handler is not enough on its own:
|
||||
* - `close()` removes every listener, and the TypeError of the same pass is
|
||||
* emitted after it. With no `'error'` listener EventEmitter throws it, and it
|
||||
* reaches the process as an unhandled rejection. So a no-op listener stays
|
||||
* on the closed watcher.
|
||||
* - callbacks chokidar already has in flight can still call `add()` (a deleted
|
||||
* file's `_remove()` re-adds the file when chokidar thinks it was the only
|
||||
* watched path), and `add()` sets `closed = false`, which starts the climb
|
||||
* again, silently now. So `add()` becomes a no-op on the closed watcher.
|
||||
*
|
||||
* @module utils/close-watcher-on-error
|
||||
*/
|
||||
|
||||
import type { FSWatcher } from 'chokidar';
|
||||
|
||||
/**
|
||||
* Close `watcher` on its first `'error'` and pass that error to `onClosed`,
|
||||
* once. The caller's polling has to cover what the watcher watched.
|
||||
*/
|
||||
export function closeWatcherOnError(watcher: FSWatcher, onClosed: (err: unknown) => void): void {
|
||||
watcher.once('error', (err: unknown) => {
|
||||
watcher.close().catch(() => {}); // best-effort, like the stop() paths
|
||||
watcher.on('error', () => {}); // see the fileoverview
|
||||
watcher.add = () => watcher; // likewise
|
||||
onClosed(err);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user