Skip to content

Harden coordinator client lifecycle races - #14240

Merged
AR-May merged 5 commits into
dotnet:mainfrom
emaf:emaf-coordinator-hardening-fixes
Jul 22, 2026
Merged

AR-May merged 5 commits into
dotnet:mainfrom
emaf:emaf-coordinator-hardening-fixes

Conversation

@emaf

@emaf emaf commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Context

This PR hardens coordinator client/server lifecycle handling on top of #14224.

Coordinator client writes can come from multiple lifecycle paths: regular heartbeat timers, deferred-wait heartbeat pumps, and Dispose releasing a grant. Those paths need to avoid overlapping BinaryWriter writes and timer callbacks racing with connection ownership changes or disposal.

Changes Made

  • Keep coordinator client transport ownership centered on CoordinatorClient.Connection.
  • Serialize client message writes inside Connection, so heartbeats, deferred-wait heartbeats, and release messages share one write path.
  • Drain deferred-wait and client heartbeat timers before the associated connection can be disposed.
  • Track accepted coordinator server connections while they are negotiating so idle shutdown does not race an in-progress client.
  • Make deferred grant notification resilient when a waiting client disconnects while being granted nodes.
  • Make connected-client disposal idempotent.
  • Add targeted regression tests for client disposal, deferred-wait heartbeats, accepted-client shutdown timing, and disconnected deferred waiters.

Testing

  • .\build.cmd -v quiet
  • dotnet msbuild src\Build\Microsoft.Build.csproj -p:Configuration=Release -v:q
  • dotnet msbuild src\MSBuild.Coordinator\MSBuild.Coordinator.csproj -p:Configuration=Release -v:q
  • dotnet test src\MSBuild.Coordinator.UnitTests\MSBuild.Coordinator.UnitTests.csproj -c Release

@emaf
emaf changed the base branch from dev/dustinca/fix-issue-14170 to main July 17, 2026 14:14
@emaf
emaf marked this pull request as ready for review July 17, 2026 16:02
Copilot AI review requested due to automatic review settings July 17, 2026 16: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.

Pull request overview

Hardens MSBuild Coordinator client/server lifecycle and deferred-grant handling to reduce race conditions around heartbeats, disposal, and deferred waiters, building on prior coordinator refactoring.

Changes:

  • Centralizes coordinator client transport ownership on CoordinatorClient.Connection and serializes client writes.
  • Adds a dedicated heartbeat timer that drains in-flight callbacks during disposal / deferred-wait periods.
  • Improves server-side deferred-grant notification flow to handle disconnected waiters and queue additional grants.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/MSBuild.Coordinator/CoordinatorServer.cs Refactors grant release + deferred notification draining to better handle disconnected waiters.
src/MSBuild.Coordinator/CoordinatorServer.ConnectedClient.cs Makes connected-client disposal idempotent.
src/MSBuild.Coordinator.UnitTests/TestCoordinatorDebugOutput.cs Routes interpolated debug output through the same WriteLine(string) path.
src/MSBuild.Coordinator.UnitTests/CoordinatorClient_Tests.cs Adds regression tests for idempotent dispose and deferred-wait heartbeats; adjusts pipe server behavior.
src/Build/Microsoft.Build.csproj Includes new CoordinatorClient.HeartbeatTimer.cs in the build.
src/Build/BackEnd/BuildManager/CoordinatorClient.TestAccessor.cs Clarifies pipe-stream ownership transfer and ensures disposal in finally.
src/Build/BackEnd/BuildManager/CoordinatorClient.HeartbeatTimer.cs Introduces a disposable heartbeat timer that waits for in-flight callbacks.
src/Build/BackEnd/BuildManager/CoordinatorClient.cs Moves client heartbeats/writes to Connection and makes Dispose idempotent.
src/Build/BackEnd/BuildManager/CoordinatorClient.Connection.cs Makes connection own pipe/reader/writer directly, serializes writes, and makes disposal idempotent.

emaf added 2 commits July 17, 2026 20:37
Keep coordinator client ownership on Connection, serialize client writes, and drain heartbeat timers before disposing the transport.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: db19afae-f9ef-45b0-b7c2-e0ced2e134d8
Avoid recursive deferred-grant release handling and make connected-client disposal idempotent while preserving existing server write ownership.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: db19afae-f9ef-45b0-b7c2-e0ced2e134d8
emaf added 2 commits July 17, 2026 20:37
Cover idempotent client disposal and deferred-grant heartbeat behavior, and remove unused test debug output plumbing.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: db19afae-f9ef-45b0-b7c2-e0ced2e134d8
Address review feedback by disposing BinaryWriter instances in coordinator client tests while tolerating broken-pipe flush during disconnect verification.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: db19afae-f9ef-45b0-b7c2-e0ced2e134d8
@emaf
emaf force-pushed the emaf-coordinator-hardening-fixes branch from 901ed73 to d1ea28c Compare July 18, 2026 03:26

