Skip to content

Commit b9d1dda

Browse files
committed
stream: finished waits for 'close' on OutgoingMessage
Don't invoke finished callback until OutgoingMessagehas emitted 'close'.
1 parent db79783 commit b9d1dda

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function isRequest(stream) {
2121
return stream.setHeader && typeof stream.abort === 'function';
2222
}
2323

24+
function isOutgoingMessage(stream) {
25+
return (
26+
typeof stream._removedConnection === 'boolean' &&
27+
typeof stream._removedContLen === 'boolean' &&
28+
typeof stream._removedTE === 'boolean' &&
29+
typeof stream._closed === 'boolean'
30+
);
31+
}
32+
2433
function isReadable(stream) {
2534
return typeof stream.readable === 'boolean' ||
2635
typeof stream.readableEnded === 'boolean' ||
@@ -80,7 +89,7 @@ function eos(stream, options, callback) {
8089
// TODO (ronag): Improve soft detection to include core modules and
8190
// common ecosystem modules that do properly emit 'close' but fail
8291
// this generic check.
83-
let willEmitClose = (
92+
let willEmitClose = isOutgoingMessage(stream) || (
8493
state &&
8594
state.autoDestroy &&
8695
(state.emitClose || isSocket(stream)) &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { finished } = require('stream');
4+
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const server = http.createServer(function(req, res) {
9+
let closed = false;
10+
res
11+
.on('close', common.mustCall(() => {
12+
closed = true;
13+
finished(res, common.mustCall(() => {
14+
server.close();
15+
}));
16+
}))
17+
.end();
18+
finished(res, common.mustCall(() => {
19+
assert.strictEqual(closed, true);
20+
}));
21+
22+
}).listen(0, function() {
23+
http
24+
.request({
25+
port: this.address().port,
26+
method: 'GET'
27+
})
28+
.on('response', function(res) {
29+
res.resume();
30+
})
31+
.end();
32+
});

0 commit comments

Comments
 (0)