Skip to content

fix(process-daemon): spawn Windows pipe processes suspended before joining their job - #64901

Merged
neriousy merged 3 commits into
devfrom
store-pwsh-spawn
Nov 19, 2026
Merged

neriousy merged 3 commits into
devfrom
store-pwsh-spawn

Conversation

@neriousy

@neriousy neriousy commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #64803
Closes #63892

Type of change

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

What does this PR do?

On Windows with PowerShell 7 from the Microsoft Store, running two shell commands at once makes one of them fail with failed to spawn ...\WindowsApps\pwsh.exe: (os error 5).

The daemon put each pipe child in its kill-on-close job with PROC_THREAD_ATTRIBUTE_JOB_LIST. With that attribute the new process is in our job before Windows adds it to the Store package's own container job, and Windows refuses that second step while another process of the same package is still running (AppModel events 212/208, 0x80070005). So the first command works and every command started while it runs is refused. The jobless retry then either fails too (the report) or runs the command outside the job, so cancelling it can't reach its children.

There is a second bug on dev: when the first command's job-list spawn succeeds, the package's container job ends up inside that command's job. Killing that command then kills every other Store pwsh command running at the same time (they exit with 1).

The child is now created with CREATE_SUSPENDED, assigned with AssignProcessToJobObject, then resumed. It can't run or start anything before it is in the job, so the tree is still fully contained. Our job now sits inside the package's job instead of the other way round, so killing one command only kills that command. Removing the job list also covers the real packaged path (C:\Program Files\WindowsApps\...\pwsh.exe), which #63892 reports failing with 0xC0070005.

Other changes:

  • Any AssignProcessToJobObject failure now falls back to running without the job, not only error 5. Errors from creating the process itself still fail the spawn as before. SPEC.md is updated to match.
  • The suspended child is terminated if the spawn fails or unwinds before it is resumed, so an error can't leave it running.

How did you verify your code works?

On Windows 11 with Store PowerShell 7.7.0:

  • Reproduced on dev through spawn: with one Store pwsh running, every further spawn was refused the job (cannot join the job object ... os error 5). In 6 rounds of 2 simultaneous spawns, each round had exactly one refusal, as in the report.
  • With the fix, all of them join their job and exit with the right code, through both the alias and the real packaged path, including 6 spawns started at once from separate threads.
  • Added store_powershell_joins_its_job_while_another_instance_runs. It starts two Store pwsh commands with the full environment, kills the first while the second is still running, then checks that the second still exits with its own code. It fails on dev and passed 10 runs out of 10 with the fix. It returns early where Store PowerShell isn't installed, as on CI.
  • Checked the new spawn order for regressions:
    • ResumeThread reports a suspend count of 1 for both cmd.exe and Store pwsh, so Store app activation doesn't start the process early.
    • Joining works from a process that is already inside a job, including one with UI restrictions.
    • cmd.exe, batch files, Windows PowerShell 5.1 and Git Bash still join their job and report their exit code.
  • Unchanged limitation: processes that Store pwsh itself starts (e.g. [Diagnostics.Process]::Start('ping.exe')) are in no job at all, on dev and with this PR, so killing the command doesn't reach them. Windows starts them outside every job, including the daemon's own. Fixed separately in the stacked fix(process-daemon): keep children of Store apps in the Windows pipe job #64902.
  • cargo fmt --check, cargo clippy --all-targets --all-features -- -D warnings and cargo test --all-targets --all-features in packages/process-daemon all pass, including the Windows pipe tests that kill grandchildren through the job.

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

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Fixes #63892

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks sound. I couldn't verify it: it needs Windows with PowerShell 7 from the Microsoft Store, and this sandbox is Linux with no Rust toolchain, so I reviewed the diff by hand.

The new order (create suspended, put the child in the job, then resume) keeps the whole process tree inside the job, because the child can't start anything before it is in. The fallback when the job refuses the child still works. If resuming fails, the child is killed before it ever ran. One behaviour change: the old jobless retry only fired on error 5, and now any refusal from the job falls back to running without it. That is what also covers the 0xC0070005 case.

The new test is small, skips itself where Store PowerShell isn't installed, and kills its 30-second command explicitly. No concerns there.

Public API: unchanged (internal change to the daemon's Windows spawn path).

Fixes #63892

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit looks sound. As before, I reviewed it by reading the code: it needs Windows with Store PowerShell 7, and this sandbox has neither Windows nor a Rust toolchain.

The suspended child is now killed whenever spawning fails before it is resumed, including when the resume itself fails. The extended test now also checks that killing one Store PowerShell command doesn't kill another one running at the same time, and it cleans up its processes if an assertion fails. The SPEC.md wording matches the new, broader fallback.

Public API: unchanged.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit looks sound. As before, I reviewed it by reading the code: it needs Windows with Store PowerShell 7, and this sandbox has neither Windows nor a Rust toolchain.

Taking the two handles straight from a successful CreateProcessW is fine, since both are valid at that point. A child that is never resumed is still killed on drop. The test now passes the full environment to the shells, as OpenCode does.

Public API: unchanged.

@neriousy
neriousy merged commit 773b9d0 into dev Nov 19, 2026
19 checks passed
@neriousy
neriousy deleted the store-pwsh-spawn branch November 19, 2026 16:53
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant