Skip to content

fix(core): kill a command's escaped descendants at teardown - #511

Merged
lg2m merged 4 commits into
mainfrom
fix/command-tree-descendants
Oct 30, 2026
Merged

lg2m merged 4 commits into
mainfrom
fix/command-tree-descendants

Conversation

@lg2m

@lg2m lg2m commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Change

On Unix, a command whose descendants leave its process group (GNU timeout, setsid) kept running after cancel, timeout, or the end of the run, reparented to PID 1. timeout 300 cargo test 2>&1 | tail was the common case.

QQ still launches commands without a helper process. Now, at teardown, it stops the group, then walks parent PIDs from it: /proc/<pid>/stat on Linux, ps -A -o pid=,ppid=,pgid= on macOS. It stops each descendant it finds and rescans until a scan finds nothing new, for at most 64 rounds. Then it kills every descendant and the group. Confirmation waits for every collected PID (pidfds on Linux), not only the group. The walk runs in spawn_blocking.

shell and exec also set PWD to the command's working directory and drop the inherited OLDPWD.

Verification

  • cancel_and_timeout_kill_descendants_that_left_the_group, a_yielded_escaped_tree_dies_when_the_run_ends, and commands_see_their_working_directory_in_pwd_and_no_oldpwd fail on origin/main: the marked PIDs are still alive after teardown, and OLDPWD is inherited. All three pass with this change. Each test finds its processes with ps by a unique sleep argv marker and polls with a bound; no fixed sleeps.
  • cargo nextest run -p qq-core -E 'test(/shell|process|teardown|yield/)': 92/92 on Linux.
  • cargo xtask check: passed locally. The full CI gate passed on 5a36851e.
  • macOS: run in place (macos-latest) runs the three new tests with Homebrew GNU timeout and util-linux setsid, and passes: https://github.com/retsu-AI/qq/actions/runs/41016563967
  • On macOS, the first CI run on this branch showed that a zombie-only group returns EPERM on the cancel and timeout path too, not only after a normal exit. Every teardown path now treats that as killed.

Known impact

Not cleaned up:

  • A descendant that left the group and whose parent had already exited when teardown began, such as a double-forked daemon or a background setsid job from a command that already finished.
  • When QQ itself is SIGKILLed or crashes, only the original group dies, through the existing watcher. Escaped descendants survive in that case. Covering it needs a process that outlives QQ, which this change deliberately does not add.

On macOS, PIDs are tracked by number, not pidfd, so a PID reused during the bounded confirmation window could be misread as still alive. That would report the cleanup as unobserved; it never kills an unrelated process. docs/guide/permissions.md states these limits.

sessions::tests::approvals::goal_parent_deadline_preserves_a_sponsored_childs_unanswered_approval failed on its first try in two CI gate runs on this branch and passed on retry, which nextest flags as flaky. It runs no shell command. It passed 150/150 locally under concurrent load, and the final run passed after a rerun of the failed job. I have not found a link to this change.

GNU timeout and setsid move processes out of the command's group, so
killing the group left them running under PID 1. Teardown now freezes the
group, follows parent PIDs to every descendant until a scan finds nothing
new, kills them all, and confirms each one exited. Commands also get PWD set
to their working directory and no inherited OLDPWD.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T17:06:25.971240Z d5a07df Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 624f8969fe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/qq-core/src/tools.rs Resolved
Comment thread crates/qq-core/src/tools.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a36851ee9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

… during teardown

Teardown no longer sends SIGSTOP to the whole group, which froze the
watcher that kills the tree if QQ dies mid-collection. It stops each member
individually and skips the watcher. On macOS, a stopped PID is resumed and
dropped unless a fresh snapshot still shows it in the command's tree, so a
recycled PID is never killed. The PWD test now runs a program that reads the
environment directly, with a known stale PWD and OLDPWD, and test process
probes no longer block the runtime.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m

lg2m commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: d5a07df2a7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lg2m
lg2m merged commit 43ef1e4 into main Oct 30, 2026
19 of 21 checks passed
@lg2m
lg2m deleted the fix/command-tree-descendants branch October 30, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant