Skip to content

fix(process-daemon): put Store app processes that leave the job back in one - #65141

Open
neriousy wants to merge 1 commit into
store-pwsh-childrenfrom
nested-store-jobs
Open

neriousy wants to merge 1 commit into
store-pwsh-childrenfrom
nested-store-jobs

Conversation

@neriousy

Copy link
Copy Markdown
Member

Issue for this PR

Stacked on #64902. Fixes the cases it lists as not covered.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

#64902 keeps the children of a Store app in the pipe job when the daemon starts the Store app itself. Windows still lets two kinds of process leave the job, and then cancelling or timing out a command, or a daemon crash, leaves them running:

  • A Store app started further down the tree, e.g. Store pwsh, wsl.exe or winget.exe run by cmd.exe or another shell. Windows moves it into its package's own job. Ours can't take it back (AssignProcessToJobObject fails with access denied).
  • The children of such a Store app, which are in no job at all. This also happens for Store pwsh started by Store pwsh: the inner one stays in the job, but its children don't, because the desktop-app policy only reaches the daemon's own child.

Windows has no way to prevent either, so this adds a watcher (src/service/pipe/windows/tree.rs):

  • One thread for all pipe commands. It records every process that enters a command's job, from the job's completion-port messages, and keeps a handle open so the PID can't be reused.
  • Once a second, while any command runs, it takes a process snapshot and finds children of recorded processes that are in none of the command's jobs. If a child is older than its parent, its parent PID came from an earlier process, so it's skipped.
  • A process in no job joins the command's job. A Store app in its package's job gets a kill-on-close job of its own, nested inside the package job, which Windows does allow.
  • Killing the tree, or a failed exit, terminates every job, then sweeps the snapshot again for anything that escaped since the last poll. So a kill never waits for the poll. The interval only limits how long an escaped process sits outside a job, which only matters if the daemon dies.
  • A successful command still leaves its background processes running: kill-on-close is cleared on every job, as before.

A snapshot takes about 4 ms here (around 360 processes). At one poll a second that's about 0.4% of a core while commands run. That's why it doesn't poll faster.

The PTY path needs nothing: a Store pwsh terminal behaves like one running regular pwsh. Closing it kills everything attached to the console, and processes with their own console survive with either shell. That's normal terminal behaviour (so code . survives closing the tab), so I left it alone.

No flag: this is a fix in the daemon's kill path.

How did you verify your code works?

On Windows 11 with Store PowerShell 7.7.0:

  • New unit tests in tree.rs run on CI without Store apps. They fake an escaped child by creating it with the root as its parent (PROC_THREAD_ATTRIBUTE_PARENT_PROCESS) before the root joins the job, then check that:
    • the watcher puts the child back in the job;
    • a kill finds it even if the watcher never saw it;
    • a successful exit leaves it running.
  • New Store-only tests in tests/pipe_windows.rs, which return early on CI:
    • cmd.exe → Store pwsh → ping, killed;
    • Store pwsh → Store pwsh → ping, killed;
    • an escaped ping is put back in a job;
    • a failed exit kills a Store app that cmd.exe started in the background.
  • A Store-only test in tests/windows-daemon.rs kills the real daemon process after adoption and checks that the nested Store pwsh and its ping die with it.
  • With adoption disabled, all of the new tests fail except the "successful exit" one, and the existing signal_reaches_a_grandchild_of_store_powershell still passes.
  • cargo fmt --check, cargo clippy --all-targets --all-features -- -D warnings and cargo test --all-targets --all-features pass. The library, Windows pipe and daemon-crash tests passed 8 times in a row.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

This branch has not been deployed

No deployments
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