From 0b5e46fc7589efe001836afb0dd310b8ca4f8582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Fri, 20 Nov 2026 16:30:03 +0100 Subject: Kill remaining processes started by task recipe Changelog: Make sure to terminate processes started by task recipe after it finishes. --- src/Job.hs | 15 ++++-- test/asset/run/background.yaml | 14 +++++ test/asset/run/forever.yaml | 7 +++ test/script/run.et | 120 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 test/asset/run/background.yaml create mode 100644 test/asset/run/forever.yaml diff --git a/src/Job.hs b/src/Job.hs index 45cc827..b40f450 100644 --- a/src/Job.hs +++ b/src/Job.hs @@ -48,6 +48,7 @@ import System.Exit import System.FilePath import System.FilePath.Glob import System.IO +import System.IO.Error import System.IO.Temp import System.Posix.Signals import System.Process @@ -529,20 +530,24 @@ runJob job uses checkoutPath jdir = do Right script -> do sh <- fromMaybe "/bin/sh" <$> liftIO (lookupEnv "SHELL") return ( proc sh [], script ) - (Just hin, _, _, hp) <- liftIO $ createProcess_ "" p + ( Just hin, _, _, hp ) <- liftIO $ createProcess_ "" p { cwd = Just checkoutPath , std_in = CreatePipe , std_out = UseHandle logs , std_err = UseHandle logs + , create_group = True } + Just pid <- liftIO $ getPid hp liftIO $ void $ forkIO $ do T.hPutStr hin input hClose hin - liftIO (waitForProcess hp) >>= \case + let killCreatedProcessGroup = do + handleJust (\case e | isDoesNotExistError e -> Just (); _ -> Nothing) return $ do + signalProcessGroup sigKILL pid + waitForProcess hp + liftIO (waitForProcess hp `finally` killCreatedProcessGroup) >>= \case ExitSuccess -> return () - ExitFailure n - | fromIntegral n == -sigINT -> throwError JobCancelled - | otherwise -> throwError JobFailed + ExitFailure _ -> throwError JobFailed artifacts <- forM (jobArtifacts job) $ \( name@(ArtifactName tname), pathPattern ) -> do let adir = jdir "artifacts" T.unpack tname diff --git a/test/asset/run/background.yaml b/test/asset/run/background.yaml new file mode 100644 index 0000000..94d32cb --- /dev/null +++ b/test/asset/run/background.yaml @@ -0,0 +1,14 @@ +job background_success: + checkout: null + shell: | + echo $$ + sleep infinity & + echo $! + +job background_failure: + checkout: null + shell: | + echo $$ + sleep infinity & + echo $! + false diff --git a/test/asset/run/forever.yaml b/test/asset/run/forever.yaml new file mode 100644 index 0000000..f9f78f1 --- /dev/null +++ b/test/asset/run/forever.yaml @@ -0,0 +1,7 @@ +job forever: + checkout: null + shell: | + echo $$ + sleep infinity & + echo $! + wait diff --git a/test/script/run.et b/test/script/run.et index 08df6ce..6c990be 100644 --- a/test/script/run.et +++ b/test/script/run.et @@ -919,3 +919,123 @@ test RunHighestTag: /job-finish $tc:fourth done/ expect /(.*)/ from p capture finish guard (finish == "run-finish") + + +test KillCancelledProcesses: + node n + shell on n as git_init: + git -c init.defaultBranch=master init -q + git -c user.name=test -c user.email=test commit -q --allow-empty -m 'initial commit' + cp "${scripts.path}/forever.yaml" minici.yaml + git add minici.yaml + git -c user.name=test -c user.email=test commit -q -m 'minici.yaml' + git rev-parse HEAD + + touch a + git add a + git -c user.name=test -c user.email=test commit -q -m a + git rev-parse HEAD + git rev-parse HEAD^{tree} + + touch b + git add b + git -c user.name=test -c user.email=test commit -q -m b + git rev-parse HEAD + git rev-parse HEAD^{tree} + + git reset --hard HEAD~2 + git rev-parse HEAD + + expect /([0-9a-f]+)/ from git_init capture c_init + expect /([0-9a-f]+)/ from git_init capture ca + expect /([0-9a-f]+)/ from git_init capture ta + expect /([0-9a-f]+)/ from git_init capture cb + expect /([0-9a-f]+)/ from git_init capture tb + expect /([0-9a-f]+)/ from git_init capture c_end + + guard (c_init == c_end) + + local: + shell on n: + git reset --hard $ca + + spawn on n as p args [ "run", "watch(master)" ] killwith SIGINT + expect from p /watch-branch-started master/ + + expect from p: + /job-enqueue $ta:forever/ + /job-start $ta:forever/ + + spawn on n as log_a args [ "log", "$ta:forever" ] + expect from log_a: + /msg (.*)/ capture a_pid_sh + /msg (.*)/ capture a_pid_sleep + + local: + shell on n as check: + ps -q $a_pid_sh,$a_pid_sleep -o pid=,comm= + + shell on n: + git reset --hard $cb + + expect from p: + /job-finish $ta:forever cancelled/ + /job-enqueue $tb:forever/ + /job-start $tb:forever/ + + spawn on n as log_b args [ "log", "$tb:forever" ] + expect from log_b: + /msg (.*)/ capture b_pid_sh + /msg (.*)/ capture b_pid_sleep + + local: + shell on n: + ! ps -q $a_pid_sh,$a_pid_sleep -o pid=,comm= + ps -q $b_pid_sh,$b_pid_sleep -o pid=,comm= + + local: + spawn on n as log args [ "log", "$tb:forever" ] + expect from log: + /msg (.*)/ capture sh_pid + /msg (.*)/ capture sleep_pid + + shell on n: + ! ps -q $sh_pid,$sleep_pid -o pid=,comm= + + +test KillBackgroundProcesses: + node n + shell on n: + cp "${scripts.path}/background.yaml" minici.yaml + + local: + spawn on n as p args [ "./minici.yaml", "run", "background_success" ] + + expect from p: + /job-enqueue background_success/ + /job-start background_success/ + /job-finish background_success done/ + + spawn on n as log args [ "./minici.yaml", "log", "background_success" ] + expect from log: + /msg (.*)/ capture pid_sh + /msg (.*)/ capture pid_sleep + + shell on n: + ! ps -q $pid_sh,$pid_sleep -o pid=,comm= + + local: + spawn on n as p args [ "./minici.yaml", "run", "background_failure" ] + + expect from p: + /job-enqueue background_failure/ + /job-start background_failure/ + /job-finish background_failure failed/ + + spawn on n as log args [ "./minici.yaml", "log", "background_failure" ] + expect from log: + /msg (.*)/ capture pid_sh + /msg (.*)/ capture pid_sleep + + shell on n: + ! ps -q $pid_sh,$pid_sleep -o pid=,comm= -- cgit v1.2.3