Repository navigation
[main] Fix TaskHost routing order after nested build callbacks - #15112
Merged
Merged
Conversation
Fixes #15067. ## Ordering bug A TaskHost normally runs one task at a time, but a task waiting for a nested build can let another task use the same process. The earlier task can then resume first. The parent remembered the last task started, not the task that had resumed. If A waited, then B waited, and A got its node back first, the parent still sent A's messages to B. In the reproducer, B's child needed A's target result: A's completion waited in B's queue while B waited for A. ## Fix When a task gets its node back after a nested build, the parent first restores the TaskHost's association with that task, then sends back the build results. The existing callback response already identifies which wait to release in the TaskHost. No extra identity needs to be sent. The TaskHost already saves a task's working directory and environment before it waits. When the response arrives, its existing `RequestId` wakes the correct callback, and the task restores its saved environment before `BuildProjectFile` returns. That part is unchanged: the missing step was restoring the parent's association before sending the reply. ```mermaid sequenceDiagram participant H as TaskHost participant P as Parent H->>P: A calls BuildProjectFile for ChildA Note over H: A waits H->>P: B calls BuildProjectFile for ChildB Note over H,P: B waits and ChildB needs target A P->>P: ChildA finishes and A gets its node back P->>P: Remember A as the current task P-->>H: A's build results with the existing RequestId H->>P: A's next callback or completion Note over P: Deliver to A ``` Other waiting tasks remain registered, and late replies cannot affect a replacement TaskHost. Finished tasks leave their results in a queue so one result cannot overwrite another before it is sent. Console output is sent before the corresponding result. There are no packet-format changes, additional invocation identifiers, or protocol-version changes. The same fix applies to .NET and full-framework MSBuild. ## How this was validated The reproducer builds two targets of the **same project configuration** through two parallel wrappers, using `-m:2 -nr:false`. File gates establish the ordering rather than relying on sleeps: 1. A's child announces that it is waiting. 2. Only then can B enter its nested build. 3. B's child releases A's child, then requests A's target result. This forces A to return while B remains blocked. Before the fix, sender-side traces showed A returning but neither parent target completing, and the MT build reached the 120-second bound. Normal mode served as the control. The same graph was run against locally built .NET MSBuild and full-framework `MSBuild.exe`. The MT runs verify that A and B used the same external TaskHost. Runtime traces distinguish `System.Private.CoreLib` from `mscorlib`, so the CLR4 result is not inferred from the driver's target framework. Each parent must receive its own role and process-ID outputs exactly once, and the call/return log entries must keep the original parent task ID. Both runtimes now complete with those checks satisfied. The edge cases keep three tasks attached to one host, resume the earliest one, remove another, and check which task receives the next message. A closed connection is checked both before and after a replacement is attached. For result delivery, two results are queued together and another is added during console flushing, checking that no result is lost, duplicated, or sent ahead of its buffered output. Cancellation coverage exercises active and waiting task contexts and isolated AppDomains. --------- Co-authored-by: Copilot App <[email protected]>
JanProvaznik
approved these changes
Sep 23, 2026
JanProvaznik
enabled auto-merge (squash)
September 23, 2026 13:09
This was referenced Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15067.
Ordering bug
A TaskHost normally runs one task at a time, but a task waiting for a nested build can let another task use the same process. The earlier task can then resume first.
The parent remembered the last task started, not the task that had resumed. If A waited, then B waited, and A got its node back first, the parent still sent A's messages to B. In the reproducer, B's child needed A's target result: A's completion waited in B's queue while B waited for A.
Fix
When a task gets its node back after a nested build, the parent first restores the TaskHost's association with that task, then sends back the build results. The existing callback response already identifies which wait to release in the TaskHost. No extra identity needs to be sent.
The TaskHost already saves a task's working directory and environment before it waits. When the response arrives, its existing
RequestIdwakes the correct callback, and the task restores its saved environment beforeBuildProjectFilereturns. That part is unchanged: the missing step was restoring the parent's association before sending the reply.sequenceDiagram participant H as TaskHost participant P as Parent H->>P: A calls BuildProjectFile for ChildA Note over H: A waits H->>P: B calls BuildProjectFile for ChildB Note over H,P: B waits and ChildB needs target A P->>P: ChildA finishes and A gets its node back P->>P: Remember A as the current task P-->>H: A's build results with the existing RequestId H->>P: A's next callback or completion Note over P: Deliver to AOther waiting tasks remain registered, and late replies cannot affect a replacement TaskHost. Finished tasks leave their results in a queue so one result cannot overwrite another before it is sent. Console output is sent before the corresponding result.
There are no packet-format changes, additional invocation identifiers, or protocol-version changes. The same fix applies to .NET and full-framework MSBuild.
How this was validated
The reproducer builds two targets of the same project configuration through two parallel wrappers, using
-m:2 -nr:false. File gates establish the ordering rather than relying on sleeps:This forces A to return while B remains blocked. Before the fix, sender-side traces showed A returning but neither parent target completing, and the MT build reached the 120-second bound. Normal mode served as the control.
The same graph was run against locally built .NET MSBuild and full-framework
MSBuild.exe. The MT runs verify that A and B used the same external TaskHost. Runtime traces distinguishSystem.Private.CoreLibfrommscorlib, so the CLR4 result is not inferred from the driver's target framework. Each parent must receive its own role and process-ID outputs exactly once, and the call/return log entries must keep the original parent task ID. Both runtimes now complete with those checks satisfied.The edge cases keep three tasks attached to one host, resume the earliest one, remove another, and check which task receives the next message. A closed connection is checked both before and after a replacement is attached. For result delivery, two results are queued together and another is added during console flushing, checking that no result is lost, duplicated, or sent ahead of its buffered output. Cancellation coverage exercises active and waiting task contexts and isolated AppDomains.