@@ -670,7 +670,7 @@ function finishSessionDestroy(socket) {
670670 debug ( `[${ sessionName ( this [ kType ] ) } ] nghttp2session handle destroyed` ) ;
671671 }
672672
673- this . emit ( 'close' ) ;
673+ process . nextTick ( emit . bind ( this , 'close' ) ) ;
674674 debug ( `[${ sessionName ( this [ kType ] ) } ] nghttp2session destroyed` ) ;
675675}
676676
@@ -953,6 +953,9 @@ class Http2Session extends EventEmitter {
953953 state . destroyed = true ;
954954 state . destroying = false ;
955955
956+ if ( this [ kHandle ] !== undefined )
957+ this [ kHandle ] . destroying ( ) ;
958+
956959 setImmediate ( finishSessionDestroy . bind ( this , socket ) ) ;
957960 }
958961
@@ -1985,6 +1988,7 @@ function socketDestroy(error) {
19851988 const type = this [ kSession ] [ kType ] ;
19861989 debug ( `[${ sessionName ( type ) } ] socket destroy called` ) ;
19871990 delete this [ kServer ] ;
1991+ this . removeListener ( 'timeout' , socketOnTimeout ) ;
19881992 // destroy the session first so that it will stop trying to
19891993 // send data while we close the socket.
19901994 this [ kSession ] . destroy ( ) ;
@@ -2046,14 +2050,18 @@ function socketOnError(error) {
20462050 this . destroy ( error ) ;
20472051}
20482052
2049- // When the socket times out, attempt a graceful shutdown
2050- // of the session
2053+ // When the socket times out on the server , attempt a graceful shutdown
2054+ // of the session.
20512055function socketOnTimeout ( ) {
20522056 debug ( 'socket timeout' ) ;
20532057 const server = this [ kServer ] ;
2054- // server can be null if the socket is a client
2055- if ( server === undefined || ! server . emit ( 'timeout' , this ) ) {
2056- this [ kSession ] . shutdown (
2058+ const session = this [ kSession ] ;
2059+ // If server or session are undefined, then we're already in the process of
2060+ // shutting down, do nothing.
2061+ if ( server === undefined || session === undefined )
2062+ return ;
2063+ if ( ! server . emit ( 'timeout' , session , this ) ) {
2064+ session . shutdown (
20572065 {
20582066 graceful : true ,
20592067 errorCode : NGHTTP2_NO_ERROR
@@ -2105,6 +2113,7 @@ function connectionListener(socket) {
21052113 socket . on ( 'resume' , socketOnResume ) ;
21062114 socket . on ( 'pause' , socketOnPause ) ;
21072115 socket . on ( 'drain' , socketOnDrain ) ;
2116+ socket . on ( 'close' , socketOnClose ) ;
21082117
21092118 // Set up the Session
21102119 const session = new ServerHttp2Session ( options , socket , this ) ;
@@ -2197,6 +2206,13 @@ function setupCompat(ev) {
21972206 }
21982207}
21992208
2209+ function socketOnClose ( hadError ) {
2210+ const session = this [ kSession ] ;
2211+ if ( session !== undefined && ! session . destroyed ) {
2212+ session . destroy ( ) ;
2213+ }
2214+ }
2215+
22002216// If the session emits an error, forward it to the socket as a sessionError;
22012217// failing that, destroy the session, remove the listener and re-emit the error
22022218function clientSessionOnError ( error ) {
@@ -2244,6 +2260,7 @@ function connect(authority, options, listener) {
22442260 socket . on ( 'resume' , socketOnResume ) ;
22452261 socket . on ( 'pause' , socketOnPause ) ;
22462262 socket . on ( 'drain' , socketOnDrain ) ;
2263+ socket . on ( 'close' , socketOnClose ) ;
22472264
22482265 const session = new ClientHttp2Session ( options , socket ) ;
22492266
0 commit comments