2020-06-13 13:38:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from pexpect_helper import SpawnedProc
|
|
|
|
|
2020-08-10 18:58:44 +00:00
|
|
|
sp = SpawnedProc(timeout=10)
|
2020-06-13 13:38:54 +00: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
|
2024-03-30 15:10:12 +00:00
|
|
|
import platform
|
2020-06-13 13:38:54 +00:00
|
|
|
import signal
|
|
|
|
import subprocess
|
2020-08-12 14:59:56 +00:00
|
|
|
import sys
|
2020-06-13 13:38:54 +00:00
|
|
|
|
2024-12-15 16:38:37 +00:00
|
|
|
if "CI" in os.environ and platform.system() in ["Darwin", "FreeBSD"]:
|
2024-03-30 15:10:12 +00:00
|
|
|
sys.exit(127)
|
|
|
|
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_prompt()
|
|
|
|
|
2021-07-26 20:12:30 +00:00
|
|
|
# Verify that SIGINT inside a command sub cancels it.
|
|
|
|
# Negate the pid to send to the pgroup (which should include sleep).
|
2024-05-05 21:51:03 +00:00
|
|
|
sendline("while true; echo (sleep 2000); end")
|
2021-07-26 20:12:30 +00:00
|
|
|
sleep(0.5)
|
|
|
|
os.kill(-sp.spawn.pid, signal.SIGINT)
|
|
|
|
expect_prompt()
|
2024-05-08 14:35:00 +00:00
|
|
|
sleep(0.2)
|
2021-07-26 20:12:30 +00:00
|
|
|
|
2024-05-05 21:51:03 +00:00
|
|
|
# SIGINT should be ignored by background processes.
|
|
|
|
sendline("sleep 1234 &")
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_prompt()
|
2024-05-05 21:51:03 +00:00
|
|
|
os.kill(-sp.spawn.pid, signal.SIGINT)
|
2024-05-07 15:08:09 +00:00
|
|
|
sleep(0.500)
|
2020-06-13 13:38:54 +00:00
|
|
|
sendline("jobs")
|
2024-05-05 21:51:03 +00:00
|
|
|
expect_prompt("sleep 1234")
|
2020-06-13 13:38:54 +00:00
|
|
|
sendline("kill %1")
|
2020-06-24 18:43:56 +00:00
|
|
|
expect_prompt()
|
2020-06-13 13:38:54 +00: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")
|
Disable terminal protocols before cancellable operations
The [disambiguate flag](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#disambiguate) means that:
> In particular, ctrl+c will no longer generate the SIGINT signal,
> but instead be delivered as a CSI u escape code.
so cancellation only works while we turn off disambiguation.
Today we turn it off while running external commands that want to
claim the TTY. Also we do it (only as a workaround for this issue)
while expanding wildcards or while running builtin wait.
However there are other cases where we don't have a workaround,
like in trivial infinite loops or when opening a fifo.
Before we run "while true; end", we put the terminal back in ICANON
mode. This means it's line-buffered, so we won't be able to detect
if the user pressed ctrl-c.
Commit 8164855b7 (Disable terminal protocols throughout evaluation,
2024-04-02) had the right solution: simply disable terminal protocols
whenever we do computations that might take a long time.
eval_node() covers most of that; there are a few others.
As pointed out in #10494, the logic was fairly unsophisticated then:
it toggled terminal protocols many times. The fix in 29f2da8d1
(Toggle terminal protocols lazily, 2024-05-16) went to the extreme
other end of only toggling protocols when absolutely necessary.
Back out part of that commit by toggling in eval_node() again,
fixing cancellation. Fortunately, we can keep most of the benefits
of the lazy approach from 29f2da8d1: we toggle only 2 times instead
of 8 times for an empty prompt.
There are only two places left where we call signal_check_cancel()
without necessarily disabling the disambiguate flag
1. open_cloexec() we assume that the files we open outside eval_node()
are never blocking fifos.
2. fire_delayed(). Judging by commit history, this check is not
relevant for interactive sessions; we'll soon end up calling
eval_node() anyway.
In future, we can leave bracketed paste, modifyOtherKeys and
application keypad mode turned on again, until we actually run an
external command. We really only want to turn off the disambiguate
flag.
Since this is approach is overly complex, I plan to go with either
of these two alternatives in future:
- extend the kitty keyboard protocol to optionally support VINTR,
VSTOP and friends. Then we can drop most of these changes.
- poll stdin for ctrl-c. This promises a great simplification,
because it implies that terminal ownership (term_steal/term_donate)
will be perfectly synced with us enabling kitty keyboard protocol.
This is because polling requires us to turn off ICANON.
I started working on this change; I'm convinced it must work,
but it's not finished yet. Note that this will also want to
add stdin polling to builtin wait.
Closes #10864
2024-11-23 16:43:19 +00:00
|
|
|
expect_re(r"\r\n?.*read> .*", timeout=10)
|
2024-04-29 20:47:24 +00:00
|
|
|
sleep(0.1)
|
2020-06-13 13:38:54 +00:00
|
|
|
os.kill(sp.spawn.pid, signal.SIGINT)
|
2024-04-25 19:36:31 +00:00
|
|
|
expect_str("fish_postexec spotted", timeout=10)
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_prompt()
|
|
|
|
|
2024-05-05 22:18:43 +00:00
|
|
|
# Ensure that we catch up.
|
|
|
|
sendline("echo 'hello' 'world'")
|
|
|
|
expect_prompt("hello world")
|
|
|
|
|
2020-06-13 13:38:54 +00:00
|
|
|
# Verify that the fish_kill_signal is set.
|
2020-06-24 18:43:56 +00:00
|
|
|
sendline(
|
|
|
|
"functions -e postexec; function postexec --on-event fish_postexec; echo fish_kill_signal $fish_kill_signal; end"
|
|
|
|
)
|
2024-05-05 22:18:43 +00:00
|
|
|
expect_prompt("fish_kill_signal 0")
|
2020-06-13 13:38:54 +00:00
|
|
|
sendline("sleep 5")
|
2024-04-23 18:38:09 +00:00
|
|
|
sleep(0.300)
|
2021-09-04 20:32:51 +00:00
|
|
|
subprocess.call(["pkill", "-INT", "-P", str(sp.spawn.pid), "sleep"])
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_str("fish_kill_signal 2")
|
|
|
|
expect_prompt()
|
|
|
|
|
|
|
|
sendline("sleep 5")
|
2022-08-23 16:42:23 +00:00
|
|
|
sleep(0.200)
|
2022-06-16 16:43:28 +00:00
|
|
|
subprocess.call(["pkill", "-TERM", "-P", str(sp.spawn.pid), "sleep"])
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_str("fish_kill_signal 15")
|
|
|
|
expect_prompt()
|
|
|
|
|
2024-01-23 15:38:03 +00:00
|
|
|
# See that open() is only interruptible by SIGINT.
|
|
|
|
sendline("mkfifo fifoo")
|
|
|
|
expect_prompt()
|
|
|
|
sendline("cat >fifoo")
|
|
|
|
subprocess.call(["kill", "-WINCH", str(sp.spawn.pid)])
|
|
|
|
expect_re("open: ", shouldfail=True, timeout=10)
|
|
|
|
subprocess.call(["kill", "-INT", str(sp.spawn.pid)])
|
|
|
|
expect_prompt()
|
|
|
|
|
2020-06-13 13:38:54 +00:00
|
|
|
# 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-23 16:42:23 +00:00
|
|
|
#
|
|
|
|
# Save the pids for later to check if they are still running.
|
|
|
|
pids = []
|
2024-05-05 22:18:43 +00:00
|
|
|
send("sleep 1300 & echo $last_pid\r")
|
2024-04-02 20:40:45 +00:00
|
|
|
pids += [expect_re("\\d+\r\n").group().strip()]
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_prompt()
|
2024-05-05 22:18:43 +00:00
|
|
|
sendline("echo 'catch' 'up' 'A'")
|
|
|
|
expect_prompt("catch up A")
|
|
|
|
send("sleep 1310 & echo $last_pid\r")
|
2024-04-02 20:40:45 +00:00
|
|
|
pids += [expect_re("\\d+\r\n").group().strip()]
|
2020-06-13 13:38:54 +00:00
|
|
|
expect_prompt()
|
2024-05-05 22:18:43 +00:00
|
|
|
sendline("echo 'catch' 'up' 'B'")
|
|
|
|
expect_prompt("catch up B")
|
2020-08-29 22:43:43 +00:00
|
|
|
send("sleep 9999999\r")
|
2023-01-18 15:48:49 +00:00
|
|
|
sleep(0.500) # ensure fish kicks off the above sleep before it gets HUP - see #7288
|
2020-06-13 13:38:54 +00: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-23 16:42:23 +00:00
|
|
|
|
2023-06-01 16:20:19 +00:00
|
|
|
remaining = []
|
2020-06-13 13:38:54 +00:00
|
|
|
if proc.returncode == 0:
|
2022-08-23 16:42:23 +00: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 13:38:54 +00:00
|
|
|
print(proc.stdout)
|
2023-06-01 16:20:19 +00:00
|
|
|
for line in proc.stdout.split(b"\n"):
|
|
|
|
pid = line.split(b" ", maxsplit=1)[0].decode("utf-8")
|
2022-08-23 16:42:23 +00:00
|
|
|
if pid in pids:
|
|
|
|
remaining += [pid]
|
2023-06-01 16:20:19 +00:00
|
|
|
|
2022-08-23 16:42:23 +00:00
|
|
|
# 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 13:38:54 +00:00
|
|
|
sys.exit(1)
|