@@ -1052,7 +1052,6 @@ class QuicSocket extends EventEmitter {
1052
1052
} ) ;
1053
1053
}
1054
1054
1055
- // Called when a QuicEndpoint closes
1056
1055
[ kEndpointClose ] ( endpoint , error ) {
1057
1056
const state = this [ kInternalState ] ;
1058
1057
state . endpoints . delete ( endpoint ) ;
@@ -1064,26 +1063,15 @@ class QuicSocket extends EventEmitter {
1064
1063
}
1065
1064
} ) ;
1066
1065
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 .
1069
1068
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 ) ;
1075
1072
}
1076
1073
}
1077
1074
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
-
1087
1075
// kMaybeDestroy is called one or more times after the close() method
1088
1076
// is called. The QuicSocket will be destroyed if there are no remaining
1089
1077
// open sessions.
@@ -1463,7 +1451,20 @@ class QuicSocket extends EventEmitter {
1463
1451
for ( const session of state . sessions )
1464
1452
session . destroy ( error ) ;
1465
1453
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' ) ) ;
1467
1468
}
1468
1469
1469
1470
ref ( ) {
0 commit comments