Skip to content

Commit b378fc3

Browse files
LiviaMedeirosRafaelGSS
authored andcommitted
fs: close dir before throwing if options.bufferSize is invalid
PR-URL: #58856 Fixes: #58854 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 7f7a833 commit b378fc3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/internal/fs/dir.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ class Dir {
5959
}),
6060
};
6161

62-
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
62+
try {
63+
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
64+
} catch (validationError) {
65+
// Userland won't be able to close handle if we throw, so we close it first
66+
this.#handle.close();
67+
throw validationError;
68+
}
6369

6470
this.#readPromisified = FunctionPrototypeBind(
6571
promisify(this.#readImpl), this, false);

test/parallel/test-fs-opendir.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ const common = require('../common');
44
const assert = require('assert');
55
const fs = require('fs');
66
const path = require('path');
7+
const process = require('node:process');
78

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

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

14+
process.on('warning', (cause) => {
15+
// If any directory handle was left unclosed and then GC'd,
16+
// it will emit `Warning: Closing directory handle on garbage collection`.
17+
// Treat this warning as error.
18+
throw new Error('Expected no warnings', { cause });
19+
});
20+
1321
// Make sure tmp directory is clean
1422
tmpdir.refresh();
1523

0 commit comments

Comments
 (0)