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: 'aborted' IncomingMessage should 'error' #33172

Closed
wants to merge 6 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/api/http.md
Expand Up @@ -333,9 +333,8 @@ Until the data is consumed, the `'end'` event will not fire. Also, until
the data is read it will consume memory that can eventually lead to a
'process out of memory' error.

Unlike the `request` object, if the response closes prematurely, the
`response` object does not emit an `'error'` event but instead emits the
`'aborted'` event.
For backward compatibility, `res` will only emit `'error'` if there is an
`'error'` listener registered.
Copy link
Member

Choose a reason for hiding this comment

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

Should the aborted part not be kept? If I understood correct, that's still the case if there's no error listener attached.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure, the previous paragraph was there because we didn't emit 'error'. Since this is only informative I would rather not mention something that we would like to deprecate in the future.


Node.js does not check whether Content-Length and the length of the
body which has been transmitted are equal or not.
Expand Down Expand Up @@ -2404,6 +2403,8 @@ the following events will be emitted in the following order:
* `'data'` any number of times, on the `res` object
* (connection closed here)
* `'aborted'` on the `res` object
* `'error'` on the `res` object with an error with message
`'Error: aborted'` and code `'ECONNRESET'`.
* `'close'`
* `'close'` on the `res` object

Expand Down Expand Up @@ -2432,6 +2433,8 @@ events will be emitted in the following order:
* `'data'` any number of times, on the `res` object
* (`req.destroy()` called here)
* `'aborted'` on the `res` object
* `'error'` on the `res` object with an error with message
`'Error: aborted'` and code `'ECONNRESET'`.
* `'close'`
* `'close'` on the `res` object

Expand Down Expand Up @@ -2461,6 +2464,8 @@ events will be emitted in the following order:
* (`req.abort()` called here)
* `'abort'`
* `'aborted'` on the `res` object
* `'error'` on the `res` object with an error with message
`'Error: aborted'` and code `'ECONNRESET'`.
* `'close'`
* `'close'` on the `res` object

