@@ -670,7 +670,7 @@ function finishSessionDestroy(socket) {
670
670
debug ( `[${ sessionName ( this [ kType ] ) } ] nghttp2session handle destroyed` ) ;
671
671
}
672
672
673
- this . emit ( 'close' ) ;
673
+ process . nextTick ( emit . bind ( this , 'close' ) ) ;
674
674
debug ( `[${ sessionName ( this [ kType ] ) } ] nghttp2session destroyed` ) ;
675
675
}
676
676
@@ -953,6 +953,9 @@ class Http2Session extends EventEmitter {
953
953
state . destroyed = true ;
954
954
state . destroying = false ;
955
955
956
+ if ( this [ kHandle ] !== undefined )
957
+ this [ kHandle ] . destroying ( ) ;
958
+
956
959
setImmediate ( finishSessionDestroy . bind ( this , socket ) ) ;
957
960
}
958
961
@@ -1985,6 +1988,7 @@ function socketDestroy(error) {
1985
1988
const type = this [ kSession ] [ kType ] ;
1986
1989
debug ( `[${ sessionName ( type ) } ] socket destroy called` ) ;
1987
1990
delete this [ kServer ] ;
1991
+ this . removeListener ( 'timeout' , socketOnTimeout ) ;
1988
1992
// destroy the session first so that it will stop trying to
1989
1993
// send data while we close the socket.
1990
1994
this [ kSession ] . destroy ( ) ;
@@ -2046,14 +2050,18 @@ function socketOnError(error) {
2046
2050
this . destroy ( error ) ;
2047
2051
}
2048
2052
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.
2051
2055
function socketOnTimeout ( ) {
2052
2056
debug ( 'socket timeout' ) ;
2053
2057
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 (
2057
2065
{
2058
2066
graceful : true ,
2059
2067
errorCode : NGHTTP2_NO_ERROR
@@ -2105,6 +2113,7 @@ function connectionListener(socket) {
2105
2113
socket . on ( 'resume' , socketOnResume ) ;
2106
2114
socket . on ( 'pause' , socketOnPause ) ;
2107
2115
socket . on ( 'drain' , socketOnDrain ) ;
2116
+ socket . on ( 'close' , socketOnClose ) ;
2108
2117
2109
2118
// Set up the Session
2110
2119
const session = new ServerHttp2Session ( options , socket , this ) ;
@@ -2197,6 +2206,13 @@ function setupCompat(ev) {
2197
2206
}
2198
2207
}
2199
2208
2209
+ function socketOnClose ( hadError ) {
2210
+ const session = this [ kSession ] ;
2211
+ if ( session !== undefined && ! session . destroyed ) {
2212
+ session . destroy ( ) ;
2213
+ }
2214
+ }
2215
+
2200
2216
// If the session emits an error, forward it to the socket as a sessionError;
2201
2217
// failing that, destroy the session, remove the listener and re-emit the error
2202
2218
function clientSessionOnError ( error ) {
@@ -2244,6 +2260,7 @@ function connect(authority, options, listener) {
2244
2260
socket . on ( 'resume' , socketOnResume ) ;
2245
2261
socket . on ( 'pause' , socketOnPause ) ;
2246
2262
socket . on ( 'drain' , socketOnDrain ) ;
2263
+ socket . on ( 'close' , socketOnClose ) ;
2247
2264
2248
2265
const session = new ClientHttp2Session ( options , socket ) ;
2249
2266
0 commit comments