From 9e9c73e46a63b2e9826b23e53d1da5e23eacfbc3 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Tue, 23 Aug 2022 18:42:23 +0200 Subject: [PATCH] tests/signals.py: Kill leftover sleeps from python This starts two sleep processes and expects them to be killed on SIGHUP. Unfortunately, if this ever fails the second run will also fail because it'll see the old sleep still lying around (because it'll run for 130 seconds). So, what we do is: 1. Keep the pids for these specific sleeps 2. Check if any of them are still running (and only fail for them) 3. Kill them from python Fixes #9152 --- tests/pexpects/signals.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/pexpects/signals.py b/tests/pexpects/signals.py index 6c5e0e339..a3b308bad 100644 --- a/tests/pexpects/signals.py +++ b/tests/pexpects/signals.py @@ -41,7 +41,7 @@ sendline("function postexec --on-event fish_postexec; echo fish_postexec spotted expect_prompt() sendline("read") expect_re("\r\n?read> $") -sleep(0.100) +sleep(0.200) os.kill(sp.spawn.pid, signal.SIGINT) expect_str("fish_postexec spotted") expect_prompt() @@ -52,13 +52,13 @@ sendline( ) expect_prompt() sendline("sleep 5") -sleep(0.100) +sleep(0.200) subprocess.call(["pkill", "-INT", "-P", str(sp.spawn.pid), "sleep"]) expect_str("fish_kill_signal 2") expect_prompt() sendline("sleep 5") -sleep(0.100) +sleep(0.200) subprocess.call(["pkill", "-TERM", "-P", str(sp.spawn.pid), "sleep"]) expect_str("fish_kill_signal 15") expect_prompt() @@ -66,12 +66,17 @@ expect_prompt() # Verify that sending SIGHUP to the shell, such as will happen when the tty is # closed by the terminal, terminates the shell and the foreground command and # any background commands run from that shell. -send("sleep 130 &\r") +# +# Save the pids for later to check if they are still running. +pids = [] +send("sleep 130 & echo $last_pid\r") +pids += [expect_re("\d+\r\n").group().strip()] expect_prompt() -send("sleep 131 &\r") +send("sleep 131 & echo $last_pid\r") +pids += [expect_re("\d+\r\n").group().strip()] expect_prompt() send("sleep 9999999\r") -sleep(0.100) # ensure fish kicks off the above sleep before it gets HUP - see #7288 +sleep(0.300) # ensure fish kicks off the above sleep before it gets HUP - see #7288 os.kill(sp.spawn.pid, signal.SIGHUP) # Verify the spawned fish shell has exited. @@ -83,7 +88,26 @@ sp.spawn.wait() proc = subprocess.run( ["pgrep", "-l", "-f", "sleep 13"], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) + +remaining=[] if proc.returncode == 0: - print("Commands still running") + # If any sleeps exist, we check them against our pids, + # to avoid false-positives (any other `sleep 13xyz` running on the system) print(proc.stdout) + for line in proc.stdout.split(b'\n'): + pid = line.split(b' ', maxsplit=1)[0].decode("utf-8") + if pid in pids: + remaining += [pid] + +# Kill any remaining sleeps ourselves, otherwise rerunning this is pointless. +for pid in remaining: + try: + os.kill(int(pid), signal.SIGTERM) + except ProcessLookupError: + continue + +if remaining: + # We still have processes left over! + print("Commands were still running!") + print(remaining) sys.exit(1)