Skip to content

fs: make Dir disposers idempotent #58692

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

Merged
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
7 changes: 4 additions & 3 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6773,8 +6773,8 @@ changes:
description: No longer experimental.
-->

Calls `dir.close()` and returns a promise that fulfills when the
dir is closed.
Calls `dir.close()` if the directory handle is open, and returns a promise that
fulfills when disposal is complete.

#### `dir[Symbol.dispose]()`

Expand All @@ -6786,7 +6786,8 @@ changes:
description: No longer experimental.
-->

Calls `dir.closeSync()` and returns `undefined`.
Calls `dir.closeSync()` if the directory handle is open, and returns
`undefined`.

### Class: `fs.Dirent`

Expand Down
34 changes: 13 additions & 21 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {

const { FSReqCallback } = binding;
const {
assignFunctionName,
promisify,
} = require('internal/util');
const {
Expand Down Expand Up @@ -296,31 +295,24 @@ class Dir {
await this.#closePromisified();
}
}

[SymbolDispose]() {
if (this.#closed) return;
this.closeSync();
}

async [SymbolAsyncDispose]() {
if (this.#closed) return;
await this.#closePromisified();
}
}

const nonEnumerableDescriptor = {
enumerable: false,
writable: true,
configurable: true,
};
ObjectDefineProperties(Dir.prototype, {
[SymbolDispose]: {
__proto__: null,
...nonEnumerableDescriptor,
value: assignFunctionName(SymbolDispose, function() {
this.closeSync();
}),
},
[SymbolAsyncDispose]: {
__proto__: null,
...nonEnumerableDescriptor,
value: assignFunctionName(SymbolAsyncDispose, function() {
this.close();
}),
},
[SymbolAsyncIterator]: {
__proto__: null,
...nonEnumerableDescriptor,
enumerable: false,
writable: true,
configurable: true,
value: Dir.prototype.entries,
},
});
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-fs-promises-file-handle-dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ async function explicitCall() {

const dh = await fs.opendir(__dirname);
await dh[Symbol.asyncDispose]();
// Repeat invocations should not reject
await dh[Symbol.asyncDispose]();
await assert.rejects(dh.read(), { code: 'ERR_DIR_CLOSED' });

const dhSync = opendirSync(__dirname);
dhSync[Symbol.dispose]();
// Repeat invocations should not throw
dhSync[Symbol.dispose]();
assert.throws(() => dhSync.readSync(), { code: 'ERR_DIR_CLOSED' });
}

Expand Down
Loading