From a3cc33d701d2c1b57d1df80b6c498f235291f56d Mon Sep 17 00:00:00 2001
From: Dmitry Kalinkin <dmitry.kalinkin@gmail.com>
Date: Wed, 5 Jun 2024 19:00:10 -0400
Subject: [PATCH] treewide: use `set -m` and `trap` to cleanup lingering
 processes

---
 benchmarks/backgrounds/Snakefile | 8 +++++++-
 benchmarks/ecal_gaps/Snakefile   | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/benchmarks/backgrounds/Snakefile b/benchmarks/backgrounds/Snakefile
index d50b8c7f..d44e695c 100644
--- a/benchmarks/backgrounds/Snakefile
+++ b/benchmarks/backgrounds/Snakefile
@@ -72,6 +72,13 @@ rule backgrounds_ecal_backwards:
     threads: workflow.cores
     shell:
         """
+set -m # monitor mode to prevent lingering shells
+cleanup() {{
+  echo Cleaning up
+  kill $WORKER_PID $SCHEDULER_PID
+}}
+trap cleanup EXIT
+
 PORT=$RANDOM
 dask scheduler --port $PORT &
 export DASK_SCHEDULER=localhost:$PORT
@@ -87,5 +94,4 @@ PROTON_BEAM_GAS_GEN=$(realpath {input.proton_beam_gas_gen}) \
 PROTON_BEAM_GAS_SIM=$(realpath {input.proton_beam_gas_sim}) \
 OUTPUT_DIR={output} \
 python {input.script}
-kill $WORKER_PID $SCHEDULER_PID
 """
diff --git a/benchmarks/ecal_gaps/Snakefile b/benchmarks/ecal_gaps/Snakefile
index a312ac1e..b649f15d 100644
--- a/benchmarks/ecal_gaps/Snakefile
+++ b/benchmarks/ecal_gaps/Snakefile
@@ -66,6 +66,7 @@ rule ecal_gaps:
     threads: workflow.cores
     shell:
         """
+set -m # monitor mode to prevent lingering shells
 cleanup() {{
   echo Cleaning up
   kill $WORKER_PID $SCHEDULER_PID
-- 
GitLab