0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

stream: don't deadlock on aborted stream

Not all streams (e.g. http.ClientRequest) will always emit
'close' after 'aborted'.

PR-URL: https://github.com/nodejs/node/pull/29376
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Robert Nagy 2019-08-30 12:13:58 +02:00 committed by Matteo Collina
parent 94c944d0d5
commit 897587c013
2 changed files with 33 additions and 0 deletions

View File

@ -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);

View File

@ -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();
}));
}