Skip to content

Commit

Permalink
fs: fixes recursive fs.watch crash on Linux when deleting files
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
Fixes: #52018
PR-URL: #52349
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
  • Loading branch information
mcollina authored and aduh95 committed Apr 29, 2024
1 parent ed080d8 commit a7e03d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@ class FSWatcher extends EventEmitter {
persistent: this.#options.persistent,
}, (eventType, filename) => {
const existingStat = this.#files.get(file);
const currentStats = statSync(file);
let currentStats;

this.#files.set(file, currentStats);
try {
currentStats = statSync(file);
this.#files.set(file, currentStats);
} catch {
// This happens if the file was removed
}

if (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0) {
if (currentStats === undefined || (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0)) {
// The file is now deleted
this.#files.delete(file);
this.#watchers.delete(file);
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-fs-watch-recursive-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');

if (common.isSunOS)
common.skip('SunOS behaves differently');

tmpdir.refresh();

fs.mkdirSync(tmpdir.resolve('./parent/child'), { recursive: true });

fs.writeFileSync(tmpdir.resolve('./parent/child/test.tmp'), 'test');

const toWatch = tmpdir.resolve('./parent');

const onFileUpdate = common.mustCallAtLeast((eventType, filename) => {
// We are only checking for the filename to avoid having Windows, Linux and Mac specific assertions
if (fs.readdirSync(tmpdir.resolve('./parent')).length === 0) {
watcher.close();
}
}, 1);

const watcher = fs.watch(toWatch, { recursive: true }, onFileUpdate);

// We must wait a bit `fs.rm()` to let the watcher be set up properly
setTimeout(() => {
fs.rm(tmpdir.resolve('./parent/child'), { recursive: true }, common.mustCall());
}, common.platformTimeout(500));

0 comments on commit a7e03d3

Please sign in to comment.