Promise catching fixes
This commit is contained in:
3 files changed
+50
-11
No files matched your search
@@ -148,14 +148,26 @@ async function main() {
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
const shutdown = () => {
|
||||
let shuttingDown = false;
|
||||
async function shutdown() {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
logger.info("shutting down");
|
||||
server.stop();
|
||||
process.exit(0);
|
||||
};
|
||||
try {
|
||||
await server.stop();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
logger.error("failed to stop server cleanly", { error });
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
||||
process.on("SIGINT", () => {
|
||||
void shutdown();
|
||||
});
|
||||
process.on("SIGTERM", () => {
|
||||
void shutdown();
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
|
||||
@@ -97,12 +97,14 @@ describe("WebSocket Server", () => {
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(async () => {
|
||||
for (const ws of connections) {
|
||||
ws.close();
|
||||
}
|
||||
connections.length = 0;
|
||||
server?.stop();
|
||||
if (server) {
|
||||
await server.stop();
|
||||
}
|
||||
server = null;
|
||||
for (const dir of tempDirs.splice(0, tempDirs.length)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
|
||||
@@ -331,7 +331,7 @@ export function createServer(options: ServerOptions) {
|
||||
});
|
||||
}
|
||||
|
||||
function stop() {
|
||||
async function stop(): Promise<void> {
|
||||
providerManager.stopAll();
|
||||
providerManager.dispose();
|
||||
|
||||
@@ -340,8 +340,33 @@ export function createServer(options: ServerOptions) {
|
||||
}
|
||||
clients.clear();
|
||||
|
||||
wss.close();
|
||||
httpServer.close();
|
||||
const isServerNotRunningError = (error: unknown): boolean => {
|
||||
if (!(error instanceof Error)) return false;
|
||||
const maybeCode = (error as NodeJS.ErrnoException).code;
|
||||
return maybeCode === "ERR_SERVER_NOT_RUNNING" || error.message.toLowerCase().includes("not running");
|
||||
};
|
||||
|
||||
const closeWebSocketServer = new Promise<void>((resolve, reject) => {
|
||||
wss.close((error) => {
|
||||
if (error && !isServerNotRunningError(error)) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const closeHttpServer = new Promise<void>((resolve, reject) => {
|
||||
httpServer.close((error) => {
|
||||
if (error && !isServerNotRunningError(error)) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
await Promise.all([closeWebSocketServer, closeHttpServer]);
|
||||
}
|
||||
|
||||
return { start, stop, httpServer };
|
||||
|
||||
Reference in new issue
Block a user