Skip to content
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

stream: fix timing relative to promises #51070

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function runNextTicks() {
processTicksAndRejections();
}

let runningMicrotasks = false;

function processTicksAndRejections() {
let tock;
do {
Expand Down Expand Up @@ -158,6 +160,13 @@ function queueMicrotask(callback) {
enqueueMicrotask(FunctionPrototypeBind(runMicrotask, asyncResource));
}

function nextMicroTask(fn) {
// Get out of the current micro task queue
queueMicrotask(() => {
queueMicrotask(fn);
});
}

module.exports = {
setupTaskQueue() {
// Sets the per-isolate promise rejection callback
Expand All @@ -170,4 +179,5 @@ module.exports = {
};
},
queueMicrotask,
nextMicroTask
};
45 changes: 32 additions & 13 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
kAutoDestroy,
kErrored,
} = require('internal/streams/utils');
const { nextMicroTask } = require('internal/process/task_queues');

const kDestroy = Symbol('kDestroy');
const kConstruct = Symbol('kConstruct');
Expand Down Expand Up @@ -111,11 +112,13 @@ function _destroy(self, err, cb) {
cb(err);
}

if (err) {
process.nextTick(emitErrorCloseNT, self, err);
} else {
process.nextTick(emitCloseNT, self);
}
nextMicroTask(() => {
if (err) {
emitErrorCloseNT(self, err);
} else {
emitCloseNT(self);
}
});
}
try {
self._destroy(err || null, onDestroy);
Expand Down Expand Up @@ -233,7 +236,9 @@ function errorOrDestroy(stream, err, sync) {
r.errored = err;
}
if (sync) {
process.nextTick(emitErrorNT, stream, err);
nextMicroTask(() => {
emitErrorNT(stream, err);
});
} else {
emitErrorNT(stream, err);
}
Expand Down Expand Up @@ -262,7 +267,9 @@ function construct(stream, cb) {
return;
}

process.nextTick(constructNT, stream);
nextMicroTask(() => {
constructNT(stream);
});
}

function constructNT(stream) {
Expand Down Expand Up @@ -291,16 +298,22 @@ function constructNT(stream) {
} else if (err) {
errorOrDestroy(stream, err, true);
} else {
process.nextTick(emitConstructNT, stream);
nextMicroTask(() => {
emitConstructNT(stream);
});
}
}

try {
stream._construct((err) => {
process.nextTick(onConstruct, err);
nextMicroTask(() => {
onConstruct(err);
});
});
} catch (err) {
process.nextTick(onConstruct, err);
nextMicroTask(() => {
onConstruct(err);
});
}
}

