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: remove race condition for recursive watch on Linux #51406

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup
Signed-off-by: Matteo Collina <[email protected]>
mcollina committed Jan 19, 2024

Verified

This commit was signed with the committer’s verified signature.
mcollina Matteo Collina
commit 00ce83355b4fc9c075b4051c19c72b501447c3ac
44 changes: 26 additions & 18 deletions test/parallel/test-fs-watch-recursive-add-folder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

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

if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');

// fs-watch on folders have limited capability in AIX.
// The testcase makes use of folder watching, and causes
// hang. This behavior is documented. Skip this for AIX.

if (common.isAIX)
common.skip('folder watch capability is limited in AIX.');

@@ -16,27 +21,30 @@ const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
tmpdir.refresh();

// Add a folder to already watching folder
(async () => {
// Add a folder to already watching folder

const rootDirectory = fs.mkdtempSync(testDir + path.sep);
const testDirectory = path.join(rootDirectory, 'test-2');
fs.mkdirSync(testDirectory);
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
const testDirectory = path.join(rootDirectory, 'test-2');
fs.mkdirSync(testDirectory);

const testFile = path.join(testDirectory, 'folder-2');
const testFile = path.join(testDirectory, 'folder-2');

const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');
const watcher = fs.watch(testDirectory, { recursive: true });
let watcherClosed = false;
watcher.on('change', function(event, filename) {
assert.strictEqual(event, 'rename');

if (filename === path.basename(testFile)) {
watcher.close();
watcherClosed = true;
}
});
if (filename === path.basename(testFile)) {
watcher.close();
watcherClosed = true;
}
});

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

process.once('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
process.once('exit', function() {
assert(watcherClosed, 'watcher Object was not closed');
});
})().then(common.mustCall());