@DustinCampbell DustinCampbell 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.

Looks great @emaf! I had just one minor question.

Revert unnecessary changes to TestCoordinatorDebugOutput so the PR only carries coordinator lifecycle hardening changes.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: db19afae-f9ef-45b0-b7c2-e0ced2e134d8

@AR-May AR-May 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.

LGTM

@AlesProkop AlesProkop 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.

LGTM

@AR-May
AR-May enabled auto-merge (squash) July 22, 2026 15:01
@AR-May
AR-May merged commit a6ed776 into dotnet:main Jul 22, 2026
10 checks passed
AlesProkop added a commit that referenced this pull request Jul 29, 2026
Automated flaky-test quarantine (2026-07-27).

---

## Quarantine: `SingleBuild_CoordinatorCapsMaxNodeCount`

**Flaky-test key** (automated de-duplication — do not edit):
```text
flaky-test-id: Microsoft.Build.Coordinator.UnitTests.CoordinatorIntegration_Tests.SingleBuild_CoordinatorCapsMaxNodeCount
```

Tracked by #14516

- **Action**: quarantine (6a) — added
`[ActiveIssue("https://github.com/dotnet/msbuild/issues/14516")]`
- **File**:
`src/MSBuild.Coordinator.UnitTests/CoordinatorIntegration_Tests.cs`
- **Distinct sources**: 3 (3 PRs: #13609, #14240, #14470; 0 rolling
builds)
- **Legs**: CoreOnLinux, CoreOnWindows, FullOnWindows Release
- **TFMs**: net10.0, net11.0, net472
- **Error hashes**: 3 distinct (`4039adb7`, `55b6331b`, `ba217936`)
- **First seen**: 2026-07-17 | **Last seen**: 2026-07-23
- [Sample
build](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1523327)

---

## Also tracked this run (new issues filed; quarantine eligible next
run)

-
`Microsoft.Build.BuildCheck.UnitTests.EndToEndTests.TFMinNonSdkCheckTest`
(3 rolling builds, 4 error hashes)
-
`Microsoft.Build.Engine.UnitTests.MSBuildServer_Tests.ServerSpawnAndReuseAreLoggedToBuildLog`
(3 rolling builds, 3 error hashes)
- `Microsoft.Build.UnitTests.CommunicationUtilitiesTests.GetEnvVars` (3
rolling builds, 2 error hashes)

---

## Skipped (possible regression — human triage needed)

The following tests showed a **single stable error hash** failing across
the same set of 4 rolling builds with no PR sources, which may indicate
a regression rather than a flake:

- `Microsoft.Build.UnitTests.CodeTaskFactoryTests.*` (7 tests, hash
`2f59b830`, builds 1506834/1514373/1517682/1527742)
- `Microsoft.Build.UnitTests.CodeTaskFactoryTests.EmbedsGenerated*` (2
tests, hash `d888cdeb`, same builds)

These were **not quarantined** to avoid masking a real bug. Human
investigation recommended.




> [!WARNING]
> <details>
> <summary>Firewall blocked 2 domains</summary>
>
> The following domains were blocked by the firewall during workflow
execution:
>
> - `awmgmcpg`
> - `southcentralus0.in.applicationinsights.azure.com`
>> To allow these domains, add them to the `network.allowed` list in
your workflow frontmatter:
>
> ```yaml
> network:
>   allowed:
>     - defaults
>     - "awmgmcpg"
>     - "southcentralus0.in.applicationinsights.azure.com"
> ```
>
> See [Network
Configuration](https://github.github.com/gh-aw/reference/network/) for
more information.
>
> </details>


> Generated by [Flaky Test
Triage](https://github.com/dotnet/msbuild/actions/runs/30267259222) ·
sonnet46 · 153.8 AIC · ⌖ 6.72 AIC · ⊞ 14.7K ·
[◷](https://github.com/search?q=repo%3Adotnet%2Fmsbuild+%22gh-aw-workflow-id%3A+flaky-test-detector.agent%22&type=pullrequests)

<!-- gh-aw-agentic-workflow: Flaky Test Triage, engine: copilot,
version: 1.0.70, model: claude-sonnet-4.6, id: 30267259222, workflow_id:
flaky-test-detector.agent, run:
https://github.com/dotnet/msbuild/actions/runs/30267259222 -->

<!-- gh-aw-workflow-id: flaky-test-detector.agent -->
<!-- gh-aw-workflow-call-id: dotnet/msbuild/flaky-test-detector.agent
-->

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Aleš Prokop <[email protected]>
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.

5 participants