-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
fs, stream: add initial Symbol.dispose
and Symbol.asyncDispose
support
#48518
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
Changes from 8 commits
8a25397
8dc2ee7
223a2d8
40ba972
e8a014f
48602bb
39b6ace
a6d798d
98285ae
5d0d435
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,12 @@ function copyPrototype(src, dest, prefix) { | |
copyPrototype(original.prototype, primordials, `${name}Prototype`); | ||
}); | ||
|
||
// Define Symbol.Dispose and Symbol.AsyncDispose | ||
// Until these are defined by the environment. | ||
MoLow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. | ||
primordials.SymbolDispose ??= primordials.SymbolFor('nodejs.dispose'); | ||
MoLow marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this mean Symbol.keyFor of these symbols won't properly return undefined? |
||
primordials.SymbolAsyncDispose ??= primordials.SymbolFor('nodejs.asyncDispose'); | ||
|
||
// Create copies of intrinsic objects that require a valid `this` to call | ||
// static methods. | ||
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ const { | |
ObjectGetOwnPropertyDescriptor, | ||
SafeMap, | ||
StringPrototypeStartsWith, | ||
Symbol, | ||
SymbolDispose, | ||
SymbolAsyncDispose, | ||
globalThis, | ||
} = primordials; | ||
|
||
|
@@ -82,6 +85,8 @@ function prepareExecution(options) { | |
|
||
require('internal/dns/utils').initializeDns(); | ||
|
||
setupSymbolDisposePolyfill(); | ||
|
||
if (isMainThread) { | ||
assert(internalBinding('worker').isMainThread); | ||
// Worker threads will get the manifest in the message handler. | ||
|
@@ -119,6 +124,14 @@ function prepareExecution(options) { | |
} | ||
} | ||
|
||
function setupSymbolDisposePolyfill() { | ||
MoLow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
Symbol.dispose ??= SymbolDispose; | ||
// eslint-disable-next-line node-core/prefer-primordials | ||
Symbol.asyncDispose ??= SymbolAsyncDispose; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be a good thing to polyfill them? Wouldn't users assume certain behaviors from the runtime because they exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is a way for typescript or bable to recognize these symbols without them being global. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcollina this is confirmed (with the TS team) to play nice. Users would be able to use them with the polyfill but not the syntactic sugar until v8 ships There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That breaks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
function setupUserModules(isLoaderWorker = false) { | ||
initializeCJSLoader(); | ||
initializeESMLoader(isLoaderWorker); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ const { | |
ObjectSetPrototypeOf, | ||
Promise, | ||
SafeSet, | ||
SymbolAsyncDispose, | ||
SymbolAsyncIterator, | ||
Symbol, | ||
} = primordials; | ||
|
@@ -67,6 +68,7 @@ const { | |
ERR_STREAM_UNSHIFT_AFTER_END_EVENT, | ||
ERR_UNKNOWN_ENCODING, | ||
}, | ||
AbortError, | ||
} = require('internal/errors'); | ||
const { validateObject } = require('internal/validators'); | ||
|
||
|
@@ -234,6 +236,15 @@ Readable.prototype[EE.captureRejectionSymbol] = function(err) { | |
this.destroy(err); | ||
}; | ||
|
||
Readable.prototype[SymbolAsyncDispose] = function() { | ||
let error; | ||
if (!this.destroyed) { | ||
error = this.readableEnded ? null : new AbortError(); | ||
this.destroy(error); | ||
} | ||
return new Promise((resolve, reject) => eos(this, (err) => (err && err !== error ? reject(err) : resolve(null)))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the outcome of the following? await readable[Symbol.asyncDispose]();
await readable[Symbol.asyncDispose](); Ideally, the second call should be a noop, but it seems like the second call could actually throw the For reference, here is the guidance from the spec related to how a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The second call would not call await readable[Symbol.asyncDispose](); // destroys the stream
await readable[Symbol.asyncDispose](); // a no-op The only difference is if the stream itself was destroyed with an error (e.g. the first call was unable to destroy it correctly) the second call would also reject with the same error as the first one. readable.destroy(ERR_COULD_NOT_RELEASE_CONNECTION); // or some such error
await readable[Symbol.asyncDispose](); // rejects with ERR_COULD_NOT_RELEASE_CONNECTION The Another thing to consider is two concurrent calls too await Promise.all([readable[Symbol.asyncDispose](), readable[Symbol.asyncDispose]()]); In that case I think it's possible the error is emitted twice which may be a bug, but I think it's fine since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explaining. I wanted to verify because it seemed that, if It might make sense to have tests for these two cases, at least, to verify these expectations: // test 1: verify sequential calls to [Symbol.asyncDispose]()
await readable[Symbol.asyncDispose]();
await readable[Symbol.asyncDispose]();
// test 2: verify concurrent calls to [Symbol.asyncDispose]();
await Promise.all([readable[Symbol.asyncDispose](), readable[Symbol.asyncDispose]()]); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rbuckton wanna open a PR? If you do, the easiest would be to add it to test/parallel/test-stream-readable-dispose.js and you don't have to build node for it you can use a nightly since it's (hopefully) a passing test. It could be aa good opportunity to practice contributing code to Ndoe :) Otherwise I'll add the test when I tackle the next stream API. Would this sort of test make sense for any disposable? Is there a list of principles we should follow? |
||
}; | ||
|
||
// Manually shove something into the read() buffer. | ||
// This returns true if the highWaterMark has not been hit yet, | ||
// similar to how Writable.write() returns true if you should | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { promises: fs } = require('fs'); | ||
|
||
async function doOpen() { | ||
const fh = await fs.open(__filename); | ||
fh.on('close', common.mustCall()); | ||
await fh[Symbol.asyncDispose](); | ||
} | ||
|
||
doOpen().then(common.mustCall()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { Readable } = require('stream'); | ||
const assert = require('assert'); | ||
|
||
{ | ||
const read = new Readable({ | ||
read() {} | ||
}); | ||
read.resume(); | ||
|
||
read.on('end', common.mustNotCall('no end event')); | ||
read.on('close', common.mustCall()); | ||
read.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.name, 'AbortError'); | ||
})); | ||
|
||
read[Symbol.asyncDispose]().then(common.mustCall(() => { | ||
assert.strictEqual(read.errored.name, 'AbortError'); | ||
assert.strictEqual(read.destroyed, true); | ||
})); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.