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

http: prevent aborted event when request is complete #18999

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function socketCloseListener() {
var parser = socket.parser;
if (req.res && req.res.readable) {
// Socket closed before we emitted 'end' below.
req.res.emit('aborted');
if (!req.res.complete) req.res.emit('aborted');
var res = req.res;
res.on('end', function() {
res.emit('close');
Expand Down
75 changes: 75 additions & 0 deletions test/parallel/test-http-client-spurious-aborted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const common = require('../common');
const http = require('http');
const assert = require('assert');
const { Writable } = require('stream');
const Countdown = require('../common/countdown');

const N = 2;
let abortRequest = true;

const server = http.Server(common.mustCall((req, res) => {
const headers = { 'Content-Type': 'text/plain' };
headers['Content-Length'] = 50;
const socket = res.socket;
res.writeHead(200, headers);
res.write('aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd');
if (abortRequest) {
process.nextTick(() => socket.destroy());
} else {
process.nextTick(() => res.end('eeeeeeeeee'));
}
}, N));

server.listen(0, common.mustCall(() => {
download();
}));

const finishCountdown = new Countdown(N, common.mustCall(() => {
server.close();
}));
const reqCountdown = new Countdown(N, common.mustCall());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if N was not a constant, but rather it uses a two steps approach, maybe with some helpers.

It's not clear why you are calling twice too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's running two tests - one to ensure we don't abort a good request, one to ensure we do abort a bad one. i can refactor to take out the countdown timers altogether. was trying to stay close to how other tests are constructed. if you can suggest how to change i am happy to refactor.


function download() {
const opts = {
port: server.address().port,
path: '/',
};
const req = http.get(opts);
req.on('error', common.mustNotCall());
req.on('response', (res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.connection, 'close');
let aborted = false;
const writable = new Writable({
write(chunk, encoding, callback) {
callback();
}
});
res.pipe(writable);
const _handle = res.socket._handle;
_handle._close = res.socket._handle.close;
_handle.close = function(callback) {
_handle._close();
// set readable to true even though request is complete
if (res.complete) res.readable = true;
callback();
};
res.on('end', common.mustCall(() => {
reqCountdown.dec();
}));
res.on('aborted', () => {
aborted = true;
});
res.on('error', common.mustNotCall());
writable.on('finish', () => {
assert.strictEqual(aborted, abortRequest);
finishCountdown.dec();
if (finishCountdown.remaining === 0) return;
abortRequest = false; // next one should be a good response
download();
});
});
req.end();
}