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: fixes recursive fs.watch crash on Linux when deleting files #52349

Merged
merged 10 commits into from
Apr 19, 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
11 changes: 8 additions & 3 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@ class FSWatcher extends EventEmitter {
persistent: this.#options.persistent,
}, (eventType, filename) => {
const existingStat = this.#files.get(file);
const currentStats = statSync(file);
let currentStats;

this.#files.set(file, currentStats);
try {
currentStats = statSync(file);
this.#files.set(file, currentStats);
} catch {
// This happens if the file was removed
}

if (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0) {
if (currentStats === undefined || (currentStats.birthtimeMs === 0 && existingStat.birthtimeMs !== 0)) {
// The file is now deleted
this.#files.delete(file);
this.#watchers.delete(file);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fs-watch-recursive-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('node:assert');

tmpdir.refresh();

fs.mkdirSync(tmpdir.resolve('./parent/child'), { recursive: true });

fs.writeFileSync(tmpdir.resolve('./parent/child/test.tmp'), 'test');

const watcher = fs.watch(tmpdir.resolve('./parent'), { recursive: true }, common.mustCall((eventType, filename) => {
// We are only checking for the filename to avoid having Windows, Linux and Mac specific assertions
assert.strictEqual(filename.indexOf('test.tmp') >= 0, true);

Check failure on line 16 in test/parallel/test-fs-watch-recursive-delete.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: false !== true at FSWatcher.<anonymous> (/Users/runner/work/node/node/test/parallel/test-fs-watch-recursive-delete.js:16:10) at FSWatcher.<anonymous> (/Users/runner/work/node/node/test/common/index.js:473:15) at FSWatcher.emit (node:events:520:28) at FSWatcher._handle.onchange (node:internal/fs/watchers:215:12) { generatedMessage: true, code: 'ERR_ASSERTION', actual: false, expected: true, operator: 'strictEqual' } Node.js v22.0.0-pre Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-fs-watch-recursive-delete.js
watcher.close();
mcollina marked this conversation as resolved.
Show resolved Hide resolved
}));

fs.rmSync(tmpdir.resolve('./parent/child/test.tmp'));
Loading