From e8aad89b0b22f6aec6d6aab27aab3cac09589b81 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Fri, 6 Oct 2023 03:26:14 -0700 Subject: [PATCH] Set unresponsive timeout Summary: Set a timeout period to become ready. It is better to kill the server if not ready, than keeping it alive. Reviewed By: antonk52 Differential Revision: D50004605 fbshipit-source-id: be1d2c022c634ece059fc03eb6faea695431ba95 --- .../src/server/startServer.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/desktop/flipper-server-core/src/server/startServer.tsx b/desktop/flipper-server-core/src/server/startServer.tsx index e6afbdc39..ea42c2d95 100644 --- a/desktop/flipper-server-core/src/server/startServer.tsx +++ b/desktop/flipper-server-core/src/server/startServer.tsx @@ -20,7 +20,7 @@ import {FlipperServerImpl} from '../FlipperServerImpl'; import {FlipperServerCompanionEnv} from 'flipper-server-companion'; import {validateAuthToken} from '../app-connectivity/certificate-exchange/certificate-utils'; import {tracker} from '../tracker'; -import {EnvironmentInfo} from 'flipper-common'; +import {EnvironmentInfo, isProduction} from 'flipper-common'; type Config = { port: number; @@ -85,6 +85,13 @@ const verifyAuthToken = (req: http.IncomingMessage): boolean => { let isReady = false; let isReadyWaitable: Promise | undefined; +/** + * Time to wait until server becomes ready to accept incoming connections. + * If within 30 seconds it is not ready, the server is considered unresponsive + * and must be terminated. + */ +const timeoutSeconds = 30; + /** * Orchestrates the creation of the HTTP server, proxy, and WS server. * @param config Server configuration. @@ -99,6 +106,15 @@ export async function startServer( socket: WebSocketServer; readyForIncomingConnections: ReadyForConnections; }> { + setTimeout(() => { + if (!isReady && isProduction()) { + console.error( + `[flipper-server] Unable to become ready within ${timeoutSeconds} seconds, exit`, + ); + process.exit(1); + } + }, timeoutSeconds * 1000); + return await startHTTPServer(config, environmentInfo); }