Skip to content
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

fs: do not crash if the watched file is removed while setting up watch #53452

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class FSWatcher extends EventEmitter {
this.#closed = true;

for (const file of this.#files.keys()) {
this.#watchers.get(file).close();
this.#watchers.get(file)?.close();
this.#watchers.delete(file);
}

Expand All @@ -98,7 +98,7 @@ class FSWatcher extends EventEmitter {
for (const filename of this.#files.keys()) {
if (StringPrototypeStartsWith(filename, file)) {
this.#files.delete(filename);
this.#watchers.get(filename).close();
this.#watchers.get(filename)?.close();
this.#watchers.delete(filename);
}
}
Expand Down Expand Up @@ -126,9 +126,16 @@ class FSWatcher extends EventEmitter {
this.#symbolicFiles.add(f);
}

this.#watchFile(f);
if (file.isDirectory() && !file.isSymbolicLink()) {
this.#watchFolder(f);
try {
this.#watchFile(f);
mcollina marked this conversation as resolved.
Show resolved Hide resolved
if (file.isDirectory() && !file.isSymbolicLink()) {
this.#watchFolder(f);
}
} catch (err) {
// Ignore ENOENT
if (err.code !== 'ENOENT') {
throw err;
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-fs-watch-recursive-linux-parallel-remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

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

if (!common.isLinux)
common.skip('This test can run only on Linux');

// Test that the watcher do not crash if the file "disappears" while
// watch is being set up.

const path = require('node:path');
const fs = require('node:fs');
const { spawn } = require('node:child_process');

const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
tmpdir.refresh();

const watcher = fs.watch(testDir, { recursive: true });
watcher.on('change', function(event, filename) {
// This console.log makes the error happen
// do not remove
console.log(filename, event);
});

const testFile = path.join(testDir, 'a');
const child = spawn(process.argv[0], ['-e', `const fs = require('node:fs'); for (let i = 0; i < 10000; i++) { const fd = fs.openSync('${testFile}', 'w'); fs.writeSync(fd, Buffer.from('hello')); fs.rmSync('${testFile}') }`], {
stdio: 'inherit'
});

child.on('exit', function() {
watcher.close();
});