Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: don't deadlock on aborted stream #29376

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

{
// abort before finished

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

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