From 7fd1db47e491a1ea6d085e052ad44607750c0420 Mon Sep 17 00:00:00 2001 From: Ryan Block Date: Wed, 27 Mar 2024 08:43:56 -0700 Subject: [PATCH] Attempt to terminate via `SIGINT` + `SIGTERM`; should fix (or at least improve) #1479 --- changelog.md | 10 ++++++++++ src/cli/_stdin.js | 25 +++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index f384dcbb..1d8c51b0 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,16 @@ --- +## [6.0.4] 2024-03-27 + +### Fixed + +- Make a best effort attempt to terminate the process via `SIGINT` + `SIGTERM` termination signals; should fix (or at least improve) #1479 + - Sandbox running via CLI will now forcefully terminate itself after 5 seconds if the termination routine has stalled out or takes too long + - Thanks @lpsinger + @courey! + +--- + ## [6.0.3] 2024-03-25 ### Changed diff --git a/src/cli/_stdin.js b/src/cli/_stdin.js index 492a6d8f..9bba3ae3 100644 --- a/src/cli/_stdin.js +++ b/src/cli/_stdin.js @@ -4,6 +4,9 @@ let sandbox = require('../') module.exports = function handleStdin (params) { let { rehydrate, update, watcher } = params + let ending = false + let timeout + // Listen for important keystrokes emitKeypressEvents(process.stdin) if (process.stdin.isTTY) { @@ -34,15 +37,29 @@ module.exports = function handleStdin (params) { }) } if (key.sequence === '\u0003' || key.sequence === '\u0004') { - if (watcher) { - watcher.close().then(end) - } - else end() + terminate('ctrl^c') } }) + process.on('SIGINT', terminate) + process.on('SIGTERM', terminate) + + function terminate (via) { + if (ending) return + else ending = true + + timeout = setTimeout(() => { + update.err(`Process failed to gracefully exit in 5 seconds via ${via}, forcefully terminated`) + process.exit(1) + }, 5000) + + if (watcher) watcher.close().then(end) + else end() + } + function end () { sandbox.end(function (err) { + if (timeout) clearTimeout(timeout) if (err) { update.err(err) process.exit(1)