Skip to content

Commit 5274a8f

Browse files
ronagruyadorno
authored andcommitted
stream: avoid premature close when will not emit close
Fixes: #45281 PR-URL: #45301 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 43e002e commit 5274a8f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/internal/streams/end-of-stream.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ function eos(stream, options, callback) {
151151
callback.call(stream);
152152
};
153153

154+
const onclosed = () => {
155+
closed = true;
156+
157+
const errored = isWritableErrored(stream) || isReadableErrored(stream);
158+
159+
if (errored && typeof errored !== 'boolean') {
160+
return callback.call(stream, errored);
161+
}
162+
163+
callback.call(stream);
164+
};
165+
154166
const onrequest = () => {
155167
stream.req.on('finish', onfinish);
156168
};
@@ -186,22 +198,22 @@ function eos(stream, options, callback) {
186198
process.nextTick(onclose);
187199
} else if (wState?.errorEmitted || rState?.errorEmitted) {
188200
if (!willEmitClose) {
189-
process.nextTick(onclose);
201+
process.nextTick(onclosed);
190202
}
191203
} else if (
192204
!readable &&
193205
(!willEmitClose || isReadable(stream)) &&
194206
(writableFinished || isWritable(stream) === false)
195207
) {
196-
process.nextTick(onclose);
208+
process.nextTick(onclosed);
197209
} else if (
198210
!writable &&
199211
(!willEmitClose || isWritable(stream)) &&
200212
(readableFinished || isReadable(stream) === false)
201213
) {
202-
process.nextTick(onclose);
214+
process.nextTick(onclosed);
203215
} else if ((rState && stream.req && stream.aborted)) {
204-
process.nextTick(onclose);
216+
process.nextTick(onclosed);
205217
}
206218

207219
const cleanup = () => {

test/parallel/test-stream-finished.js

+14
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,17 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
667667
).end();
668668
});
669669
}
670+
671+
{
672+
const stream = new Duplex({
673+
write(chunk, enc, cb) {
674+
setImmediate(cb);
675+
}
676+
});
677+
678+
stream.end('foo');
679+
680+
finished(stream, { readable: false }, common.mustCall((err) => {
681+
assert(!err);
682+
}));
683+
}

0 commit comments

Comments
 (0)