From 8fe8b2bfeb529666d5392b87e500c214a846d2fc Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Wed, 18 Nov 2026 09:30:03 +0800 Subject: [PATCH] Fix root PID tracking for script launch wrappers Recognize the original script invocation after the interpreter arguments so root_pid can follow wrappers that run the command as a child. --- kitty/child.py | 22 ++++++++++++++++++++-- kitty_tests/child.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/kitty/child.py b/kitty/child.py index 1c24ab08e..d4e1ee9f4 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -374,6 +374,22 @@ class KittyWrapper(NamedTuple): # True if the wrapper eventually replaces 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. + script_args: tuple[str, ...] | None = None + + def matches_cmdline(self, cmdline: tuple[str, ...]) -> bool: + if cmdline[: len(self.argv)] == self.argv: + return True + 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: + return False + script = cmdline[script_index] + return script == self.argv[0] or os.path.basename(script) == self.argv[0] child_counter = count() @@ -572,7 +588,9 @@ 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 - kitty_wrappers.append(KittyWrapper(user_prefix, True)) + # 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)) if self.hold: hold_argv = cmdline_for_hold() argv = hold_argv + argv @@ -731,7 +749,7 @@ class Child: except Exception: final = False break - if cmdline[: len(w.argv)] != w.argv: + if not w.matches_cmdline(cmdline): # Either the wrapper has replaced itself, which is # permanent, or it has not been exec-ed yet and the forked # process is still running kitty code diff --git a/kitty_tests/child.py b/kitty_tests/child.py index 3e176080d..0c60c3e57 100644 --- a/kitty_tests/child.py +++ b/kitty_tests/child.py @@ -4,6 +4,7 @@ import os import subprocess import tempfile +from select import select from types import SimpleNamespace from unittest.mock import patch @@ -301,6 +302,32 @@ class LaunchWrapperTest(BaseTest): return func + def test_script_launch_wrapper_root_pid(self): + from kitty.launch_wrapper import LaunchRequest + + req = LaunchRequest('tab') + 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): + with open(script, 'w') as f: + f.write('#!/bin/sh -e\n' + ('exec "$@"\n' if execs else '"$@"\nexit $?\n')) + 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: + 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 + self.ae(c.root_pid, command_pid) + finally: + p.stdin.close() + def test_launch_wrapper(self): from kitty.launch_wrapper import LaunchRequest @@ -319,7 +346,7 @@ class LaunchWrapperTest(BaseTest): self.ae(exe, '/bin/wrapper') self.ae(c.argv, ['/bin/prog', 'arg']) self.ae(c.final_exe, '/bin/prog') - self.ae(c.kitty_wrappers, (KittyWrapper(('/bin/wrapper', '-x'), True),)) + self.ae(c.kitty_wrappers, (KittyWrapper(('/bin/wrapper', '-x'), True, ('-x', '/bin/prog', 'arg')),)) ctx = self.contexts[-1] self.ae(ctx.argv, ('/bin/prog', 'arg')) self.ae(ctx.cwd, '/') @@ -336,7 +363,7 @@ class LaunchWrapperTest(BaseTest): c, exe, argv = self._fork(launch_request=req, hold=True, user_func=self._recording(['/bin/wrapper'])) self.ae(argv[-2:], ('/bin/prog', 'arg')) self.ae(argv[-3], '/bin/wrapper') - self.ae(c.kitty_wrappers[-1], KittyWrapper(('/bin/wrapper',), True)) + self.ae(c.kitty_wrappers[-1], KittyWrapper(('/bin/wrapper',), True, ('/bin/prog', 'arg'))) self.ae(len(c.kitty_wrappers), 2) self.ae(c.kitty_wrappers[0].argv, argv[: len(c.kitty_wrappers[0].argv)]) # Failures and invalid return values result in no prefix