Skip to content

Preserve virtual task environments in TaskHosts - #15032

Merged
JanProvaznik merged 13 commits into
mainfrom
rainersigwald-coordinator-restore-deadlock
Oct 19, 2026
Merged

JanProvaznik merged 13 commits into
mainfrom
rainersigwald-coordinator-restore-deadlock

Conversation

@rainersigwald

@rainersigwald rainersigwald commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Related issue(s):

Fixes: #14986

copilot writes:

Context

Under -mt, a task can add an environment variable before any TaskHost launches without changing the parent's process-global environment. The variable reaches the first task configuration, but TaskHost reconciliation compares that configuration with its inherited startup environment and treats every difference as a bitness adjustment. It consequently replaces the supplied value with the startup value, including deleting a newly added variable. The first task's environment was incorrectly serving as a proxy for the launcher's environment at process creation.

For static-graph restore, this strips MSBUILDCOORDINATORGRANTID from the RestoreTaskEx TaskHost. The nested NuGet process then requests capacity held by its waiting parent, causing a deadlock.

Changes Made

Make the supplied task environment authoritative for ordinary variables. Restrict startup reconciliation to the six documented Windows WOW64 architecture/Program Files variables, retaining their existing forward and reverse mappings. Unix hosts apply none of these Windows-specific adjustments. This corrects the reconciliation policy rather than special-casing the coordinator token or adding a launch-time environment exchange.

Add focused regression coverage for environment additions/replacements/removals, task mutations, host reuse, concurrent projects, cross-bitness execution, nested coordinator grants, and static-graph restore. Update the related documentation. Review feedback reduces the process-test cases from 22 to 10, retaining the inexpensive policy-test coverage.

Compatibility

The correction uses Change Wave 18.13. MSBUILDDISABLEFEATURESFROMVERSION=18.13 restores legacy reconciliation and can reintroduce the deadlock. No public APIs, diagnostics, or packet formats change. The modern .NET and .NET Framework hosts receive the fix; the separate legacy CLR2 host is unchanged.

Testing

Validation after applying review feedback in a7e52e5b91:

  • build.cmd -v quiet /bl:1.binlog succeeded with no errors. It reported 204 NuGet dependency-audit warnings; no package references were changed by this PR.
  • Bootstrap SDK/version, MSBuild help, and src/Samples/Dependency/Dependency.csproj build succeeded.
  • 67 focused cases passed across net472 and net11.0: 42 policy cases, 13 environment end-to-end cases, and 12 coordinator integration cases. One .NET Framework-only case was correctly skipped on .NET.
  • The .NET Framework test process ran as x86 and passed the x64 TaskHost architecture regression. Coordinator cases require both parent and child to receive grants under a one-node budget; static-graph restore uses cleared package sources.
  • Before the original fix, the MT addition/replacement/removal regressions failed as expected.

The preceding PR head passed Windows, Linux (including MT), macOS, and source-build CI. CI for the feedback commit must complete before merging.

Dependencies and Follow-up

None. This does not depend on enlightening NuGet's static-graph restore task.

rainersigwald and others added 10 commits September 8, 2026 18:25
Restrict startup reconciliation to documented Windows architecture variables so virtualized task environments, including coordinator grant IDs, survive task-host execution. Preserve legacy reconciliation behind the Wave 18.12 opt-out.

Cover snapshot changes, host reuse, concurrent builds, cross-bitness execution, nested grants, and static-graph restore; document the corrected behavior.

Fixes #14986

Co-authored-by: Copilot App <[email protected]>
Replace overlapping BuildManagers, which MT strict mode now rejects, with concurrent project requests in a single build.

Use enlightened setters followed by TaskHost readers to verify that distinct per-project environment values survive reconciliation without leaking into the parent process. Assert the execution PIDs to cover the in-process-to-TaskHost boundary directly.

Co-authored-by: Copilot App <[email protected]>
@rainersigwald
rainersigwald marked this pull request as ready for review October 15, 2026 02:02
Copilot AI balanced review requested due to automatic review settings October 15, 2026 02:02

Copilot AI 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.

🔵 Needs a closer look

Cross-process environment changes need human sign-off, with post-merge validation and actual Unix execution still outstanding.

0 open findings

What changed in this PR

Preserves virtual task environments in TaskHosts to prevent the coordinator/static-graph restore deadlock described in #14986.

Changes:

  • Limits reconciliation to Windows architecture variables, with legacy behavior available through the Wave 18.12 opt-out.
  • Adds environment, host-reuse, cross-bitness, and coordinator regression coverage.
  • Documents environment propagation and compatibility behavior.
File Description
src/​MSBuild/​TaskHostEnvironment.cs Introduces scoped environment reconciliation.
src/​MSBuild/​OutOfProcTaskHostNode.cs Applies the wave-gated reconciliation policy.
src/​MSBuild/​MSBuild.csproj Includes the new helper.
src/​MSBuild.UnitTests/​TaskHostEnvironment_Tests.cs Tests platform and legacy policies.
src/​MSBuild.Coordinator.UnitTests/​CoordinatorIntegration_Tests.cs Covers nested grants and static-graph restore.
src/​Build.UnitTests/​BackEnd/​TaskHostEnvironment_E2E_Tests.cs Tests propagation, isolation, reuse, and cross-bitness execution.
documentation/​wiki/​ChangeWaves.md Records Wave 18.12 behavior and opt-out risks.
documentation/​specs/​multithreading/​taskhost-threading.md Documents reconciliation and retained state.
documentation/​MSBuild-Coordinator.md Clarifies grant-token propagation and deadlock prevention.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@JanProvaznik JanProvaznik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wrong changewave, and I'd tune down the tests a bit, strategy ok

Comment thread documentation/wiki/ChangeWaves.md Resolved
Comment thread documentation/MSBuild-Coordinator.md Resolved
Comment thread src/MSBuild/OutOfProcTaskHostNode.cs Resolved
Use Change Wave 18.13, trim redundant process-test combinations, and shorten coordinator documentation.

Co-authored-by: Copilot App <[email protected]>
rainersigwald and others added 2 commits October 19, 2026 09:32
Keep the change-wave check inside TaskHostEnvironment so the legacy branch and its gate can be retired together. Exercise the actual wave opt-out in policy tests and retain coverage for earlier-wave opt-outs.

Update the coordinator and threading documentation to identify the correct wave.

Co-authored-by: Copilot App <[email protected]>
Keep the upstream-focused process regressions and shortened documentation while retaining the helper-local Wave 18.13 check and its policy tests.

Co-authored-by: Copilot App <[email protected]>
@JanProvaznik
JanProvaznik merged commit 36e738a into main Oct 19, 2026
14 checks passed
@JanProvaznik
JanProvaznik deleted the rainersigwald-coordinator-restore-deadlock branch October 19, 2026 15:52
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.

Deadlock when coordinator, static-graph restore, and MT are all used

4 participants