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

lib: fix recursive watch on Linux #51049

Closed
Closed
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
39 changes: 3 additions & 36 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 Down Expand Up @@ -42,31 +39,8 @@ 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 @@ -219,15 +193,8 @@ 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.#watchFile(filename);
this.#watchFolder(filename);
} else {
this.#watchFile(filename);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -50,7 +49,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(childrenAbsolutePath, 'world');

process.once('exit', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -46,9 +45,7 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.mkdirSync(filePath);
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(childrenAbsolutePath, 'world');

process.once('exit', function() {
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-fs-watch-recursive-add-file-with-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -43,7 +42,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'world');

process.on('exit', function() {
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-fs-watch-recursive-add-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -41,7 +40,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(testFile, 'world');

process.once('exit', function() {
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-fs-watch-recursive-add-folder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -41,7 +40,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.mkdirSync(testFile);

process.once('exit', function() {
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-fs-watch-recursive-assert-leaks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -31,18 +30,15 @@ tmpdir.refresh();
let watcherClosed = false;
const watcher = fs.watch(testDirectory, { recursive: true });
watcher.on('change', common.mustCallAtLeast(async (event, filename) => {
await setTimeout(common.platformTimeout(100));
if (filename === path.basename(filePath)) {
watcher.close();
watcherClosed = true;
}
await setTimeout(common.platformTimeout(100));
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());
4 changes: 0 additions & 4 deletions test/parallel/test-fs-watch-recursive-symlink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common');
const { setTimeout } = require('timers/promises');

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
Expand Down Expand Up @@ -48,7 +47,6 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(filePath, 'world');

process.once('exit', function() {
Expand Down Expand Up @@ -89,9 +87,7 @@ tmpdir.refresh();
}
});

await setTimeout(common.platformTimeout(100));
fs.writeFileSync(forbiddenFile, 'world');
await setTimeout(common.platformTimeout(100));
fs.writeFileSync(acceptableFile, 'acceptable');

process.once('exit', function() {
Expand Down