From 19c6d34fc23804f3903316fbc607caf8166892ed Mon Sep 17 00:00:00 2001 From: Rodney Lorrimar Date: Mon, 18 Mar 2019 15:26:07 +1000 Subject: [PATCH] launcher: When terminated, ensure child processes are cleaned up --- .weeder.yaml | 15 +++++++++++++++ app/Cardano/Launcher.hs | 16 +++++++++++++--- app/Cardano/Launcher/POSIX.hs | 33 +++++++++++++++++++++++++++++++++ app/Cardano/Launcher/Windows.hs | 15 +++++++++++++++ app/launcher/Main.hs | 7 ++++++- cardano-wallet.cabal | 13 +++++++++++++ 6 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 app/Cardano/Launcher/POSIX.hs create mode 100644 app/Cardano/Launcher/Windows.hs diff --git a/.weeder.yaml b/.weeder.yaml index cfd796e6bec..0ab51f2d230 100644 --- a/.weeder.yaml +++ b/.weeder.yaml @@ -10,3 +10,18 @@ - message: - name: Module reused between components - module: Cardano.Launcher + - section: + - name: exe:cardano-wallet-launcher test:unit + - message: + - name: Module reused between components + - module: Cardano.Launcher.POSIX + - section: + - name: exe:cardano-wallet-launcher + - message: + - name: Module not compiled + - module: Cardano.Launcher.Windows + - section: + - name: test:unit + - message: + - name: Module not compiled + - module: Cardano.Launcher.Windows diff --git a/app/Cardano/Launcher.hs b/app/Cardano/Launcher.hs index 9aa446daa4a..aaaf81edf23 100644 --- a/app/Cardano/Launcher.hs +++ b/app/Cardano/Launcher.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE CPP #-} + -- | -- Copyright: © 2018-2019 IOHK -- License: MIT @@ -10,6 +12,7 @@ module Cardano.Launcher ( Command (..) , ProcessHasExited(..) , launch + , installSignalHandlers ) where import Prelude @@ -27,6 +30,13 @@ import System.Exit import System.Process ( proc, waitForProcess, withCreateProcess ) +#ifdef mingw32_HOST_OS +import Cardano.Launcher.Windows + ( installSignalHandlers ) +#else +import Cardano.Launcher.POSIX + ( installSignalHandlers ) +#endif data Command = Command { cmdName :: String @@ -73,6 +83,6 @@ launch cmds = do throwIO $ ProcessHasExited name code case res of Left e -> return e - Right _ -> error - "Unreachable. Supervising threads should never finish. \ - \They should stay running or throw @ProcessHasExited@." + Right _ -> error $ + "Unreachable. Supervising threads should never finish. " <> + "They should stay running or throw @ProcessHasExited@." diff --git a/app/Cardano/Launcher/POSIX.hs b/app/Cardano/Launcher/POSIX.hs new file mode 100644 index 00000000000..6cee08cd596 --- /dev/null +++ b/app/Cardano/Launcher/POSIX.hs @@ -0,0 +1,33 @@ +-- | +-- Copyright: © 2018-2019 IOHK +-- License: MIT +-- Portability: POSIX +-- + +module Cardano.Launcher.POSIX + ( installSignalHandlers + ) where + +import Prelude + +import Control.Monad + ( void ) +import Say + ( sayErr ) +import System.Posix.Signals + ( Handler (..) + , installHandler + , keyboardSignal + , raiseSignal + , softwareTermination + ) + +-- | Convert any SIGTERM received to SIGINT, for which the runtime system has +-- handlers that will correctly clean up sub-processes. +installSignalHandlers :: IO () +installSignalHandlers = void $ + installHandler softwareTermination termHandler Nothing + where + termHandler = CatchOnce $ do + sayErr "Terminated by signal." + raiseSignal keyboardSignal diff --git a/app/Cardano/Launcher/Windows.hs b/app/Cardano/Launcher/Windows.hs new file mode 100644 index 00000000000..50e0a01e127 --- /dev/null +++ b/app/Cardano/Launcher/Windows.hs @@ -0,0 +1,15 @@ +-- | +-- Copyright: © 2018-2019 IOHK +-- License: MIT +-- Portability: Windows +-- + +module Cardano.Launcher.Windows + ( installSignalHandlers + ) where + +import Prelude + +-- | Stub function for windows. +installSignalHandlers :: IO () +installSignalHandlers = pure () diff --git a/app/launcher/Main.hs b/app/launcher/Main.hs index a1a77c2f8ea..fe4ea85a13e 100644 --- a/app/launcher/Main.hs +++ b/app/launcher/Main.hs @@ -8,7 +8,11 @@ import Prelude import Cardano.CLI ( Network, Port, decode, encode, getArg ) import Cardano.Launcher - ( Command (Command), ProcessHasExited (ProcessHasExited), launch ) + ( Command (Command) + , ProcessHasExited (ProcessHasExited) + , installSignalHandlers + , launch + ) import Control.Concurrent ( threadDelay ) import Control.Monad @@ -59,6 +63,7 @@ main = do network <- getArg args cli (longOption "network") decode sayErr "Starting..." + installSignalHandlers let commands = [ nodeHttpBridgeOn bridgePort , walletOn walletPort bridgePort network diff --git a/cardano-wallet.cabal b/cardano-wallet.cabal index 8f96943ee52..3f5a9c551cd 100644 --- a/cardano-wallet.cabal +++ b/cardano-wallet.cabal @@ -130,6 +130,12 @@ test-suite unit Cardano.Wallet.MnemonicSpec Cardano.Wallet.PrimitiveSpec Cardano.WalletSpec + if os(windows) + build-depends: Win32 + other-modules: Cardano.Launcher.Windows + else + build-depends: unix, say + other-modules: Cardano.Launcher.POSIX executable cardano-wallet-server @@ -183,6 +189,13 @@ executable cardano-wallet-launcher other-modules: Cardano.CLI Cardano.Launcher + if os(windows) + build-depends: Win32 + other-modules: Cardano.Launcher.Windows + else + build-depends: unix + other-modules: Cardano.Launcher.POSIX + main-is: Main.hs