Skip to content

Commit 486cfeb

Browse files
authored
Merge pull request #103 from MatrixAI/feature-eng-328-abort-starting-connections
fix: we now abort starting connection when stopping the `QUICServer`
2 parents 448402c + 51aef5f commit 486cfeb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/QUICServer.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
StreamReasonToCode,
1111
} from './types';
1212
import type { Header } from './native/types';
13+
import nodesEvents from 'events';
1314
import Logger from '@matrixai/logger';
1415
import { AbstractEvent, EventAll } from '@matrixai/events';
1516
import {
@@ -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(

src/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class ErrorQUICServerInternal<T> extends ErrorQUICServer<T> {
9393
static description = 'QUIC Server internal error';
9494
}
9595

96+
class ErrorQUICServerStopping<T> extends ErrorQUICServer<T> {
97+
static description = 'QUIC Server is stopping';
98+
}
99+
96100
class ErrorQUICConnection<T> extends ErrorQUIC<T> {
97101
static description = 'QUIC Connection error';
98102
}
@@ -306,6 +310,7 @@ export {
306310
ErrorQUICServerSocketNotRunning,
307311
ErrorQUICServerNewConnection,
308312
ErrorQUICServerInternal,
313+
ErrorQUICServerStopping,
309314
ErrorQUICConnection,
310315
ErrorQUICConnectionStopping,
311316
ErrorQUICConnectionNotRunning,

0 commit comments

Comments
 (0)