Cleanup previous PR

A wrapper that runs the command as a child, rather than exec-ing it, can
run other children before, after or alongside it, such as a script that
runs some setup commands first. Re-check that the cached child of a
wrapper is still its live child, so that root_pid does not get stuck on
an exited helper, or on the command after it exits while the wrapper
keeps running. When a wrapper has several children, prefer the one
running the wrapped command, and only cache the choice if it is certain.

Match wrappers specified by name when their argv[0] is the path they
were found at, as is the case when run by the hold kitten, and wrappers
specified by path only by that exact path.

Compare the wrapper argv and the script arguments with empty arguments
dropped, as cmdline_of_pid() does on Linux, in one place, so that
binary wrappers with empty arguments are also matched.

Add tests for wrappers that run other commands, that exec after being
seen, with prefix and empty arguments, run via the hold kitten, and for
matches_cmdline() and runs_command().
This commit is contained in:
Kovid Goyal 2026-11-18 07:54:54 +05:30
parent 8fe8b2bfeb
commit 8133effda3
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 264 additions and 37 deletions

View file

@ -48,6 +48,9 @@ if is_macos:
def cmdline_of_pid(pid: int) -> list[str]:
return cmdline_(pid)
def _ppid_of_live_process(pid: int) -> int:
return _ppid_of_process(pid)
def _get_descendants_of_macos(pid: int) -> set[int]:
children_map: DefaultDict[int, list[int]] = defaultdict(list)
for p in fast_data_types.get_all_processes():
@ -145,6 +148,22 @@ else:
ans.extend(map(int, f.read().split()))
return ans
def _state_and_ppid_of_process(pid: int) -> tuple[bytes, int]:
with open(f'/proc/{pid}/stat', 'rb') as f:
raw = f.read()
# The process name can contain spaces and parentheses
state, ppid = raw[raw.rindex(b')') + 2 :].split()[:2]
return state, int(ppid)
def _ppid_of_process(pid: int) -> int:
return _state_and_ppid_of_process(pid)[1]
def _ppid_of_live_process(pid: int) -> int:
state, ppid = _state_and_ppid_of_process(pid)
if state == b'Z':
raise ProcessLookupError(f'The process {pid} has exited')
return ppid
def _children_map() -> DefaultDict[int, list[int]]:
ans: DefaultDict[int, list[int]] = defaultdict(list)
try:
@ -155,10 +174,8 @@ else:
if not x.isdigit():
continue
with suppress(OSError, ValueError, IndexError):
with open(f'/proc/{x}/stat', 'rb') as f:
raw = f.read()
# The process name can contain spaces and parentheses
ans[int(raw[raw.rindex(b')') + 2 :].split()[1])].append(int(x))
pid = int(x)
ans[_ppid_of_process(pid)].append(pid)
return ans
def children_of_process(pid: int) -> list[int]:
@ -368,28 +385,58 @@ class ProcessDesc(TypedDict):
cmdline: Sequence[str] | None
def _as_in_cmdline(args: Sequence[str]) -> tuple[str, ...]:
# cmdline_of_pid() drops empty arguments on Linux
return tuple(args) if is_macos else tuple(filter(None, args))
def _is_program(arg0: str, name: str) -> bool:
# Whether arg0 is that of the program run as name. Programs run by name
# can have the path they were found at as argv[0], for example, when run
# by the hold kitten, or when they are scripts.
return arg0 == name or (os.sep not in name and os.path.basename(arg0) == name)
def _runs_as_script(cmdline: tuple[str, ...], name: str, args: tuple[str, ...]) -> bool:
# Scripts are run by their interpreter as: interpreter [interpreter args]
# script_path args
i = len(cmdline) - len(args) - 1
return i > 0 and cmdline[i + 1 :] == args and _is_program(cmdline[i], name)
class KittyWrapper(NamedTuple):
# The argv the wrapper process was started with
argv: tuple[str, ...]
# True if the wrapper eventually replaces itself (via exec) with
# something else, at which point it is no longer a wrapper
# True if the wrapper might replace itself (via exec) with something
# else, at which point it is no longer a wrapper
replaces_itself: bool
# Arguments after the script path, including the wrapped command.
# For wrappers that might be scripts, all the arguments the wrapper is run
# with, including the wrapped command, after its own argv[0]
script_args: tuple[str, ...] | None = None
def matches_cmdline(self, cmdline: tuple[str, ...]) -> bool:
if cmdline[: len(self.argv)] == self.argv:
def matches_cmdline(self, cmdline: Sequence[str]) -> bool:
"""True if cmdline, from cmdline_of_pid(), is that of the wrapper"""
argv, cmdline = _as_in_cmdline(self.argv), tuple(cmdline)
if not argv or not cmdline:
return False
if _is_program(cmdline[0], argv[0]) and cmdline[1 : len(argv)] == argv[1:]:
return True
# Since script_args includes the wrapped command, the command cannot
# match after the wrapper has exec-ed it.
return self.script_args is not None and _runs_as_script(cmdline, argv[0], _as_in_cmdline(self.script_args))
def runs_command(self, cmdline: Sequence[str]) -> bool:
"""True if cmdline, from cmdline_of_pid(), is that of the command
the wrapper runs, as long as the command has not exec-ed something else"""
if self.script_args is None:
return False
# The kernel puts the interpreter ahead of the script invocation.
# Match the whole tail since the wrapped command might also take the
# wrapper's filename as an argument.
script_index = len(cmdline) - len(self.script_args) - 1
if script_index <= 0 or cmdline[script_index + 1 :] != self.script_args:
command, cmdline = _as_in_cmdline(self.script_args[len(self.argv) - 1 :]), tuple(cmdline)
if not command or not cmdline:
return False
script = cmdline[script_index]
return script == self.argv[0] or os.path.basename(script) == self.argv[0]
if _is_program(cmdline[0], command[0]) and cmdline[1:] == command[1:]:
return True
# The cmdline of the wrapper itself also ends with the command
return _runs_as_script(cmdline, command[0], command[1:]) and not self.matches_cmdline(cmdline)
child_counter = count()
@ -588,9 +635,7 @@ class Child:
final_exe = which(argv[0]) or argv[0]
# The wrapper either replaces itself with the command or runs
# it as a child, either way it is not the root process
# The procfs cmdline reader omits empty arguments.
script_args = tuple(arg for arg in argv[pos + 1 :] if is_macos or arg)
kitty_wrappers.append(KittyWrapper(user_prefix, True, script_args))
kitty_wrappers.append(KittyWrapper(user_prefix, True, tuple(argv[pos + 1 :])))
if self.hold:
hold_argv = cmdline_for_hold()
argv = hold_argv + argv
@ -728,12 +773,32 @@ class Child:
return cwd_of_process(pid)
return None
@staticmethod
def _is_child_of(pid: int, ppid: int) -> bool:
try:
return _ppid_of_live_process(pid) == ppid
except Exception: # pid has exited
return False
@staticmethod
def _wrapped_child(w: KittyWrapper, children: list[int]) -> tuple[int, bool]:
# The child of the wrapper that is running the command and whether
# it is certain to be the one
if len(children) == 1:
return children[0], True
running_command = []
for c in children:
with suppress(Exception): # child has exited
if w.runs_command(cmdline_of_pid(c)):
running_command.append(c)
return min(running_command or children), bool(running_command)
@property
def root_pid(self) -> int | None:
"""
The pid of the process originally started when the window was created,
skipping over any wrapper processes kitty itself inserted, such as
/usr/bin/login on macOS. A wrapper is replaced by its first child.
/usr/bin/login on macOS. A wrapper is replaced by its child.
"""
if self.resolved_root_pid is not None:
return self.resolved_root_pid
@ -756,9 +821,16 @@ class Child:
final = final and i in self.wrappers_seen
break
self.wrappers_seen.add(i)
final = False # it will replace itself later
# A wrapper only ever runs a single child, so cache it
final = False # it might replace itself later
# A wrapper runs the command as its child, so cache it. However,
# wrappers that run it as a child, rather than exec-ing it, can
# run other children before, after or alongside it, such as a
# script that runs some setup commands first, so check the cached
# child is still running.
child = self.wrapper_children.get(pid)
if child is not None and not self._is_child_of(child, pid):
del self.wrapper_children[pid]
child = None
if child is None:
try:
children = children_of_process(pid)
@ -767,9 +839,14 @@ class Child:
final = False
break
if not children:
final = False # the wrapper has not started its child yet
# The wrapper has not started its child yet, or it has exited
final = False
break
child = self.wrapper_children[pid] = min(children)
child, certain = self._wrapped_child(w, children)
if certain:
self.wrapper_children[pid] = child
else:
final = False
pid = child
if final:
self.resolved_root_pid = pid

