2020-06-13 21:38:54 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from pexpect_helper import SpawnedProc
|
|
|
|
|
2020-08-11 02:58:44 +08:00
|
|
|
sp = SpawnedProc(timeout=10)
|
2020-06-13 21:38:54 +08:00
|
|
|
send, sendline, sleep, expect_prompt, expect_re, expect_str = (
|
|
|
|
sp.send,
|
|
|
|
sp.sendline,
|
|
|
|
sp.sleep,
|
|
|
|
sp.expect_prompt,
|
|
|
|
sp.expect_re,
|
|
|
|
sp.expect_str,
|
|
|
|
)
|
|
|
|
|
|
|
|
from time import sleep
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import subprocess
|
2020-08-12 22:59:56 +08:00
|
|
|
import sys
|
2020-06-13 21:38:54 +08:00
|
|
|
|
|
|
|
expect_prompt()
|
|
|
|
|
2021-07-27 04:12:30 +08:00
|
|
|
# Verify that SIGINT inside a command sub cancels it.
|
|
|
|
# Negate the pid to send to the pgroup (which should include sleep).
|
|
|
|
sendline("while true; echo (sleep 1000); end")
|
|
|
|
sleep(0.5)
|
|
|
|
os.kill(-sp.spawn.pid, signal.SIGINT)
|
|
|
|
expect_prompt()
|
|
|
|
|
2020-06-13 21:38:54 +08:00
|
|
|
sendline("sleep 10 &")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
send("\x03")
|
|
|
|
sleep(0.010)
|
|
|
|
sendline("jobs")
|
|
|
|
expect_prompt("sleep.10")
|
|
|
|
sendline("kill %1")
|
2020-06-25 02:43:56 +08:00
|
|
|
expect_prompt()
|
2020-06-13 21:38:54 +08:00
|
|
|
|
|
|
|
# Verify that the fish_postexec handler is called after SIGINT.
|
|
|
|
sendline("function postexec --on-event fish_postexec; echo fish_postexec spotted; end")
|
|
|
|
expect_prompt()
|
|
|
|
sendline("read")
|
|
|
|
expect_re("\r\n?read> $")
|
2022-08-24 00:42:23 +08:00
|
|
|
sleep(0.200)
|
2020-06-13 21:38:54 +08:00
|
|
|
os.kill(sp.spawn.pid, signal.SIGINT)
|
|
|
|
expect_str("fish_postexec spotted")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
# Verify that the fish_kill_signal is set.
|
2020-06-25 02:43:56 +08:00
|
|
|
sendline(
|
|
|
|
"functions -e postexec; function postexec --on-event fish_postexec; echo fish_kill_signal $fish_kill_signal; end"
|
|
|
|
)
|
2020-06-13 21:38:54 +08:00
|
|
|
expect_prompt()
|
|
|
|
sendline("sleep 5")
|
2022-08-24 00:42:23 +08:00
|
|
|
sleep(0.200)
|
2021-09-05 04:32:51 +08:00
|
|
|
subprocess.call(["pkill", "-INT", "-P", str(sp.spawn.pid), "sleep"])
|
2020-06-13 21:38:54 +08:00
|
|
|
expect_str("fish_kill_signal 2")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
sendline("sleep 5")
|
2022-08-24 00:42:23 +08:00
|
|
|
sleep(0.200)
|
2022-06-17 00:43:28 +08:00
|
|
|
subprocess.call(["pkill", "-TERM", "-P", str(sp.spawn.pid), "sleep"])
|
2020-06-13 21:38:54 +08:00
|
|
|
expect_str("fish_kill_signal 15")
|
|
|
|
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.
|
2022-08-24 00:42:23 +08:00
|
|
|
#
|
|
|
|
# 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()]
|
2020-06-13 21:38:54 +08:00
|
|
|
expect_prompt()
|
2022-08-24 00:42:23 +08:00
|
|
|
send("sleep 131 & echo $last_pid\r")
|
|
|
|
pids += [expect_re("\d+\r\n").group().strip()]
|
2020-06-13 21:38:54 +08:00
|
|
|
expect_prompt()
|
2020-08-30 06:43:43 +08:00
|
|
|
send("sleep 9999999\r")
|
2023-01-18 23:48:49 +08:00
|
|
|
sleep(0.500) # ensure fish kicks off the above sleep before it gets HUP - see #7288
|
2020-06-13 21:38:54 +08:00
|
|
|
os.kill(sp.spawn.pid, signal.SIGHUP)
|
|
|
|
|
|
|
|
# Verify the spawned fish shell has exited.
|
|
|
|
sp.spawn.wait()
|
|
|
|
|
|
|
|
# Verify all child processes have been killed. We don't use `-p $pid` because
|
|
|
|
# if the shell has a bug the child processes might have been reparented to pid
|
|
|
|
# 1 rather than killed.
|
|
|
|
proc = subprocess.run(
|
|
|
|
["pgrep", "-l", "-f", "sleep 13"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
|
|
|
)
|
2022-08-24 00:42:23 +08:00
|
|
|
|
|
|
|
remaining=[]
|
2020-06-13 21:38:54 +08:00
|
|
|
if proc.returncode == 0:
|
2022-08-24 00:42:23 +08:00
|
|
|
# If any sleeps exist, we check them against our pids,
|
|
|
|
# to avoid false-positives (any other `sleep 13xyz` running on the system)
|
2020-06-13 21:38:54 +08:00
|
|
|
print(proc.stdout)
|
2022-08-24 00:42:23 +08:00
|
|
|
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)
|
2020-06-13 21:38:54 +08:00
|
|
|
sys.exit(1)
|