Skip to content

fs: remove race condition for recursive watch on Linux #51406

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

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ async function* _watch(filename, options = kEmptyObject) {
// e.g. Linux due to the limitations of inotify.
if (options.recursive && !isOSX && !isWindows) {
const watcher = new nonNativeWatcher.FSWatcher(options);
await watcher[kFSWatchStart](filename);
watcher[kFSWatchStart](filename);
yield* watcher;
return;
}
Expand Down
58 changes: 12 additions & 46 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

const {
ArrayPrototypePush,
SafePromiseAllReturnVoid,
Promise,
PromisePrototypeThen,
SafeMap,
SafeSet,
StringPrototypeStartsWith,
Expand All @@ -31,42 +28,13 @@ const {
} = require('path');

let internalSync;
let internalPromises;

function lazyLoadFsPromises() {
internalPromises ??= require('fs/promises');
return internalPromises;
}

function lazyLoadFsSync() {
internalSync ??= require('fs');
return internalSync;
}
let kResistStopPropagation;

async function traverse(dir, files = new SafeMap(), symbolicLinks = new SafeSet()) {
const { opendir } = lazyLoadFsPromises();

const filenames = await opendir(dir);
const subdirectories = [];

for await (const file of filenames) {
const f = pathJoin(dir, file.name);

files.set(f, file);

// Do not follow symbolic links
if (file.isSymbolicLink()) {
symbolicLinks.add(f);
} else if (file.isDirectory()) {
ArrayPrototypePush(subdirectories, traverse(f, files));
}
}

await SafePromiseAllReturnVoid(subdirectories);

return files;
}
let kResistStopPropagation;

class FSWatcher extends EventEmitter {
#options = null;
Expand Down Expand Up @@ -135,13 +103,15 @@ class FSWatcher extends EventEmitter {
}
}

async #watchFolder(folder) {
const { opendir } = lazyLoadFsPromises();
#watchFolder(folder) {
const { readdirSync } = lazyLoadFsSync();

try {
const files = await opendir(folder);
const files = readdirSync(folder, {
withFileTypes: true,
});

for await (const file of files) {
for (const file of files) {
if (this.#closed) {
break;
}
Expand All @@ -159,7 +129,7 @@ class FSWatcher extends EventEmitter {
if (file.isFile()) {
this.#watchFile(f);
} else if (file.isDirectory() && !file.isSymbolicLink()) {
await this.#watchFolder(f);
this.#watchFolder(f);
}
}
}
Expand Down Expand Up @@ -220,14 +190,10 @@ class FSWatcher extends EventEmitter {
if (file.isDirectory()) {
this.#files.set(filename, file);

PromisePrototypeThen(
traverse(filename, this.#files, this.#symbolicFiles),
() => {
for (const f of this.#files.keys()) {
this.#watchFile(f);
}
},
);
this.#watchFolder(filename);
for (const f of this.#files.keys()) {
this.#watchFile(f);
}
} else {
this.#watchFile(filename);
}
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-fs-recursive-sync-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const { mkdtempSync, watch, writeFileSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join } = require('node:path');
const tmpDir = mkdtempSync(join(tmpdir(), 'repro-test-'));
const filename = join(tmpDir, 'test.file');
const assert = require('assert');

const keepalive = setTimeout(() => {
throw new Error('timed out');
}, common.platformTimeout(30_0000));

const watcher = watch(tmpDir, { recursive: true }, common.mustCall((eventType, _filename) => {
clearTimeout(keepalive);
watcher.close();
assert.strictEqual(eventType, 'rename');
assert.strictEqual(join(tmpDir, _filename), filename);
console.log(eventType, filename);
}));

writeFileSync(filename, 'foobar2');