Skip to content

Terminate child processes on sigINT/sigTERM #7757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ library
filepath >= 1.3.0.1 && < 1.5,
pretty >= 1.1.1 && < 1.2,
process >= 1.1.0.2 && < 1.7,
time >= 1.4.0.1 && < 1.12
time >= 1.4.0.1 && < 1.12,
signal

if flag(bundled-binary-generic)
build-depends: binary >= 0.5.1.1 && < 0.7
Expand Down
15 changes: 14 additions & 1 deletion Cabal/src/Distribution/Compat/Process.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ module Distribution.Compat.Process (
rawSystem,
-- * Additions
enableProcessJobs,
cleanUpProcessOnInterrupt,
) where

import Control.Monad (forM_)
import System.Exit (ExitCode (..))
import System.IO (Handle)
import System.Signal

import System.Process (CreateProcess, ProcessHandle)
import qualified System.Process as Process
Expand Down Expand Up @@ -54,13 +57,23 @@ createProcess = Process.createProcess . enableProcessJobs
rawSystem :: String -> [String] -> IO ExitCode
rawSystem cmd args = do
#if MIN_VERSION_process(1,2,0)
(_,_,_,p) <- createProcess (Process.proc cmd args) { Process.delegate_ctlc = True }
pp@(_, _, _, p) <- createProcess (Process.proc cmd args) { Process.delegate_ctlc = True }
cleanUpProcessOnInterrupt pp
waitForProcess p
#else
-- With very old 'process', just do its rawSystem
Process.rawSystem cmd args
#endif


-- | Installs signal handlers to clean up the process on interrupt of:
-- - '[sigINT, sigTERM]'
cleanUpProcessOnInterrupt :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
cleanUpProcessOnInterrupt (stdin, stdout, stderr, p) =
forM_ [sigINT, sigTERM] $ \sig ->
installHandler sig (\_ -> Process.cleanupProcess (stdin, stdout, stderr, p))


-- | 'System.Process.runInteractiveProcess' with process jobs enabled when
-- appropriate. See 'enableProcessJobs'.
runInteractiveProcess
Expand Down
7 changes: 5 additions & 2 deletions Cabal/src/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ import qualified Control.Exception as Exception
import Foreign.C.Error (Errno (..), ePIPE)
import Data.Time.Clock.POSIX (getPOSIXTime, POSIXTime)
import Numeric (showFFloat)
import Distribution.Compat.Process (createProcess, rawSystem, runInteractiveProcess)
import Distribution.Compat.Process (createProcess, rawSystem, runInteractiveProcess, cleanUpProcessOnInterrupt)
import System.Process
( ProcessHandle
, showCommandForUser, waitForProcess)
Expand Down Expand Up @@ -800,8 +800,10 @@ rawSystemIOWithEnv :: Verbosity
-> Maybe Handle -- ^ stderr
-> IO ExitCode
rawSystemIOWithEnv verbosity path args mcwd menv inp out err = withFrozenCallStack $ do
(_,_,_,ph) <- createProcessWithEnv verbosity path args mcwd menv
pp@(_,_,_,ph) <- createProcessWithEnv verbosity path args mcwd menv
(mbToStd inp) (mbToStd out) (mbToStd err)
cleanUpProcessOnInterrupt pp

exitcode <- waitForProcess ph
unless (exitcode == ExitSuccess) $ do
debug verbosity $ path ++ " returned " ++ show exitcode
Expand Down Expand Up @@ -897,6 +899,7 @@ rawSystemStdInOut verbosity path args mcwd menv input _ = withFrozenCallStack $
(runInteractiveProcess path args mcwd menv)
(\(inh,outh,errh,_) -> hClose inh >> hClose outh >> hClose errh)
$ \(inh,outh,errh,pid) -> do
cleanUpProcessOnInterrupt (Just inh,Just outh,Just errh,pid)

-- output mode depends on what the caller wants
-- but the errors are always assumed to be text (in the current locale)
Expand Down