Skip to content

Fix MSB0001 on concurrent BuildManager creation - #15522

Merged
JanProvaznik merged 1 commit into
dotnet:mainfrom
davkean:dev/davkean/fix-serialization-contract-race
Nov 11, 2026
Merged

JanProvaznik merged 1 commit into
dotnet:mainfrom
davkean:dev/davkean/fix-serialization-contract-race

Conversation

@davkean

@davkean davkean commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Creating multiple BuildManager instances concurrently can fail with MSB0001: Internal MSBuild Error: Serialization contract was already initialized. Multiple constructors can begin initializing the exception serialization contract before any of them publishes it, causing the later callers to throw.

The contract is identical for each caller, so concurrent initialization should keep the first published contract without failing the other constructors.

These peaks represent all the times we hit this in the lab:

image

Creating BuildManager instances on several threads at the same time can
make one of them throw "Serialization contract was already initialized".
The early-out check is not atomic, so two threads can both build the
exception factory map and the second to publish it throws.

Every caller builds an equivalent map, so keep whichever was published
first and ignore the rest.

Co-authored-by: Copilot <[email protected]>
@davkean
davkean marked this pull request as ready for review November 11, 2026 06:23
Copilot AI balanced review requested due to automatic review settings November 11, 2026 06:23
@davkean
davkean temporarily deployed to copilot-pat-pool November 11, 2026 06:23 — with GitHub Actions Inactive
@davkean
davkean temporarily deployed to copilot-pat-pool November 11, 2026 06:23 — with GitHub Actions Inactive
@davkean
davkean temporarily deployed to copilot-pat-pool November 11, 2026 06:24 — with GitHub Actions Inactive

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.

🟡 Changes recommended

A deterministic regression test is still needed to verify that concurrent first-time initialization completes without exceptions and leaves the registered factory usable.

1 open issue
What changed in this PR

Fixes the internal serialization-contract error that can occur when multiple BuildManager instances are constructed concurrently.

Changes:

  • Uses Interlocked.CompareExchange to retain the first published contract without throwing for competing initializers.
  • Adds a comment explaining why concurrent callers can safely use the first contract.
File Description
src/​Framework/​BuildException/​BuildExceptionSerializationHelper.cs Makes concurrent contract publication first-writer-wins instead of throwing.

Review effort: Balanced

@github-actions github-actions 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.

# Dimension Verdict
4 Test Coverage & Completeness 🟡 1 MAJOR

✅ 23/24 dimensions clean.

  • Test Coverage & Completeness — add a deterministic regression test for concurrent serialization-contract initialization

Warning

Firewall blocked 3 domains

The following domains were blocked by the firewall during workflow execution:

  • api.github.com
  • github.com
  • patchdiff.githubusercontent.com

[!TIP]
api.github.com is blocked because GitHub API access uses the built-in GitHub tools by default. Instead of adding api.github.com to network.allowed, use tools.github.mode: gh-proxy for direct pre-authenticated GitHub CLI access without requiring network access to api.github.com:

tools:
  github:
    mode: gh-proxy

See GitHub Tools for more information on gh-proxy mode.

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.github.com"
    - "github.com"
    - "patchdiff.githubusercontent.com"

See Network Configuration for more information.

Generated by Expert Code Review (on open) for #15522 · copilot · gpt56 · 420 AIC · ⌖ 0.53 AIC · ⊞ 20.9K

@davkean
davkean temporarily deployed to copilot-pat-pool November 11, 2026 06:37 — with GitHub Actions Inactive
@JanProvaznik
JanProvaznik merged commit 0f5373f into dotnet:main Nov 11, 2026
27 checks passed

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 4e49dab6 Deployed Nov 11, 2026 by davkean via conclusion #1145
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.

4 participants