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: add catch handler for async _construct #34416

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/internal/streams/destroy.js
Expand Up @@ -230,7 +230,7 @@ function constructNT(stream) {
const s = w || r;

let called = false;
stream._construct((err) => {
const result = stream._construct((err) => {
if (r) {
r.constructed = true;
}
Expand All @@ -252,6 +252,25 @@ function constructNT(stream) {
process.nextTick(emitConstructNT, stream);
}
});
if (result !== undefined && result !== null) {
try {
const then = result.then;
if (typeof then === 'function') {
then.call(result, undefined, function(err) {
jasnell marked this conversation as resolved.
Show resolved Hide resolved
if (r) {
r.constructed = true;
}
if (w) {
w.constructed = true;
}
called = true;
process.nextTick(errorOrDestroy, stream, err);
});
}
} catch (err) {
process.nextTick(emitErrorNT, stream, err);
}
}
jasnell marked this conversation as resolved.
Show resolved Hide resolved
}

function emitConstructNT(stream) {
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-stream-construct-async-error.js
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const { Duplex } = require('stream');
const { setTimeout } = require('timers/promises');
const assert = require('assert');

class Foo extends Duplex {
async _construct(cb) {
// eslint-disable-next-line no-restricted-syntax
jasnell marked this conversation as resolved.
Show resolved Hide resolved
await setTimeout(common.platformTimeout(1));
cb();
throw new Error('boom');
}
}

const foo = new Foo();
foo.on('error', common.expectsError({
message: 'boom'
}));
foo.on('close', common.mustCall(() => {
assert(foo._writableState.constructed);
assert(foo._readableState.constructed);
}));