Skip to content

Commit e342c7a

Browse files
ronagcjihrig
authored andcommitted
stream: fix Duplex._construct race
Ensures that _construct has finished before invoking _destroy. The 'constructed' property was not properly set to false for both writable and readable state. Fixes: #34448 PR-URL: #34456 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 8ad4920 commit e342c7a

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/internal/streams/destroy.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,6 @@ function construct(stream, cb) {
203203
return;
204204
}
205205

206-
stream.once(kConstruct, cb);
207-
208-
if (stream.listenerCount(kConstruct) > 1) {
209-
// Duplex
210-
return;
211-
}
212-
213206
const r = stream._readableState;
214207
const w = stream._writableState;
215208

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

216+
stream.once(kConstruct, cb);
217+
218+
if (stream.listenerCount(kConstruct) > 1) {
219+
// Duplex
220+
return;
221+
}
222+
223223
process.nextTick(constructNT, stream);
224224
}
225225

test/parallel/test-stream-construct.js

+26
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,29 @@ testDestroy((opts) => new Writable({
242242
construct: common.mustCall()
243243
});
244244
}
245+
246+
{
247+
// https://github.com/nodejs/node/issues/34448
248+
249+
let constructed = false;
250+
const d = new Duplex({
251+
readable: false,
252+
construct: common.mustCall((callback) => {
253+
setImmediate(common.mustCall(() => {
254+
constructed = true;
255+
callback();
256+
}));
257+
}),
258+
write(chunk, encoding, callback) {
259+
callback();
260+
},
261+
read() {
262+
this.push(null);
263+
}
264+
});
265+
d.resume();
266+
d.end('foo');
267+
d.on('close', common.mustCall(() => {
268+
assert.strictEqual(constructed, true);
269+
}));
270+
}

0 commit comments

Comments
 (0)