Skip to content

Terminate process group after cram execution #11841

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions doc/changes/11841.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Kill all processes in the process group after the main process has
terminated; in particular this avoids background processes in cram tests to
stick around after the test finished (#11841, fixes #11820, @Alizter,
@Leonidas-from-XIV)
7 changes: 6 additions & 1 deletion src/dune_engine/scheduler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,12 @@ let wait_for_build_process t pid =
~on_cancel:(fun () ->
Process_watcher.killall t.process_watcher Sys.sigkill;
Fiber.return ())
(fun () -> wait_for_process t pid)
(fun () ->
let+ r = wait_for_process t pid in
(* [kill_process_group] on Windows only kills the pid and by this
time the process should've exited anyway *)
if not Sys.win32 then kill_process_group pid Sys.sigterm;
r)
in
( res
, match outcome with
Expand Down
29 changes: 29 additions & 0 deletions test/blackbox-tests/test-cases/cram/subprocess.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Testing the termination of subprocesses in cram tests. We first create a dune
project with a single cram test.

$ cat > dune-project <<EOF
> (lang dune 3.19)
> EOF

We create a file for tracking the PID of a subprocess.

$ cat > pid.txt
$ export FILE=$(realpath ./pid.txt)

We create a cram test that spawns a subprocess and records its PID in the file
we gave before.

$ cat > mycram.t <<EOF
> $ sleep 5 &
> $ echo \$! > $FILE
> EOF

We can now run this test, which will record its PID in the file.

$ dune build @mycram

The test finished successfully, now we make sure that the PID was correctly
terminated.

$ ps -p $(cat $FILE) > /dev/null || echo "Process terminated"
Process terminated
Loading