@@ -27,7 +27,7 @@ const {
27
27
Symbol
28
28
} = primordials ;
29
29
30
- const Stream = require ( 'stream' ) ;
30
+ const { Readable , finished } = require ( 'stream' ) ;
31
31
32
32
const kHeaders = Symbol ( 'kHeaders' ) ;
33
33
const kHeadersCount = Symbol ( 'kHeadersCount' ) ;
@@ -54,7 +54,7 @@ function IncomingMessage(socket) {
54
54
} ;
55
55
}
56
56
57
- Stream . Readable . call ( this , streamOptions ) ;
57
+ Readable . call ( this , streamOptions ) ;
58
58
59
59
this . _readableState . readingMore = true ;
60
60
@@ -89,8 +89,8 @@ function IncomingMessage(socket) {
89
89
// read by the user, so there's no point continuing to handle it.
90
90
this . _dumped = false ;
91
91
}
92
- ObjectSetPrototypeOf ( IncomingMessage . prototype , Stream . Readable . prototype ) ;
93
- ObjectSetPrototypeOf ( IncomingMessage , Stream . Readable ) ;
92
+ ObjectSetPrototypeOf ( IncomingMessage . prototype , Readable . prototype ) ;
93
+ ObjectSetPrototypeOf ( IncomingMessage , Readable ) ;
94
94
95
95
ObjectDefineProperty ( IncomingMessage . prototype , 'connection' , {
96
96
get : function ( ) {
@@ -168,10 +168,18 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
168
168
this . aborted = true ;
169
169
this . emit ( 'aborted' ) ;
170
170
}
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 ) {
172
175
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 ) ;
173
182
}
174
- this . listenerCount ( 'error' ) > 0 ? cb ( err ) : cb ( ) ;
175
183
} ;
176
184
177
185
IncomingMessage . prototype . _addHeaderLines = _addHeaderLines ;
@@ -350,6 +358,10 @@ IncomingMessage.prototype._dump = function _dump() {
350
358
}
351
359
} ;
352
360
361
+ function onError ( instance , cb , error ) {
362
+ instance . listenerCount ( 'error' ) > 0 ? cb ( error ) : cb ( ) ;
363
+ }
364
+
353
365
module . exports = {
354
366
IncomingMessage,
355
367
readStart,
0 commit comments