From 6893058913f251260b7ca2fb5577afbd353743e9 Mon Sep 17 00:00:00 2001 From: bettercalljason <11984468+bettercalljason@users.noreply.github.com> Date: Fri, 3 May 2024 16:01:46 +0000 Subject: [PATCH] fix(ws): close existing connections Existing connections must be closed manually. Otherwise, application shutdown hangs when shutdown hooks are enabled and there are existing connections. --- packages/platform-ws/adapters/ws-adapter.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/platform-ws/adapters/ws-adapter.ts b/packages/platform-ws/adapters/ws-adapter.ts index 770a5f9963a..4111e894276 100644 --- a/packages/platform-ws/adapters/ws-adapter.ts +++ b/packages/platform-ws/adapters/ws-adapter.ts @@ -158,6 +158,16 @@ export class WsAdapter extends AbstractWsAdapter { client.on(CLOSE_EVENT, callback); } + public async close(server: any) { + const closeEventSignal = new Promise((resolve, reject) => + server.close(err => (err ? reject(err) : resolve(undefined))), + ); + for (const ws of server.clients) { + ws.terminate(); + } + await closeEventSignal; + } + public async dispose() { const closeEventSignals = Array.from(this.httpServersRegistry) .filter(([port]) => port !== UNDERLYING_HTTP_SERVER_PORT)