Repository navigation
Fix MSB0001 on concurrent BuildManager creation - #15522
JanProvaznik merged 1 commit into
Conversation
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]>
There was a problem hiding this comment.
🟡 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.CompareExchangeto 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
There was a problem hiding this comment.
| # | 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.comgithub.compatchdiff.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-proxySee 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

Creating multiple
BuildManagerinstances concurrently can fail withMSB0001: 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: