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: fix nonNativeWatcher leak of StatWatchers #45501

Merged
merged 1 commit into from
Nov 27, 2022
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
9 changes: 3 additions & 6 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,11 @@ class FSWatcher extends EventEmitter {
this.#symbolicFiles.add(f);
}

this.#files.set(f, file);
anonrig marked this conversation as resolved.
Show resolved Hide resolved
if (file.isFile()) {
this.#watchFile(f);
} else {
this.#files.set(f, file);

if (file.isDirectory() && !file.isSymbolicLink()) {
await this.#watchFolder(f);
}
} else if (file.isDirectory() && !file.isSymbolicLink()) {
await this.#watchFolder(f);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-watch-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,33 @@ tmpdir.refresh();
});
})().then(common.mustCall());


(async () => {
// Assert recursive watch does not leak handles
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
const testDirectory = path.join(rootDirectory, 'test-7');
const filePath = path.join(testDirectory, 'only-file.txt');
fs.mkdirSync(testDirectory);

let watcherClosed = false;
const watcher = fs.watch(testDirectory, { recursive: true });
watcher.on('change', common.mustCallAtLeast(async (event, filename) => {
await setTimeout(common.platformTimeout(100));
anonrig marked this conversation as resolved.
Show resolved Hide resolved
if (filename === path.basename(filePath)) {
watcher.close();
watcherClosed = true;
}
await setTimeout(common.platformTimeout(100));
anonrig marked this conversation as resolved.
Show resolved Hide resolved
assert(!process._getActiveHandles().some((handle) => handle.constructor.name === 'StatWatcher'));
}));

process.on('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'content');
})().then(common.mustCall());

(async () => {
// Handle non-boolean values for options.recursive

Expand Down