From d1bdbfdcd1aac4aecdf0ae568598ea39c9f23b61 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 3 Aug 2020 11:41:45 +0100 Subject: [PATCH] Debounce kill signals --- CHANGELOG.md | 1 + src/emulator/commandUtils.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad25a4f7d7..f32268ff4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,3 +5,4 @@ - Fixes issue where database emulator did not properly load initial rules (#2483). - Allow starting the UI with `emulators:exec` using the `--ui` flag. - Fixes issue where multiple CLI instances compete for the same log (#2464). +- Fixes issue where emulators run from `npm` scripts could not be shut down cleanly (#2507). diff --git a/src/emulator/commandUtils.ts b/src/emulator/commandUtils.ts index d4141574dc6..f7970fe5cd7 100644 --- a/src/emulator/commandUtils.ts +++ b/src/emulator/commandUtils.ts @@ -215,10 +215,22 @@ function processKillSignal( rej: (value?: unknown) => void, options: any ): SignalsListener { + let lastSignal = new Date().getTime(); let signalCount = 0; return async () => { try { + const now = new Date().getTime(); + const diff = now - lastSignal; + if (diff < 100) { + // If we got a signal twice in 100ms it likely was not an intentional human action. + // It could be a shaky MacBook keyboard or a known issue with "npm" scripts and signals. + logger.debug(`Ignoring signal ${signal} due to short delay of ${diff}ms`); + return; + } + signalCount = signalCount + 1; + lastSignal = now; + const signalDisplay = signal === "SIGINT" ? `SIGINT (Ctrl-C)` : signal; logger.debug(`Received signal ${signalDisplay} ${signalCount}`); logger.info(" "); // to not indent the log with the possible Ctrl-C char