fix: graceful ctrl+c shutdown and await session-end memory writes

This commit is contained in:
William Valentin
2026-02-18 11:30:47 -08:00
parent 55cde541ea
commit 21232748b9
6 changed files with 103 additions and 30 deletions
+8 -3
View File
@@ -564,11 +564,14 @@ export class GatewayServer {
}
}
// Close all WebSocket connections first
// Close all WebSocket connections first.
// Await disconnects so end-of-session summary/memory writes complete before process exit.
const disconnects: Array<Promise<void>> = [];
for (const [ws, connectionId] of this.connectionMap) {
this.sessionBridge.disconnect(connectionId);
disconnects.push(this.sessionBridge.disconnect(connectionId));
ws.close(1001, 'Server shutting down');
}
await Promise.allSettled(disconnects);
this.connectionMap.clear();
this.connectionStateMap.clear();
@@ -628,7 +631,9 @@ export class GatewayServer {
});
ws.on('close', () => {
this.sessionBridge.disconnect(connectionId);
void this.sessionBridge.disconnect(connectionId).catch((error) => {
console.warn('Session disconnect failed:', error);
});
this.connectionMap.delete(ws);
this.connectionRateMap.delete(connectionId);
this.connectionStateMap.delete(connectionId);