Skip to content

[main] Fix TaskHost routing order after nested build callbacks - #15112

Merged
JanProvaznik merged 1 commit into
mainfrom
backport/pr-15069-to-main
Sep 23, 2026
Merged

JanProvaznik merged 1 commit into
mainfrom
backport/pr-15069-to-main

Conversation

@github-actions

@github-actions github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

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.

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
Loading

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.

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 JanProvaznik changed the title [main] [vs18.12] [NET 11 RC2] Fix TaskHost routing order after nested build callbacks [main] Fix TaskHost routing order after nested build callbacks Sep 23, 2026
@JanProvaznik
JanProvaznik enabled auto-merge (squash) September 23, 2026 13:09
@JanProvaznik
JanProvaznik merged commit c32fc90 into main Sep 23, 2026
14 checks passed
@JanProvaznik
JanProvaznik deleted the backport/pr-15069-to-main branch September 23, 2026 14:15
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.

TaskHost routes completion to the wrong invocation when nested builds finish out of order

1 participant