@@ -190,6 +190,7 @@ function ClientRequest(input, options, cb) {
190
190
}
191
191
192
192
this . _ended = false ;
193
+ this . _errorEmitted = false ;
193
194
this . res = null ;
194
195
this . aborted = false ;
195
196
this . timeoutCb = null ;
@@ -265,7 +266,7 @@ function ClientRequest(input, options, cb) {
265
266
return ;
266
267
called = true ;
267
268
if ( err ) {
268
- process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
269
+ process . nextTick ( emitError , this , err ) ;
269
270
return ;
270
271
}
271
272
this . onSocket ( socket ) ;
@@ -324,6 +325,11 @@ ClientRequest.prototype.destroy = function destroy(error) {
324
325
this . res . _dump ( ) ;
325
326
}
326
327
328
+ if ( ! error ) {
329
+ // No more errors after destroy has been called without error.
330
+ this . _errorEmitted = true ;
331
+ }
332
+
327
333
// In the event that we don't have a socket, we will pop out of
328
334
// the request queue through handling in onSocket.
329
335
if ( this . socket ) {
@@ -337,6 +343,13 @@ ClientRequest.prototype.abort = function abort() {
337
343
this . destroy ( ) ;
338
344
} ;
339
345
346
+ function emitError ( req , err ) {
347
+ if ( ! req . _errorEmitted ) {
348
+ req . _errorEmitted = true ;
349
+ req . emit ( 'error' , err ) ;
350
+ }
351
+ }
352
+
340
353
function emitAbortNT ( ) {
341
354
this . emit ( 'abort' ) ;
342
355
}
@@ -371,15 +384,10 @@ function socketCloseListener() {
371
384
res . emit ( 'close' ) ;
372
385
}
373
386
} else {
374
- if ( ! req . socket . _hadError ) {
375
- // This socket error fired before we started to
376
- // receive a response. The error needs to
377
- // fire on the request.
378
- req . socket . _hadError = true ;
379
- if ( ! req . aborted ) {
380
- req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
381
- }
382
- }
387
+ // This socket error fired before we started to
388
+ // receive a response. The error needs to
389
+ // fire on the request.
390
+ emitError ( req , connResetException ( 'socket hang up' ) ) ;
383
391
req . emit ( 'close' ) ;
384
392
}
385
393
@@ -401,12 +409,7 @@ function socketErrorListener(err) {
401
409
debug ( 'SOCKET ERROR:' , err . message , err . stack ) ;
402
410
403
411
if ( req ) {
404
- // For Safety. Some additional errors might fire later on
405
- // and we need to make sure we don't double-fire the error event.
406
- req . socket . _hadError = true ;
407
- if ( ! req . aborted ) {
408
- req . emit ( 'error' , err ) ;
409
- }
412
+ emitError ( req , err ) ;
410
413
}
411
414
412
415
// Handle any pending data
@@ -436,13 +439,10 @@ function socketOnEnd() {
436
439
const req = this . _httpMessage ;
437
440
const parser = this . parser ;
438
441
439
- if ( ! req . res && ! req . socket . _hadError ) {
442
+ if ( ! req . res ) {
440
443
// If we don't have a response then we know that the socket
441
444
// ended prematurely and we need to emit an error on the request.
442
- req . socket . _hadError = true ;
443
- if ( ! req . aborted ) {
444
- req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
445
- }
445
+ emitError ( req , connResetException ( 'socket hang up' ) ) ;
446
446
}
447
447
if ( parser ) {
448
448
parser . finish ( ) ;
@@ -464,10 +464,7 @@ function socketOnData(d) {
464
464
debug ( 'parse error' , ret ) ;
465
465
freeParser ( parser , req , socket ) ;
466
466
socket . destroy ( ) ;
467
- req . socket . _hadError = true ;
468
- if ( ! req . aborted ) {
469
- req . emit ( 'error' , ret ) ;
470
- }
467
+ emitError ( req , ret ) ;
471
468
} else if ( parser . incoming && parser . incoming . upgrade ) {
472
469
// Upgrade (if status code 101) or CONNECT
473
470
var bytesParsed = ret ;
@@ -725,10 +722,12 @@ function onSocketNT(req, socket) {
725
722
socket . destroy ( req . _destroyError ) ;
726
723
} else {
727
724
if ( req . _destroyError ) {
728
- req . emit ( 'error' , req . _destroyError ) ;
725
+ emitError ( req , req . _destroyError ) ;
729
726
}
727
+ req . emit ( 'close' ) ;
730
728
socket . emit ( 'free' ) ;
731
729
}
730
+ req . _destroyError = null ;
732
731
} else {
733
732
tickOnSocket ( req , socket ) ;
734
733
}
0 commit comments