@@ -1052,7 +1052,6 @@ class QuicSocket extends EventEmitter {
10521052 } ) ;
10531053 }
10541054
1055- // Called when a QuicEndpoint closes
10561055 [ kEndpointClose ] ( endpoint , error ) {
10571056 const state = this [ kInternalState ] ;
10581057 state . endpoints . delete ( endpoint ) ;
@@ -1064,26 +1063,15 @@ class QuicSocket extends EventEmitter {
10641063 }
10651064 } ) ;
10661065
1067- // If there are no more QuicEndpoints , the QuicSocket is no
1068- // longer usable.
1066+ // If there aren't any more endpoints , the QuicSession
1067+ // is no longer usable and needs to be destroyed .
10691068 if ( state . endpoints . size === 0 ) {
1070- for ( const session of state . sessions )
1071- session . destroy ( error ) ;
1072-
1073- if ( error ) process . nextTick ( emit . bind ( this , 'error' , error ) ) ;
1074- process . nextTick ( emit . bind ( this , 'close' ) ) ;
1069+ if ( ! this . destroyed )
1070+ return this . destroy ( error ) ;
1071+ this [ kDestroy ] ( error ) ;
10751072 }
10761073 }
10771074
1078- // kDestroy is called to actually free the QuicSocket resources and
1079- // cause the error and close events to be emitted.
1080- [ kDestroy ] ( error ) {
1081- // The QuicSocket will be destroyed once all QuicEndpoints
1082- // are destroyed. See [kEndpointClose].
1083- for ( const endpoint of this [ kInternalState ] . endpoints )
1084- endpoint . destroy ( error ) ;
1085- }
1086-
10871075 // kMaybeDestroy is called one or more times after the close() method
10881076 // is called. The QuicSocket will be destroyed if there are no remaining
10891077 // open sessions.
@@ -1463,7 +1451,20 @@ class QuicSocket extends EventEmitter {
14631451 for ( const session of state . sessions )
14641452 session . destroy ( error ) ;
14651453
1466- this [ kDestroy ] ( error ) ;
1454+ // If there aren't any QuicEndpoints to clean up, skip
1455+ // directly to the end to emit the error and close events.
1456+ if ( state . endpoints . size === 0 )
1457+ return this [ kDestroy ] ( error ) ;
1458+
1459+ // Otherwise, the QuicSocket will be destroyed once all
1460+ // QuicEndpoints are destroyed. See [kEndpointClose].
1461+ for ( const endpoint of state . endpoints )
1462+ endpoint . destroy ( error ) ;
1463+ }
1464+
1465+ [ kDestroy ] ( error ) {
1466+ if ( error ) process . nextTick ( emit . bind ( this , 'error' , error ) ) ;
1467+ process . nextTick ( emit . bind ( this , 'close' ) ) ;
14671468 }
14681469
14691470 ref ( ) {
0 commit comments