From 9379853d4779ccc6cd704af0fe3095268fcf9dec Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 10 Jul 2022 11:37:40 +0530 Subject: [PATCH] Get the SIGINT test working on macOS --- kitty/prewarm.py | 8 ++++++-- kitty_tests/prewarm.py | 15 +++++++++++---- prewarm-launcher.c | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/kitty/prewarm.py b/kitty/prewarm.py index 08c2a131b..f68bb8ce5 100644 --- a/kitty/prewarm.py +++ b/kitty/prewarm.py @@ -62,9 +62,11 @@ class Child: child_process_pid: int -def wait_for_child_death(child_pid: int, timeout: float = 1) -> Optional[int]: +def wait_for_child_death(child_pid: int, timeout: float = 1, kill_signal: Optional[signal.Signals] = None) -> Optional[int]: st = time.monotonic() - while time.monotonic() - st < timeout: + while not timeout or time.monotonic() - st < timeout: + if kill_signal is not None: + os.kill(child_pid, kill_signal) try: pid, status = os.waitpid(child_pid, os.WNOHANG) except ChildProcessError: @@ -72,6 +74,8 @@ def wait_for_child_death(child_pid: int, timeout: float = 1) -> Optional[int]: else: if pid == child_pid: return status + if not timeout: + break time.sleep(0.01) return None diff --git a/kitty_tests/prewarm.py b/kitty_tests/prewarm.py index 23781f0e9..aa61b97b0 100644 --- a/kitty_tests/prewarm.py +++ b/kitty_tests/prewarm.py @@ -64,23 +64,30 @@ def report_screen_size_change(*a): env.update({'TEST_ENV_PASS': 'xyz', 'KITTY_PREWARM_SOCKET': p.socket_env_var(), 'TERM': 'xterm-kitty', 'TERMINFO': terminfo_dir}) cols = 117 - def wait_for_death(exit_code): - status = wait_for_child_death(pty.child_pid, timeout=5) + def wait_for_death(exit_code, signal=None): + status = wait_for_child_death(pty.child_pid, timeout=5, kill_signal=signal) if status is None: os.kill(pty.child_pid, signal.SIGKILL) self.assertIsNotNone(status, f'prewarm wrapper process did not exit. Screen contents: {pty.screen_contents()}') with suppress(AttributeError): self.assertEqual(os.waitstatus_to_exitcode(status), exit_code, pty.screen_contents()) + # test SIGINT both via signal to wrapper and by sending ctrl-c over the tty pty = self.create_pty( argv=[kitty_exe(), '+runpy', src + 'socket_child_main(initial_print="child ready:")'], cols=cols, env=env, cwd=cwd) pty.wait_till(lambda: 'child ready:' in pty.screen_contents()) pty.set_window_size(columns=cols + 3) pty.wait_till(lambda: f'Screen size changed: {cols + 3}' in pty.screen_contents()) - os.kill(pty.child_pid, signal.SIGINT) - wait_for_death(128 + signal.SIGINT) + wait_for_death(128 + signal.SIGINT, signal=signal.SIGINT) pty.wait_till(lambda: 'KeyboardInterrupt' in pty.screen_contents()) + pty = self.create_pty( + argv=[kitty_exe(), '+runpy', src + 'socket_child_main(initial_print="child ready:")'], cols=cols, env=env, cwd=cwd) + pty.wait_till(lambda: 'child ready:' in pty.screen_contents()) + pty.write_to_child('\x03') + pty.wait_till(lambda: 'KeyboardInterrupt' in pty.screen_contents()) + wait_for_death(128 + signal.SIGINT) + # test passing of data via cwd, env vars and stdin/stdout redirection stdin_r, stdin_w = os.pipe() os.set_inheritable(stdin_w, False) stdout_r, stdout_w = os.pipe() diff --git a/prewarm-launcher.c b/prewarm-launcher.c index dc3c0066e..815711c21 100644 --- a/prewarm-launcher.c +++ b/prewarm-launcher.c @@ -491,8 +491,8 @@ read_signals(void) { } break; } - memmove(buf, buf + sizeof(siginfo_t), sizeof(siginfo_t)); buf_pos -= sizeof(siginfo_t); + memmove(buf, buf + sizeof(siginfo_t), buf_pos); } return true; }