Skip to content

Commit

Permalink
stream: finished waits for 'close' on OutgoingMessage
Browse files Browse the repository at this point in the history
Don't invoke finished callback until OutgoingMessagehas
emitted 'close'.

PR-URL: #36648
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ronag authored and jasnell committed Jan 11, 2021
1 parent f4ef517 commit 52546c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/internal/streams/end-of-stream.js
Expand Up @@ -21,6 +21,16 @@ function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}

function isServerResponse(stream) {
return (
typeof stream._sent100 === 'boolean' &&
typeof stream._removedConnection === 'boolean' &&
typeof stream._removedContLen === 'boolean' &&
typeof stream._removedTE === 'boolean' &&
typeof stream._closed === 'boolean'
);
}

function isReadable(stream) {
return typeof stream.readable === 'boolean' ||
typeof stream.readableEnded === 'boolean' ||
Expand Down Expand Up @@ -80,7 +90,7 @@ function eos(stream, options, callback) {
// TODO (ronag): Improve soft detection to include core modules and
// common ecosystem modules that do properly emit 'close' but fail
// this generic check.
let willEmitClose = (
let willEmitClose = isServerResponse(stream) || (
state &&
state.autoDestroy &&
(state.emitClose || isSocket(stream)) &&
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-outgoing-finished.js
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');
const { finished } = require('stream');

const http = require('http');
const assert = require('assert');

const server = http.createServer(function(req, res) {
let closed = false;
res
.on('close', common.mustCall(() => {
closed = true;
finished(res, common.mustCall(() => {
server.close();
}));
}))
.end();
finished(res, common.mustCall(() => {
assert.strictEqual(closed, true);
}));

}).listen(0, function() {
http
.request({
port: this.address().port,
method: 'GET'
})
.on('response', function(res) {
res.resume();
})
.end();
});

0 comments on commit 52546c0

Please sign in to comment.