Skip to content

Commit

Permalink
stream: don't deadlock on aborted stream
Browse files Browse the repository at this point in the history
Not all streams (e.g. http.ClientRequest) will always emit
'close' after 'aborted'.

PR-URL: #29376
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
ronag authored and mcollina committed Sep 2, 2019
1 parent 94c944d commit 897587c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ function eos(stream, opts, callback) {
stream.on('close', onlegacyfinish);
}

// Not all streams will emit 'close' after 'aborted'.
if (typeof stream.aborted === 'boolean') {
stream.on('aborted', onclose);
}

stream.on('end', onend);
stream.on('finish', onfinish);
if (opts.error !== false) stream.on('error', onerror);
stream.on('close', onclose);

return function() {
stream.removeListener('aborted', onclose);
stream.removeListener('complete', onfinish);
stream.removeListener('abort', onclose);
stream.removeListener('request', onrequest);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-http-client-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const http = require('http');
const { finished } = require('stream');

{
// Test abort before finished.

const server = http.createServer(function(req, res) {
res.write('asd');
});

server.listen(0, common.mustCall(function() {
http.request({
port: this.address().port
})
.on('response', (res) => {
res.on('readable', () => {
res.destroy();
});
finished(res, common.mustCall(() => {
server.close();
}));
})
.end();
}));
}

0 comments on commit 897587c

Please sign in to comment.