@@ -150,8 +150,8 @@ function emit() {
150
150
// event. If the stream is not new, emit the 'headers' event to pass
151
151
// the block of headers on.
152
152
function onSessionHeaders ( id , cat , flags , headers ) {
153
- _unrefActive ( this ) ;
154
153
const owner = this [ kOwner ] ;
154
+ _unrefActive ( owner ) ;
155
155
debug ( `[${ sessionName ( owner [ kType ] ) } ] headers were received on ` +
156
156
`stream ${ id } : ${ cat } ` ) ;
157
157
const streams = owner [ kState ] . streams ;
@@ -265,7 +265,7 @@ function onSessionStreamClose(id, code) {
265
265
const stream = owner [ kState ] . streams . get ( id ) ;
266
266
if ( stream === undefined )
267
267
return ;
268
- _unrefActive ( this ) ;
268
+ _unrefActive ( owner ) ;
269
269
// Set the rst state for the stream
270
270
abort ( stream ) ;
271
271
const state = stream [ kState ] ;
@@ -287,14 +287,16 @@ function afterFDClose(err) {
287
287
288
288
// Called when an error event needs to be triggered
289
289
function onSessionError ( error ) {
290
- _unrefActive ( this ) ;
291
- process . nextTick ( ( ) => this [ kOwner ] . emit ( 'error' , error ) ) ;
290
+ const owner = this [ kOwner ] ;
291
+ _unrefActive ( owner ) ;
292
+ process . nextTick ( ( ) => owner . emit ( 'error' , error ) ) ;
292
293
}
293
294
294
295
// Receives a chunk of data for a given stream and forwards it on
295
296
// to the Http2Stream Duplex for processing.
296
297
function onSessionRead ( nread , buf , handle ) {
297
- const streams = this [ kOwner ] [ kState ] . streams ;
298
+ const owner = this [ kOwner ] ;
299
+ const streams = owner [ kState ] . streams ;
298
300
const id = handle . id ;
299
301
const stream = streams . get ( id ) ;
300
302
// It should not be possible for the stream to not exist at this point.
@@ -303,7 +305,7 @@ function onSessionRead(nread, buf, handle) {
303
305
'Internal HTTP/2 Failure. Stream does not exist. Please ' +
304
306
'report this as a bug in Node.js' ) ;
305
307
const state = stream [ kState ] ;
306
- _unrefActive ( this ) ; // Reset the session timeout timer
308
+ _unrefActive ( owner ) ; // Reset the session timeout timer
307
309
_unrefActive ( stream ) ; // Reset the stream timeout timer
308
310
309
311
if ( nread >= 0 && ! stream . destroyed ) {
@@ -322,7 +324,7 @@ function onSessionRead(nread, buf, handle) {
322
324
function onSettings ( ack ) {
323
325
const owner = this [ kOwner ] ;
324
326
debug ( `[${ sessionName ( owner [ kType ] ) } ] new settings received` ) ;
325
- _unrefActive ( this ) ;
327
+ _unrefActive ( owner ) ;
326
328
let event = 'remoteSettings' ;
327
329
if ( ack ) {
328
330
if ( owner [ kState ] . pendingAck > 0 )
@@ -348,7 +350,7 @@ function onPriority(id, parent, weight, exclusive) {
348
350
debug ( `[${ sessionName ( owner [ kType ] ) } ] priority advisement for stream ` +
349
351
`${ id } : \n parent: ${ parent } ,\n weight: ${ weight } ,\n` +
350
352
` exclusive: ${ exclusive } ` ) ;
351
- _unrefActive ( this ) ;
353
+ _unrefActive ( owner ) ;
352
354
const streams = owner [ kState ] . streams ;
353
355
const stream = streams . get ( id ) ;
354
356
const emitter = stream === undefined ? owner : stream ;
@@ -370,7 +372,7 @@ function onFrameError(id, type, code) {
370
372
const owner = this [ kOwner ] ;
371
373
debug ( `[${ sessionName ( owner [ kType ] ) } ] error sending frame type ` +
372
374
`${ type } on stream ${ id } , code: ${ code } ` ) ;
373
- _unrefActive ( this ) ;
375
+ _unrefActive ( owner ) ;
374
376
const streams = owner [ kState ] . streams ;
375
377
const stream = streams . get ( id ) ;
376
378
const emitter = stream !== undefined ? stream : owner ;
@@ -975,7 +977,7 @@ class Http2Session extends EventEmitter {
975
977
state . destroying = true ;
976
978
977
979
// Unenroll the timer
978
- this . setTimeout ( 0 ) ;
980
+ this . setTimeout ( 0 , sessionOnTimeout ) ;
979
981
980
982
// Shut down any still open streams
981
983
const streams = state . streams ;
@@ -2185,7 +2187,6 @@ function socketDestroy(error) {
2185
2187
const type = this [ kSession ] [ kType ] ;
2186
2188
debug ( `[${ sessionName ( type ) } ] socket destroy called` ) ;
2187
2189
delete this [ kServer ] ;
2188
- this . setTimeout ( 0 ) ;
2189
2190
// destroy the session first so that it will stop trying to
2190
2191
// send data while we close the socket.
2191
2192
this [ kSession ] . destroy ( ) ;
@@ -2247,31 +2248,6 @@ function socketOnError(error) {
2247
2248
this . destroy ( error ) ;
2248
2249
}
2249
2250
2250
- // When the socket times out on the server, attempt a graceful shutdown
2251
- // of the session.
2252
- function socketOnTimeout ( ) {
2253
- debug ( 'socket timeout' ) ;
2254
- process . nextTick ( ( ) => {
2255
- const server = this [ kServer ] ;
2256
- const session = this [ kSession ] ;
2257
- // If server or session are undefined, or session.destroyed is true
2258
- // then we're already in the process of shutting down, do nothing.
2259
- if ( server === undefined || session === undefined )
2260
- return ;
2261
- const state = session [ kState ] ;
2262
- if ( state . destroyed || state . destroying )
2263
- return ;
2264
- if ( ! server . emit ( 'timeout' , session , this ) ) {
2265
- session . shutdown (
2266
- {
2267
- graceful : true ,
2268
- errorCode : NGHTTP2_NO_ERROR
2269
- } ,
2270
- this . destroy . bind ( this ) ) ;
2271
- }
2272
- } ) ;
2273
- }
2274
-
2275
2251
// Handles the on('stream') event for a session and forwards
2276
2252
// it on to the server object.
2277
2253
function sessionOnStream ( stream , headers , flags , rawHeaders ) {
@@ -2289,15 +2265,34 @@ function sessionOnSocketError(error, socket) {
2289
2265
this [ kServer ] . emit ( 'socketError' , error , socket , this ) ;
2290
2266
}
2291
2267
2268
+ // When the session times out on the server, attempt a graceful shutdown
2269
+ function sessionOnTimeout ( ) {
2270
+ debug ( 'session timeout' ) ;
2271
+ process . nextTick ( ( ) => {
2272
+ // if destroyed or destryoing, do nothing
2273
+ if ( this [ kState ] . destroyed || this [ kState ] . destroying )
2274
+ return ;
2275
+ const server = this [ kServer ] ;
2276
+ const socket = this [ kSocket ] ;
2277
+ // If server or socket are undefined, then we're already in the process of
2278
+ // shutting down, do nothing.
2279
+ if ( server === undefined || socket === undefined )
2280
+ return ;
2281
+ if ( ! server . emit ( 'timeout' , this ) ) {
2282
+ this . shutdown (
2283
+ {
2284
+ graceful : true ,
2285
+ errorCode : NGHTTP2_NO_ERROR
2286
+ } ,
2287
+ socket . destroy . bind ( socket ) ) ;
2288
+ }
2289
+ } ) ;
2290
+ }
2291
+
2292
2292
function connectionListener ( socket ) {
2293
2293
debug ( '[server] received a connection' ) ;
2294
2294
const options = this [ kOptions ] || { } ;
2295
2295
2296
- if ( this . timeout ) {
2297
- socket . setTimeout ( this . timeout ) ;
2298
- socket . on ( 'timeout' , socketOnTimeout ) ;
2299
- }
2300
-
2301
2296
if ( socket . alpnProtocol === false || socket . alpnProtocol === 'http/1.1' ) {
2302
2297
if ( options . allowHTTP1 === true ) {
2303
2298
// Fallback to HTTP/1.1
@@ -2325,6 +2320,11 @@ function connectionListener(socket) {
2325
2320
session . on ( 'priority' , sessionOnPriority ) ;
2326
2321
session . on ( 'socketError' , sessionOnSocketError ) ;
2327
2322
2323
+ if ( this . timeout ) {
2324
+ session . setTimeout ( this . timeout ) ;
2325
+ session . on ( 'timeout' , sessionOnTimeout ) ;
2326
+ }
2327
+
2328
2328
socket [ kServer ] = this ;
2329
2329
2330
2330
process . nextTick ( emit . bind ( this , 'session' , session ) ) ;
0 commit comments