diff --git a/test/harness/reap_daemons_test.py b/test/harness/reap_daemons_test.py index 1126eca60..f6cbc9442 100644 --- a/test/harness/reap_daemons_test.py +++ b/test/harness/reap_daemons_test.py @@ -44,3 +44,22 @@ class ReapDaemonsTests(unittest.TestCase): if process.poll() is None: process.terminate() process.wait(timeout=5) + + def test_calling_process_may_name_a_path_under_the_snapshot(self) -> None: + script = Path(__file__).resolve().parents[1] / "reap-daemons.sh" + with tempfile.TemporaryDirectory(prefix="reaper-caller-") as directory: + root = Path(directory) + result = subprocess.run( + [ + sys.executable, + "-c", + "import subprocess, sys; sys.exit(subprocess.call(sys.argv[1:3]))", + str(script), + str(root), + str(root / "launcher"), + ], + capture_output=True, + text=True, + timeout=15, + ) + self.assertEqual(result.returncode, 0, result.stderr) diff --git a/test/reap-daemons.sh b/test/reap-daemons.sh index 0bbcc537f..059c98cee 100755 --- a/test/reap-daemons.sh +++ b/test/reap-daemons.sh @@ -19,11 +19,19 @@ wait_seconds="${2:-0}" # pgrep takes an extended regular expression; snapshot paths are literal. pattern="$(printf '%s/' "${under%/}" | sed 's/[][\\.^$*+?(){}|]/\\&/g')" -# Processes whose command line names a path under $under, except this script. +# A shell or other caller can name the snapshot in its arguments. It owns +# this reaper, so exclude it and the rest of our process ancestry. +ancestors=" $$ " +parent="$PPID" +while [[ -n $parent && $parent != 0 ]]; do + ancestors+="$parent " + read -r parent < <(ps -o ppid= -p "$parent") || break +done + running() { local pid for pid in $(pgrep -f -- "$pattern" || true); do - if [[ $pid != "$$" && $pid != "$BASHPID" ]]; then + if [[ $ancestors != *" $pid "* && $pid != "$BASHPID" ]]; then echo "$pid" fi done