@@ -36,7 +36,7 @@ const assert = require('internal/assert');
36
36
const EE = require ( 'events' ) ;
37
37
const Stream = require ( 'stream' ) ;
38
38
const internalUtil = require ( 'internal/util' ) ;
39
- const { kOutHeaders, utcDate, kNeedDrain } = require ( 'internal/http' ) ;
39
+ const { kOutHeaders, utcDate, kNeedDrain, kClosed } = require ( 'internal/http' ) ;
40
40
const { Buffer } = require ( 'buffer' ) ;
41
41
const common = require ( '_http_common' ) ;
42
42
const checkIsHttpToken = common . _checkIsHttpToken ;
@@ -117,6 +117,7 @@ function OutgoingMessage() {
117
117
this . finished = false ;
118
118
this . _headerSent = false ;
119
119
this [ kCorked ] = 0 ;
120
+ this [ kClosed ] = false ;
120
121
121
122
this . socket = null ;
122
123
this . _header = null ;
@@ -663,7 +664,9 @@ function onError(msg, err, callback) {
663
664
664
665
function emitErrorNt ( msg , err , callback ) {
665
666
callback ( err ) ;
666
- if ( typeof msg . emit === 'function' ) msg . emit ( 'error' , err ) ;
667
+ if ( typeof msg . emit === 'function' && ! msg [ kClosed ] ) {
668
+ msg . emit ( 'error' , err ) ;
669
+ }
667
670
}
668
671
669
672
function write_ ( msg , chunk , encoding , callback , fromEnd ) {
@@ -690,7 +693,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
690
693
}
691
694
692
695
if ( err ) {
693
- onError ( msg , err , callback ) ;
696
+ if ( ! msg . destroyed ) {
697
+ onError ( msg , err , callback ) ;
698
+ } else {
699
+ process . nextTick ( callback , err ) ;
700
+ }
694
701
return false ;
695
702
}
696
703
@@ -780,7 +787,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
780
787
}
781
788
782
789
if ( chunk ) {
783
- write_ ( this , chunk , encoding , null , true ) ;
790
+ if ( ! write_ ( this , chunk , encoding , null , true ) ) {
791
+ return ;
792
+ }
784
793
} else if ( this . finished ) {
785
794
if ( typeof callback === 'function' ) {
786
795
if ( ! this . writableFinished ) {
0 commit comments