Skip to content

Commit

Permalink
http: reafactor incoming message destroy
Browse files Browse the repository at this point in the history
Destroy the underlying socket only if it is not ready destroyed. Wait
for the stream to finish in that case.

PR-URL: nodejs#33035
Refs: nodejs#30625
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
dnlup authored and nodejs-github-bot committed Dec 17, 2020
1 parent 55e83cb commit 6120028
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
Symbol
} = primordials;

const Stream = require('stream');
const { Readable, finished } = require('stream');

const kHeaders = Symbol('kHeaders');
const kHeadersCount = Symbol('kHeadersCount');
Expand All @@ -54,7 +54,7 @@ function IncomingMessage(socket) {
};
}

Stream.Readable.call(this, streamOptions);
Readable.call(this, streamOptions);

this._readableState.readingMore = true;

Expand Down Expand Up @@ -89,8 +89,8 @@ function IncomingMessage(socket) {
// read by the user, so there's no point continuing to handle it.
this._dumped = false;
}
ObjectSetPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
ObjectSetPrototypeOf(IncomingMessage, Stream.Readable);
ObjectSetPrototypeOf(IncomingMessage.prototype, Readable.prototype);
ObjectSetPrototypeOf(IncomingMessage, Readable);

ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
get: function() {
Expand Down Expand Up @@ -168,10 +168,18 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
this.aborted = true;
this.emit('aborted');
}
if (this.socket && !this.readableEnded) {

// If aborted and the underlying socket not already destroyed,
// destroy it.
if (this.socket && !this.socket.destroyed && this.aborted) {
this.socket.destroy(err);
const cleanup = finished(this.socket, (e) => {
cleanup();
onError(this, cb, e || err);
});
} else {
onError(this, cb, err);
}
this.listenerCount('error') > 0 ? cb(err) : cb();
};

IncomingMessage.prototype._addHeaderLines = _addHeaderLines;
Expand Down Expand Up @@ -350,6 +358,10 @@ IncomingMessage.prototype._dump = function _dump() {
}
};

function onError(instance, cb, error) {
instance.listenerCount('error') > 0 ? cb(error) : cb();
}

module.exports = {
IncomingMessage,
readStart,
Expand Down

0 comments on commit 6120028

Please sign in to comment.