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 Duplex._construct race #34456

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
14 changes: 7 additions & 7 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,6 @@ function construct(stream, cb) {
return;
}

stream.once(kConstruct, cb);

if (stream.listenerCount(kConstruct) > 1) {
// Duplex
return;
}

const r = stream._readableState;
const w = stream._writableState;

Expand All @@ -220,6 +213,13 @@ function construct(stream, cb) {
w.constructed = false;
}

stream.once(kConstruct, cb);

if (stream.listenerCount(kConstruct) > 1) {
// Duplex
return;
}

process.nextTick(constructNT, stream);
}

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-stream-construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,29 @@ testDestroy((opts) => new Writable({
construct: common.mustCall()
});
}

{
// https://github.com/nodejs/node/issues/34448

let constructed = false;
const d = new Duplex({
readable: false,
construct: common.mustCall((callback) => {
setImmediate(common.mustCall(() => {
constructed = true;
callback();
}));
}),
write(chunk, encoding, callback) {
callback();
},
read() {
this.push(null);
}
});
d.resume();
d.end('foo');
d.on('close', common.mustCall(() => {
assert.strictEqual(constructed, true);
}));
}