@@ -10,6 +10,7 @@ import type {
1010 StreamReasonToCode ,
1111} from './types' ;
1212import type { Header } from './native/types' ;
13+ import nodesEvents from 'events' ;
1314import Logger from '@matrixai/logger' ;
1415import { AbstractEvent , EventAll } from '@matrixai/events' ;
1516import {
@@ -62,6 +63,8 @@ class QUICServer {
6263 protected _closed : boolean = false ;
6364 protected _closedP : Promise < void > ;
6465 protected resolveClosedP : ( ) => void ;
66+ // Used to abort any starting connections when the server stops
67+ protected stopAbortController : AbortController | undefined ;
6568
6669 /**
6770 * Handles `EventQUICServerError`.
@@ -370,6 +373,10 @@ class QUICServer {
370373 reuseAddr ?: boolean ;
371374 ipv6Only ?: boolean ;
372375 } = { } ) {
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 ) ;
373380 let address : string ;
374381 if ( ! this . isSocketShared ) {
375382 address = utils . buildAddress ( host , port ) ;
@@ -444,6 +451,11 @@ class QUICServer {
444451 // Stop answering new connections
445452 this . socket . unsetServer ( ) ;
446453 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 ;
447459 for ( const connection of this . socket . connectionMap . serverConnections . values ( ) ) {
448460 connectionsDestroyP . push (
449461 connection . stop ( {
@@ -628,7 +640,10 @@ class QUICServer {
628640 data,
629641 remoteInfo,
630642 } ,
631- { timer : this . minIdleTimeout } ,
643+ {
644+ timer : this . minIdleTimeout ,
645+ signal : this . stopAbortController ?. signal ,
646+ } ,
632647 ) ;
633648 } catch ( e ) {
634649 connection . removeEventListener (
0 commit comments