Skip to content
Permalink
Browse files

stream: don't deadlock on aborted stream

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 Aug 30, 2019
1 parent 94c944d commit 897587c0132da5b50f54409422b91a935739edb9
Showing with 33 additions and 0 deletions.
  1. +6 −0 lib/internal/streams/end-of-stream.js
  2. +27 −0 test/parallel/test-http-client-finished.js
@@ -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);
@@ -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.
You can’t perform that action at this time.