fs: handle recursive watch setup races by panva · Pull Request #65699 · nodejs/node · GitHub
Skip to content
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
89 changes: 69 additions & 20 deletions lib/internal/fs/recursive_watch.js
2 changes: 0 additions & 2 deletions test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ test-fs-watch-ignore-regexp: SKIP
test-runner-coverage: PASS, FLAKY
# https://github.com/nodejs/node/issues/54346
test-esm-loader-hooks-inspect-wait: PASS, FLAKY
# https://github.com/nodejs/node/issues/65697
test-fs-watch-recursive-delete-race: SKIP

[$system==ibmi]
# https://github.com/nodejs/node/pull/30819
Expand Down
53 changes: 53 additions & 0 deletions test/parallel/test-fs-watch-recursive-aix-preserve-watch-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Flags: --expose-internals
'use strict';

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

if (!common.isAIX)
common.skip('AIX-specific ENODEV handling');

const assert = require('assert');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const { kFSWatchStart } = require('internal/fs/watchers');
const { FSWatcher } = require('internal/fs/recursive_watch');

tmpdir.refresh();

const directory = tmpdir.resolve('preserve-watch-error');
const originalStatSync = fs.statSync;
const originalWatch = fs.watch;
const expected = new Error('watch failed');
const verificationError = new Error('stat failed');
const watcher = new FSWatcher({ recursive: true, throwIfNoEntry: false });
const failVerification = common.mustCall(() => {
throw verificationError;
});

expected.code = 'ENODEV';
verificationError.code = 'EIO';
fs.mkdirSync(directory);
fs.watch = common.mustCall((filename) => {
assert.strictEqual(filename, directory);
throw expected;
});
fs.statSync = (filename, options) => {
if (filename === directory && options?.throwIfNoEntry === false) {
return failVerification();
}
return Reflect.apply(originalStatSync, fs, [filename, options]);
};

try {
assert.throws(
() => watcher[kFSWatchStart](directory),
(error) => {
assert.strictEqual(error, expected);
return true;
},
);
} finally {
watcher.close();
fs.statSync = originalStatSync;
fs.watch = originalWatch;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const { kFSWatchStart } = require('internal/fs/watchers');
const { FSWatcher } = require('internal/fs/recursive_watch');

function createWatcher() {
return { close() {} };
}

tmpdir.refresh();

const directory = tmpdir.resolve('retry-directory-disappears-before-read');
const child = path.join(directory, 'child');
const originalWatch = fs.watch;
const watcher = new FSWatcher({ recursive: true });
let childWatchCalls = 0;
let directoryListener;
let staleWatcherCloseCalls = 0;

fs.mkdirSync(child, { recursive: true });
fs.watch = common.mustCall((filename, options, listener) => {
if (filename === directory) {
directoryListener = listener;
return createWatcher();
}

assert.strictEqual(filename, child);
childWatchCalls++;
if (childWatchCalls === 1) {
fs.rmSync(child, { recursive: true });
return { close: () => staleWatcherCloseCalls++ };
}
return createWatcher();
}, 3);

try {
watcher[kFSWatchStart](directory);
assert.strictEqual(childWatchCalls, 1);
assert.strictEqual(staleWatcherCloseCalls, 1);

fs.mkdirSync(child);
directoryListener('rename', null);

assert.strictEqual(childWatchCalls, 2);
} finally {
watcher.close();
fs.watch = originalWatch;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Flags: --expose-internals
'use strict';

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

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

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');
const { kFSWatchStart } = require('internal/fs/watchers');
const { FSWatcher } = require('internal/fs/recursive_watch');

function createWatcher() {
return { close() {} };
}

tmpdir.refresh();

const directory = tmpdir.resolve('retry-directory-disappears-before-watch');
const child = path.join(directory, 'child');
const originalWatch = fs.watch;
const changes = [];
const watcher = new FSWatcher({ recursive: true });
let childWatchCalls = 0;
let directoryListener;
let watchError;

fs.mkdirSync(child, { recursive: true });
watcher.on('change', (eventType, filename) => changes.push([eventType, filename]));
fs.watch = common.mustCall((filename, options, listener) => {
if (filename === directory) {
directoryListener = listener;
return createWatcher();
}

assert.strictEqual(filename, child);
childWatchCalls++;
if (childWatchCalls === 1) {
fs.rmSync(child, { recursive: true });
try {
return Reflect.apply(originalWatch, fs, [filename, options, listener]);
} catch (error) {
watchError = error;
throw error;
}
}
return createWatcher();
}, 3);

try {
watcher[kFSWatchStart](directory);
assert.strictEqual(childWatchCalls, 1);
assert.match(watchError?.code, /^(ENOENT|ENODEV)$/);

fs.mkdirSync(child);
directoryListener('rename', null);

assert.strictEqual(childWatchCalls, 2);
assert.deepStrictEqual(changes, [['rename', 'child']]);
} finally {
watcher.close();
fs.watch = originalWatch;
}
Loading
Loading