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
fs: close dir before throwing if options.bufferSize is invalid
  • Loading branch information
LiviaMedeiros committed Jun 29, 2025
commit eadbae56bb701769473930af8fdfe928eef2e4ba
8 changes: 7 additions & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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 common = require('../common');
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 });
});

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

Expand Down
Loading