Expand Down
4 changes: 4 additions & 0 deletions lib/_http_client.js
Expand Up @@ -413,9 +413,13 @@ function socketCloseListener() {
const res = req.res;
if (res) {
// Socket closed before we emitted 'end' below.
// TOOD(ronag): res.destroy(err)
if (!res.complete) {
res.aborted = true;
res.emit('aborted');
if (res.listenerCount('error') > 0) {
res.emit('error', connResetException('aborted'));
}
}
req.emit('close');
if (!res.aborted && res.readable) {
Expand Down
10 changes: 9 additions & 1 deletion lib/_http_server.js
Expand Up @@ -56,12 +56,16 @@ const {
getOrSetAsyncId
} = require('internal/async_hooks');
const { IncomingMessage } = require('_http_incoming');
const {
connResetException,
codes
} = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_STATUS_CODE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CHAR
} = require('internal/errors').codes;
} = codes;
const { validateInteger } = require('internal/validators');
const Buffer = require('buffer').Buffer;
const {
Expand Down Expand Up @@ -533,8 +537,12 @@ function socketOnClose(socket, state) {
function abortIncoming(incoming) {
while (incoming.length) {
const req = incoming.shift();
// TODO(ronag): req.destroy(err)
req.aborted = true;
req.emit('aborted');
if (req.listenerCount('error') > 0) {
req.emit('error', connResetException('aborted'));
}
req.emit('close');
}
// Abort socket._httpMessage ?
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-http-abort-client.js
Expand Up @@ -41,6 +41,9 @@ server.listen(0, common.mustCall(() => {
res.resume();
res.on('end', common.mustNotCall());
res.on('aborted', common.mustCall());
res.on('error', common.expectsError({
code: 'ECONNRESET'
}));
res.on('close', common.mustCall());
res.socket.on('close', common.mustCall());
}));
Expand Down
67 changes: 51 additions & 16 deletions test/parallel/test-http-aborted.js
Expand Up @@ -4,23 +4,58 @@ const common = require('../common');
const http = require('http');
const assert = require('assert');

const server = http.createServer(common.mustCall(function(req, res) {
req.on('aborted', common.mustCall(function() {
assert.strictEqual(this.aborted, true);
server.close();
{
const server = http.createServer(common.mustCall(function(req, res) {
req.on('aborted', common.mustCall(function() {
assert.strictEqual(this.aborted, true);
}));
req.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'ECONNRESET');
assert.strictEqual(err.message, 'aborted');
server.close();
}));
assert.strictEqual(req.aborted, false);
res.write('hello');
}));

server.listen(0, common.mustCall(() => {
const req = http.get({
port: server.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall((res) => {
res.on('aborted', common.mustCall(() => {
assert.strictEqual(res.aborted, true);
}));
res.on('error', common.expectsError({
code: 'ECONNRESET',
message: 'aborted'
}));
req.abort();
}));
}));
}

{
// Don't crash if no 'error' handler on server request.

const server = http.createServer(common.mustCall(function(req, res) {
req.on('aborted', common.mustCall(function() {
assert.strictEqual(this.aborted, true);
server.close();
}));
assert.strictEqual(req.aborted, false);
res.write('hello');
}));
assert.strictEqual(req.aborted, false);
res.write('hello');
}));

server.listen(0, common.mustCall(() => {
const req = http.get({
port: server.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall((res) => {
res.on('aborted', common.mustCall(() => {
assert.strictEqual(res.aborted, true);
server.listen(0, common.mustCall(() => {
const req = http.get({
port: server.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall((res) => {
res.on('aborted', common.mustCall(() => {
assert.strictEqual(res.aborted, true);
}));
req.abort();
}));
req.abort();
}));
}));
}
53 changes: 39 additions & 14 deletions test/parallel/test-http-client-aborted-event.js
Expand Up @@ -2,19 +2,44 @@
const common = require('../common');
const http = require('http');

let serverRes;
const server = http.Server(function(req, res) {
res.write('Part of my res.');
serverRes = res;
});
{
let serverRes;
const server = http.Server(function(req, res) {
res.write('Part of my res.');
serverRes = res;
});

server.listen(0, common.mustCall(function() {
http.get({
port: this.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall(function(res) {
server.close();
serverRes.destroy();
res.on('aborted', common.mustCall());
server.listen(0, common.mustCall(function() {
http.get({
port: this.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall(function(res) {
server.close();
serverRes.destroy();
res.on('aborted', common.mustCall());
res.on('error', common.expectsError({
code: 'ECONNRESET'
}));
}));
}));
}));
}

{
// Don't crash of no 'error' handler.
let serverRes;
const server = http.Server(function(req, res) {
res.write('Part of my res.');
serverRes = res;
});

server.listen(0, common.mustCall(function() {
http.get({
port: this.address().port,
headers: { connection: 'keep-alive' }
}, common.mustCall(function(res) {
server.close();
serverRes.destroy();
res.on('aborted', common.mustCall());
}));
}));
}
5 changes: 4 additions & 1 deletion test/parallel/test-http-client-spurious-aborted.js
Expand Up @@ -60,15 +60,18 @@ function download() {
res.on('end', common.mustCall(() => {
reqCountdown.dec();
}));
res.on('error', common.mustNotCall());
} else {
res.on('aborted', common.mustCall(() => {
aborted = true;
reqCountdown.dec();
writable.end();
}));
res.on('error', common.expectsError({
code: 'ECONNRESET'
}));
}

res.on('error', common.mustNotCall());
writable.on('finish', () => {
assert.strictEqual(aborted, abortRequest);
finishCountdown.dec();
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-http-outgoing-message-capture-rejection.js
Expand Up @@ -33,6 +33,9 @@ events.captureRejections = true;

req.on('response', common.mustCall((res) => {
res.on('aborted', common.mustCall());
res.on('error', common.expectsError({
code: 'ECONNRESET'
}));
res.resume();
server.close();
}));
Expand Down