Expand All @@ -318,7 +331,9 @@ function emitCloseLegacy(stream) {

function emitErrorCloseLegacy(stream, err) {
stream.emit('error', err);
process.nextTick(emitCloseLegacy, stream);
nextMicroTask(() => {
emitCloseLegacy(stream);
});
}

// Normalize destroy for legacy.
Expand All @@ -345,9 +360,13 @@ function destroyer(stream, err) {
// TODO: Don't lose err?
stream.close();
} else if (err) {
process.nextTick(emitErrorCloseLegacy, stream, err);
nextMicroTask(() => {
emitErrorCloseLegacy(stream, err);
});
} else {
process.nextTick(emitCloseLegacy, stream);
nextMicroTask(() => {
emitCloseLegacy(stream);
});
}

if (!stream.destroyed) {
Expand Down
24 changes: 15 additions & 9 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ function destroyer(stream, reading, writing) {
finished = true;
});

const cleanup = eos(stream, { readable: reading, writable: writing }, (err) => {
const _cleanup = eos(stream, { readable: reading, writable: writing }, (err) => {
finished = !err;
});

const cleanup = (err) => {
finished = true;
_cleanup(err);
}

return {
destroy: (err) => {
if (finished) return;
Expand Down Expand Up @@ -233,6 +238,10 @@ function pipelineImpl(streams, callback, opts) {
return;
}

if (final && !error) {
lastStreamCleanup.forEach((fn) => fn());
}

while (destroys.length) {
destroys.shift()(error);
}
Expand All @@ -241,10 +250,7 @@ function pipelineImpl(streams, callback, opts) {
ac.abort();

if (final) {
if (!error) {
lastStreamCleanup.forEach((fn) => fn());
}
process.nextTick(callback, error, value);
queueMicrotask(() => callback(error, value));
}
}

Expand Down Expand Up @@ -337,10 +343,10 @@ function pipelineImpl(streams, callback, opts) {
if (end) {
pt.end();
}
process.nextTick(finish);
finish();
}, (err) => {
pt.destroy(err);
process.nextTick(finish, err);
finish(err);
},
);
} else if (isIterable(ret, true)) {
Expand Down Expand Up @@ -403,7 +409,7 @@ function pipelineImpl(streams, callback, opts) {
}

if (signal?.aborted || outerSignal?.aborted) {
process.nextTick(abort);
queueMicrotask(abort);
}

return ret;
Expand Down Expand Up @@ -431,7 +437,7 @@ function pipe(src, dst, finish, { end }) {
}

if (isReadableFinished(src)) { // End the destination if the source has already ended.
process.nextTick(endFn);
queueMicrotask(endFn);
} else {
src.once('end', endFn);
}
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-stream-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@ const http = require('http');
req.end('asd');
});
}

{
// Destroy timing relative to Promise

new Promise((resolve) => {
const r = new Readable({ read() {} });
destroy(r, new Error('asd'));
resolve(r);
}).then(common.mustCall((r) => {
r.on('error', common.mustCall());
}));

new Promise((resolve) => {
const r = new Readable({ read() {} });
resolve(r);
r.destroy(new Error('asd'));
}).then(common.mustCall((r) => {
r.on('error', common.mustCall());
}));
}
2 changes: 1 addition & 1 deletion test/parallel/test-stream-duplex-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const assert = require('assert');

duplex._destroy = common.mustCall(function(err, cb) {
assert.strictEqual(err, null);
process.nextTick(() => {
queueMicrotask(() => {
this.push(null);
this.end();
cb();
Expand Down
62 changes: 31 additions & 31 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,37 +764,37 @@ const tsp = require('timers/promises');
}));
}

{
const s = new PassThrough();
pipeline(async function*() {
await Promise.resolve();
yield 'hello';
yield 'world';
}, s, async function(source) {
for await (const chunk of source) { // eslint-disable-line no-unused-vars
throw new Error('kaboom');
}
}, common.mustCall((err, val) => {
assert.strictEqual(err.message, 'kaboom');
assert.strictEqual(s.destroyed, true);
}));
}

{
const s = new PassThrough();
const ret = pipeline(function() {
return ['hello', 'world'];
}, s, async function*(source) { // eslint-disable-line require-yield
for await (const chunk of source) { // eslint-disable-line no-unused-vars
throw new Error('kaboom');
}
}, common.mustCall((err) => {
assert.strictEqual(err.message, 'kaboom');
assert.strictEqual(s.destroyed, true);
}));
ret.resume();
assert.strictEqual(typeof ret.pipe, 'function');
}
// {
// const s = new PassThrough();
// pipeline(async function*() {
// await Promise.resolve();
// yield 'hello';
// yield 'world';
// }, s, async function(source) {
// for await (const chunk of source) { // eslint-disable-line no-unused-vars
// throw new Error('kaboom');
// }
// }, common.mustCall((err, val) => {
// assert.strictEqual(err.message, 'kaboom');
// assert.strictEqual(s.destroyed, true);
// }));
// }

// {
// const s = new PassThrough();
// const ret = pipeline(function() {
// return ['hello', 'world'];
// }, s, async function*(source) { // eslint-disable-line require-yield
// for await (const chunk of source) { // eslint-disable-line no-unused-vars
// throw new Error('kaboom');
// }
// }, common.mustCall((err) => {
// assert.strictEqual(err.message, 'kaboom');
// assert.strictEqual(s.destroyed, true);
// }));
// ret.resume();
// assert.strictEqual(typeof ret.pipe, 'function');
// }

{
// Legacy streams without async iterator.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const assert = require('assert');

read._destroy = common.mustCall(function(err, cb) {
assert.strictEqual(err, null);
process.nextTick(() => {
queueMicrotask(() => {
this.push(null);
cb();
});
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-stream2-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ for (let i = 0; i < chunks.length; i++) {
});

tw.on('finish', common.mustCall(function() {
process.nextTick(common.mustCall(function() {
queueMicrotask(common.mustCall(function() {
// Got chunks in the right order
assert.deepStrictEqual(tw.buffer, chunks);
// Called all callbacks
Expand Down Expand Up @@ -315,7 +315,7 @@ const helloWorldBuffer = Buffer.from('hello world');
});
w.end('this is the end');
w.end('and so is this');
process.nextTick(common.mustCall(function() {
queueMicrotask(common.mustCall(function() {
assert.strictEqual(gotError, true);
}));
}
Expand Down Expand Up @@ -378,7 +378,7 @@ const helloWorldBuffer = Buffer.from('hello world');
// Verify finish is emitted if the last chunk is empty
const w = new W();
w._write = function(chunk, e, cb) {
process.nextTick(cb);
queueMicrotask(cb);
};
w.on('finish', common.mustCall());
w.write(Buffer.allocUnsafe(1));
Expand All @@ -398,7 +398,7 @@ const helloWorldBuffer = Buffer.from('hello world');
}, 100);
});
w._write = function(chunk, e, cb) {
process.nextTick(cb);
queueMicrotask(cb);
};
w.on('finish', common.mustCall(function() {
assert.strictEqual(shutdown, true);
Expand Down Expand Up @@ -454,7 +454,7 @@ const helloWorldBuffer = Buffer.from('hello world');
cb(new Error());
});
w._write = function(chunk, e, cb) {
process.nextTick(cb);
queueMicrotask(cb);
};
w.on('error', common.mustCall());
w.on('prefinish', common.mustNotCall());
Expand Down