Skip to content

Commit 397e31e

Browse files
dnluptargos
authored andcommitted
http: reafactor incoming message destroy
Destroy the underlying socket only if it is not ready destroyed. Wait for the stream to finish in that case. PR-URL: #33035 Refs: #30625 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 25d7e90 commit 397e31e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/_http_incoming.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727
Symbol
2828
} = primordials;
2929

30-
const Stream = require('stream');
30+
const { Readable, finished } = require('stream');
3131

3232
const kHeaders = Symbol('kHeaders');
3333
const kHeadersCount = Symbol('kHeadersCount');
@@ -54,7 +54,7 @@ function IncomingMessage(socket) {
5454
};
5555
}
5656

57-
Stream.Readable.call(this, streamOptions);
57+
Readable.call(this, streamOptions);
5858

5959
this._readableState.readingMore = true;
6060

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

9595
ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
9696
get: function() {
@@ -168,10 +168,18 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
168168
this.aborted = true;
169169
this.emit('aborted');
170170
}
171-
if (this.socket && !this.readableEnded) {
171+
172+
// If aborted and the underlying socket not already destroyed,
173+
// destroy it.
174+
if (this.socket && !this.socket.destroyed && this.aborted) {
172175
this.socket.destroy(err);
176+
const cleanup = finished(this.socket, (e) => {
177+
cleanup();
178+
onError(this, cb, e || err);
179+
});
180+
} else {
181+
onError(this, cb, err);
173182
}
174-
this.listenerCount('error') > 0 ? cb(err) : cb();
175183
};
176184

177185
IncomingMessage.prototype._addHeaderLines = _addHeaderLines;
@@ -350,6 +358,10 @@ IncomingMessage.prototype._dump = function _dump() {
350358
}
351359
};
352360

361+
function onError(instance, cb, error) {
362+
instance.listenerCount('error') > 0 ? cb(error) : cb();
363+
}
364+
353365
module.exports = {
354366
IncomingMessage,
355367
readStart,

0 commit comments

Comments
 (0)