From 3f37d29b9524caf3eac98d5aee0bb4ac08e0d05e Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Thu, 17 Aug 2023 13:46:08 -0700 Subject: [PATCH] Reintroduce timeout and is production check Summary: For dev, let's not kill the server. Also, reintroduce a timeout of 60 seconds before disconnecting. Reviewed By: antonk52 Differential Revision: D48432317 fbshipit-source-id: bac6f67101e5be481af06a5ea6ccb3b3134c4075 --- .../src/server/attachSocketServer.tsx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/desktop/flipper-server-core/src/server/attachSocketServer.tsx b/desktop/flipper-server-core/src/server/attachSocketServer.tsx index fbcada23a9a..55116c5644b 100644 --- a/desktop/flipper-server-core/src/server/attachSocketServer.tsx +++ b/desktop/flipper-server-core/src/server/attachSocketServer.tsx @@ -17,6 +17,7 @@ import { SystemError, getLogger, CompanionEventWebSocketMessage, + isProduction, } from 'flipper-common'; import {FlipperServerImpl} from '../FlipperServerImpl'; import {RawData, WebSocketServer} from 'ws'; @@ -38,6 +39,7 @@ const safe = (f: () => void) => { }; let numberOfConnectedClients = 0; +let disconnectTimeout: NodeJS.Timeout | undefined; /** * Attach and handle incoming messages from clients. @@ -239,10 +241,19 @@ export function attachSocketServer( flipperServerCompanion?.destroyAll(); if (getFlipperServerConfig().environmentInfo.isHeadlessBuild) { - if (numberOfConnectedClients === 0) { - console.info('Shutdown as no clients are currently connected'); - process.exit(0); + if (disconnectTimeout) { + clearTimeout(disconnectTimeout); } + + /** + * If, after 60 seconds, there are no more connected clients, we exit the process. + */ + disconnectTimeout = setTimeout(() => { + if (numberOfConnectedClients === 0 && isProduction()) { + console.info('Shutdown as no clients are currently connected'); + process.exit(0); + } + }, 60 * 1000); } }