Skip to content

fs: close dir before throwing if options.bufferSize is invalid #58856

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ class Dir {
}),
};

validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
try {
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
} catch (validationError) {
// Userland won't be able to close handle if we throw, so we close it first
this.#handle.close();
throw validationError;
}

this.#readPromisified = FunctionPrototypeBind(
promisify(this.#readImpl), this, false);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const process = require('node:process');

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

const testDir = tmpdir.path;
const files = ['empty', 'files', 'for', 'just', 'testing'];

process.on('warning', (cause) => {
// If any directory handle was left unclosed and then GC'd,
// it will emit `Warning: Closing directory handle on garbage collection`.
// Treat this warning as error.
throw new Error('Expected no warnings', { cause });

Check failure on line 18 in test/parallel/test-fs-opendir.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stderr --- (node:141437) Warning: Closing directory handle on garbage collection (Use `node --trace-warnings ...` to show where the warning was created) /home/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18 throw new Error('Expected no warnings', { cause }); ^ Error: Expected no warnings at process.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18:9) at process.emit (node:events:519:35) at doEmitWarning (node:internal/process/warning:85:11) at process.processTicksAndRejections (node:internal/process/task_queues:89:21) { [cause]: [Warning: Closing directory handle on garbage collection] } Node.js v25.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-fs-opendir.js

Check failure on line 18 in test/parallel/test-fs-opendir.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04)

--- stderr --- (node:141979) Warning: Closing directory handle on garbage collection (Use `node --trace-warnings ...` to show where the warning was created) /home/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18 throw new Error('Expected no warnings', { cause }); ^ Error: Expected no warnings at process.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18:9) at process.emit (node:events:519:35) at doEmitWarning (node:internal/process/warning:85:11) at process.processTicksAndRejections (node:internal/process/task_queues:89:21) { [cause]: [Warning: Closing directory handle on garbage collection] } Node.js v25.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-fs-opendir.js

Check failure on line 18 in test/parallel/test-fs-opendir.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- (node:64733) Warning: Closing directory handle on garbage collection (Use `node --trace-warnings ...` to show where the warning was created) /Users/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18 throw new Error('Expected no warnings', { cause }); ^ Error: Expected no warnings at process.<anonymous> (/Users/runner/work/node/node/node/test/parallel/test-fs-opendir.js:18:9) at process.emit (node:events:519:35) at doEmitWarning (node:internal/process/warning:85:11) at process.processTicksAndRejections (node:internal/process/task_queues:89:21) { [cause]: [Warning: Closing directory handle on garbage collection] } Node.js v25.0.0-pre Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/node/node/node/test/parallel/test-fs-opendir.js
});

// Make sure tmp directory is clean
tmpdir.refresh();

Expand Down
Loading