From fcba44c6d31cd7f6dd7844ae4be6e0e819a8068f Mon Sep 17 00:00:00 2001 From: David Eichmann Date: Wed, 23 Feb 2022 18:01:52 +0000 Subject: [PATCH] RTS workaround converting SIGTERM into SIGINT This is important for graceful shutdown, cleaning up resources in `bracket` statements. Fixes #1697 --- cardano-node/src/Cardano/Node/Run.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cardano-node/src/Cardano/Node/Run.hs b/cardano-node/src/Cardano/Node/Run.hs index 6474eb5b95c..b6531c9f899 100644 --- a/cardano-node/src/Cardano/Node/Run.hs +++ b/cardano-node/src/Cardano/Node/Run.hs @@ -38,6 +38,7 @@ import System.Directory (canonicalizePath, createDirectoryIfMissing, m import System.Environment (lookupEnv) #ifdef UNIX +import GHC.Weak (deRefWeak) import System.Posix.Files import qualified System.Posix.Signals as Signals import System.Posix.Types (FileMode) @@ -105,6 +106,23 @@ runNode :: PartialNodeConfiguration -> IO () runNode cmdPc = do + -- Workaround to ensure that the main thread throws an async exception on + -- receiving a SIGTERM signal. +#ifdef UNIX + -- Similar implementation to the RTS's handling of SIGINT (see GHC's + -- TopHandler.hs) + runThreadIdWk <- mkWeakThreadId =<< myThreadId + void $ Signals.installHandler + Signals.sigTERM + (Signals.CatchOnce $ do + runThreadIdMay <- deRefWeak runThreadIdWk + case runThreadIdMay of + Nothing -> return () + Just runThreadId -> killThread runThreadId + ) + Nothing +#endif + -- TODO: Remove sodiumInit: https://github.com/input-output-hk/cardano-base/issues/175 Crypto.sodiumInit