View file

@ -2,8 +2,11 @@
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
import os
import shutil
import signal
import subprocess
import tempfile
from contextlib import suppress
from select import select
from types import SimpleNamespace
from unittest.mock import patch
@ -14,6 +17,14 @@ from kitty.constants import is_macos, kitty_exe
from .base import BaseTest
def _descendants(pid: int) -> list[int]:
ans = []
for child in children_of_process(pid):
ans.append(child)
ans.extend(_descendants(child))
return ans
def spawn_and_wait_for_ready(code: str, what: str) -> subprocess.Popen:
# Run code via kitty +runpy and wait for it to print ready. stderr is
# captured, as in test workers it is /dev/null, so that the reason the
@ -302,31 +313,170 @@ class LaunchWrapperTest(BaseTest):
return func
def test_wrapper_matches_cmdline(self):
def cmdline(*args):
# cmdline_of_pid() drops empty arguments on Linux
return args if is_macos else tuple(filter(None, args))
w = KittyWrapper(('wrapper', '-x'), True, ('-x', 'prog', 'arg'))
for yes in (
('wrapper', '-x', 'prog', 'arg'),
# Run via PATH lookup by the hold kitten
('/usr/bin/wrapper', '-x', 'prog', 'arg'),
('/bin/sh', '/usr/bin/wrapper', '-x', 'prog', 'arg'),
('/usr/bin/env', 'sh', '-e', 'wrapper', '-x', 'prog', 'arg'),
):
self.assertTrue(w.matches_cmdline(yes), yes)
for no in (
(),
('prog', 'arg'),
('/usr/bin/other', '-x', 'prog', 'arg'),
('/usr/bin/wrapper', 'prog', 'arg'),
('/bin/sh', '/usr/bin/other', '-x', 'prog', 'arg'),
('/bin/sh', '/usr/bin/wrapper', '-x', 'prog'),
('/bin/sh', '/usr/bin/wrapper', 'prog', 'arg'),
('/bin/sh', 'wrapper', 'prog', 'arg'),
):
self.assertFalse(w.matches_cmdline(no), no)
self.assertFalse(KittyWrapper(('wrapper', '-x'), True).matches_cmdline(('/bin/sh', '/usr/bin/wrapper', '-x', 'prog', 'arg')))
# Wrappers specified by path must be run from that path
w = KittyWrapper(('/opt/wrapper',), True, ('prog',))
self.assertTrue(w.matches_cmdline(('/opt/wrapper', 'prog')))
self.assertTrue(w.matches_cmdline(('/bin/sh', '/opt/wrapper', 'prog')))
self.assertFalse(w.matches_cmdline(('/usr/opt/wrapper', 'prog')))
self.assertFalse(w.matches_cmdline(('/bin/sh', '/usr/opt/wrapper', 'prog')))
# Empty arguments
w = KittyWrapper(('wrapper', '', 'x'), True, ('', 'x', '', 'prog'))
self.assertTrue(w.matches_cmdline(cmdline('wrapper', '', 'x', '', 'prog')))
self.assertTrue(w.matches_cmdline(cmdline('/bin/sh', '/bin/wrapper', '', 'x', '', 'prog')))
self.assertFalse(w.matches_cmdline(cmdline('', 'prog')))
def test_wrapper_runs_command(self):
w = KittyWrapper(('wrapper', '-x'), True, ('-x', 'prog', 'arg'))
for yes in (
('prog', 'arg'),
('/usr/bin/prog', 'arg'),
('/bin/sh', '/usr/bin/prog', 'arg'),
):
self.assertTrue(w.runs_command(yes), yes)
for no in (
(),
('prog',),
('prog', 'arg', 'x'),
('other', 'arg'),
('/bin/sh', '/usr/bin/other', 'arg'),
('/bin/sh', '/usr/bin/wrapper', '-x', 'prog', 'arg'),
):
self.assertFalse(w.runs_command(no), no)
self.assertFalse(KittyWrapper(('wrapper', '-x'), True).runs_command(('prog', 'arg')))
def _read_line(self, p: subprocess.Popen) -> str:
self.assertTrue(select([p.stdout], [], [], 5)[0], 'Timed out waiting for output')
return p.stdout.readline().decode().strip()
def _stop_tree(self, p: subprocess.Popen) -> None:
# Closing stdin makes everything the tests run exit, kill whatever
# does not, as leftover processes keep the output of the test runner
# open
descendants = _descendants(p.pid)
p.stdin.close()
try:
p.wait(5)
except subprocess.TimeoutExpired:
p.kill()
p.wait()
for pid in descendants:
with suppress(ProcessLookupError):
os.kill(pid, signal.SIGKILL)
def _send_line(self, p: subprocess.Popen) -> None:
p.stdin.write(b'\n')
p.stdin.flush()
# The command reports its PID and waits for a line on stdin in a child, so
# the process tree is ready when its PID is reported
report_pid = r"""/bin/sh -c 'printf "%s\n" "$0"; read -r _' "$$"
exit $?"""
def test_script_launch_wrapper_root_pid(self):
from kitty.launch_wrapper import LaunchRequest
req = LaunchRequest('tab')
# Reports its own PID and waits for a line, before exec-ing the command
exec_wrapper = 'printf "%s\\n" "$$"\nread -r _\nexec "$@"\n'
child_wrapper = '"$@"\nexit $?\n'
# Runs another command first, and keeps running after the command exits
busy_wrapper = """shift 2
/bin/sh -c 'printf "%s\\n" "$$"; read -r _'
"$@"
echo done
read -r _
"""
# Runs another command in the background, alongside the command
background_wrapper = 'sleep 60 &\ntrap "kill $!" EXIT\n"$@"\n'
with tempfile.TemporaryDirectory() as tdir, patch.dict(os.environ, {'PATH': tdir + os.pathsep + os.environ.get('PATH', '')}):
name = 'launch-wrapper'
script = os.path.join(tdir, name)
# The command reports its PID from a child so the process tree is ready.
report_pid = r'''/bin/sh -c 'printf "%s\n" "$0"; read -r _' "$$"'''
command = ('/bin/sh', '-c', f'{report_pid}\nexit $?', script, '')
for execs in (False, True):
with self.subTest(execs=execs):
# The command takes the path to the wrapper as an argument, to
# check it is not mistaken for the wrapper after being exec-ed
command = ('/bin/sh', '-c', self.report_pid, script, '')
for kind, body, prefix in (
('exec', exec_wrapper, (name,)),
('child', child_wrapper, (name,)),
('busy', busy_wrapper, (name, '', 'x')),
('background', background_wrapper, (name,)),
):
with self.subTest(wrapper=kind):
with open(script, 'w') as f:
f.write('#!/bin/sh -e\n' + ('exec "$@"\n' if execs else '"$@"\nexit $?\n'))
f.write('#!/bin/sh -e\n' + body)
os.chmod(script, 0o700)
c, exe, argv = self._fork(argv=command, launch_request=req, user_func=lambda ctx: [name])
with subprocess.Popen(argv, executable=exe, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as p:
c, exe, argv = self._fork(argv=command, launch_request=req, user_func=lambda ctx, prefix=prefix: list(prefix))
with subprocess.Popen(argv, executable=exe, stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0) as p:
c.pid = p.pid
try:
self.assertTrue(select([p.stdout], [], [], 5)[0], 'Wrapped command did not report its PID')
command_pid = int(p.stdout.readline())
self.ae(command_pid == p.pid, execs)
c.pid = p.pid
if kind == 'exec':
# Not exec-ed yet and has no child
self.ae(int(self._read_line(p)), p.pid)
self.ae(c.root_pid, p.pid)
self.assertIsNone(c.resolved_root_pid)
self._send_line(p)
elif kind == 'busy':
# While the first command runs, it is the child
helper_pid = int(self._read_line(p))
self.ae(c.root_pid, helper_pid)
self._send_line(p)
command_pid = int(self._read_line(p))
self.ae(command_pid == p.pid, kind == 'exec')
if kind == 'background':
self.ae(len(children_of_process(p.pid)), 2)
self.ae(c.root_pid, command_pid)
if kind == 'exec':
self.ae(c.resolved_root_pid, p.pid)
elif kind == 'busy':
self._send_line(p)
self.ae(self._read_line(p), 'done')
# The command has exited, leaving only the wrapper
self.ae(c.root_pid, p.pid)
self.assertIsNone(c.resolved_root_pid)
finally:
p.stdin.close()
self._stop_tree(p)
def test_launch_wrapper_root_pid_with_hold(self):
# The hold kitten runs wrappers specified by name via their path
from kitty.launch_wrapper import LaunchRequest
if not shutil.which('timeout'):
self.skipTest('No timeout program')
command = ('/bin/sh', '-c', self.report_pid, 'arg')
c, exe, argv = self._fork(argv=command, launch_request=LaunchRequest('tab'), hold=True, user_func=lambda ctx: ['timeout', '100'])
with subprocess.Popen(argv, executable=exe, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=0) as p:
c.pid = p.pid
try:
command_pid = int(self._read_line(p))
self.ae(c.root_pid, command_pid)
finally:
self._stop_tree(p)
def test_launch_wrapper(self):
from kitty.launch_wrapper import LaunchRequest