Avoid caching helper PIDs for launch wrappers

A user launch wrapper can start a persistent helper before the command. Use a lone, unmatched child for the current lookup and cache the child only once its command line matches the launch arguments.
This commit is contained in:
Zhizhen He 2026-11-18 12:04:49 +08:00
parent 8133effda3
commit fea2fff082
2 changed files with 14 additions and 7 deletions

View file

@ -784,8 +784,10 @@ class Child:
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
if w.script_args is None:
return min(children), len(children) == 1
# A sole child can be a helper that stays alive after the wrapped
# command starts. Cache it only if its command line matches.
running_command = []
for c in children:
with suppress(Exception): # child has exited

View file

@ -412,8 +412,13 @@ exit $?"""
echo done
read -r _
"""
# Runs another command in the background, alongside the command
background_wrapper = 'sleep 60 &\ntrap "kill $!" EXIT\n"$@"\n'
# Starts a background helper before the command and keeps it alive
background_wrapper = """sleep 60 &
trap "kill $!" EXIT
printf "%s\\n" "$!"
read -r _
"$@"
"""
with tempfile.TemporaryDirectory() as tdir, patch.dict(os.environ, {'PATH': tdir + os.pathsep + os.environ.get('PATH', '')}):
name = 'launch-wrapper'
@ -441,15 +446,15 @@ read -r _
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
elif kind in ('busy', 'background'):
# Query while the helper is the wrapper's only 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(set(children_of_process(p.pid)), {helper_pid, command_pid})
self.ae(c.root_pid, command_pid)
if kind == 'exec':
self.ae(c.resolved_root_pid, p.pid)