@@ -10,6 +10,7 @@ import type {
10
10
StreamReasonToCode ,
11
11
} from './types' ;
12
12
import type { Header } from './native/types' ;
13
+ import nodesEvents from 'events' ;
13
14
import Logger from '@matrixai/logger' ;
14
15
import { AbstractEvent , EventAll } from '@matrixai/events' ;
15
16
import {
@@ -62,6 +63,8 @@ class QUICServer {
62
63
protected _closed : boolean = false ;
63
64
protected _closedP : Promise < void > ;
64
65
protected resolveClosedP : ( ) => void ;
66
+ // Used to abort any starting connections when the server stops
67
+ protected stopAbortController : AbortController | undefined ;
65
68
66
69
/**
67
70
* Handles `EventQUICServerError`.
@@ -370,6 +373,10 @@ class QUICServer {
370
373
reuseAddr ?: boolean ;
371
374
ipv6Only ?: boolean ;
372
375
} = { } ) {
376
+ this . stopAbortController = new AbortController ( ) ;
377
+ // Since we have a one-to-many relationship with clients and connections,
378
+ // we want to up the warning limit on the stopAbortController
379
+ nodesEvents . setMaxListeners ( 100000 , this . stopAbortController . signal ) ;
373
380
let address : string ;
374
381
if ( ! this . isSocketShared ) {
375
382
address = utils . buildAddress ( host , port ) ;
@@ -444,6 +451,11 @@ class QUICServer {
444
451
// Stop answering new connections
445
452
this . socket . unsetServer ( ) ;
446
453
const connectionsDestroyP : Array < Promise < void > > = [ ] ;
454
+ // If force then signal for any starting connections to abort
455
+ if ( force ) {
456
+ this . stopAbortController ?. abort ( new errors . ErrorQUICServerStopping ( ) ) ;
457
+ }
458
+ this . stopAbortController = undefined ;
447
459
for ( const connection of this . socket . connectionMap . serverConnections . values ( ) ) {
448
460
connectionsDestroyP . push (
449
461
connection . stop ( {
@@ -628,7 +640,10 @@ class QUICServer {
628
640
data,
629
641
remoteInfo,
630
642
} ,
631
- { timer : this . minIdleTimeout } ,
643
+ {
644
+ timer : this . minIdleTimeout ,
645
+ signal : this . stopAbortController ?. signal ,
646
+ } ,
632
647
) ;
633
648
} catch ( e ) {
634
649
connection . removeEventListener (
0 commit comments