@@ -533,8 +533,8 @@ function socketOnData(d) {
533533 socket . removeListener ( 'end' , socketOnEnd ) ;
534534 socket . removeListener ( 'drain' , ondrain ) ;
535535
536- if ( req . timeoutCb )
537- socket . removeListener ( 'timeout' , req . timeoutCb ) ;
536+ if ( req . timeoutCb ) socket . removeListener ( 'timeout' , req . timeoutCb ) ;
537+ socket . removeListener ( 'timeout' , responseOnTimeout ) ;
538538
539539 parser . finish ( ) ;
540540 freeParser ( parser , req , socket ) ;
@@ -644,6 +644,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
644644 // Add our listener first, so that we guarantee socket cleanup
645645 res . on ( 'end' , responseOnEnd ) ;
646646 req . on ( 'prefinish' , requestOnPrefinish ) ;
647+ socket . on ( 'timeout' , responseOnTimeout ) ;
647648
648649 // If the user did not listen for the 'response' event, then they
649650 // can't possibly read the data, so we ._dump() it into the void
@@ -692,15 +693,16 @@ function responseKeepAlive(req) {
692693
693694function responseOnEnd ( ) {
694695 const req = this . req ;
696+ const socket = req . socket ;
695697
696- if ( req . socket && req . timeoutCb ) {
697- req . socket . removeListener ( 'timeout' , emitRequestTimeout ) ;
698+ if ( socket ) {
699+ if ( req . timeoutCb ) socket . removeListener ( 'timeout' , emitRequestTimeout ) ;
700+ socket . removeListener ( 'timeout' , responseOnTimeout ) ;
698701 }
699702
700703 req . _ended = true ;
701704
702705 if ( ! req . shouldKeepAlive ) {
703- const socket = req . socket ;
704706 if ( socket . writable ) {
705707 debug ( 'AGENT socket.destroySoon()' ) ;
706708 if ( typeof socket . destroySoon === 'function' )
@@ -719,6 +721,14 @@ function responseOnEnd() {
719721 }
720722}
721723
724+ function responseOnTimeout ( ) {
725+ const req = this . _httpMessage ;
726+ if ( ! req ) return ;
727+ const res = req . res ;
728+ if ( ! res ) return ;
729+ res . emit ( 'timeout' ) ;
730+ }
731+
722732function requestOnPrefinish ( ) {
723733 const req = this ;
724734
0 commit comments