Fix IBuildEngine9.RequestCores returning 0 causing ReleaseCores(0) to throw in WASM/Mono build tasks#125714
Closed
Fix IBuildEngine9.RequestCores returning 0 causing ReleaseCores(0) to throw in WASM/Mono build tasks#125714
Conversation
… throw, and catch broader exceptions for task host compatibility Co-authored-by: pavelsavara <271576+pavelsavara@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [browser][docker] Fix MSBuild task host error with EmccCompile
Fix IBuildEngine9.RequestCores returning 0 causing ReleaseCores(0) to throw in WASM/Mono build tasks
Mar 18, 2026
Member
|
@copilot check when this code was really introduced and update "regression" section in the PR description |
Contributor
Author
Traced the history:
So the root regression is .NET 9 (PR #104141). Updating the PR description now. |
This was referenced Mar 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Defensive fix across WASM/Mono MSBuild tasks to avoid crashes and task-host failures when negotiating CPU cores via IBuildEngine9 in constrained or out-of-proc environments.
Changes:
- Guard
ReleaseCores(...)by only keepingbe9whenRequestCores(...)returns a positive grant. - Broaden the
RequestCores(...)exception handling to cover task-host callback failures (e.g., MSB5022 scenarios). - Apply the same pattern consistently across four tasks (
EmccCompile,MonoAOTCompiler,ILStrip,EmitBundleBase).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| src/tasks/WasmAppBuilder/EmccCompile.cs | Prevents ReleaseCores(0) crash and avoids task-host callback failures around parallel emcc compilation. |
| src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs | Applies the same RequestCores/ReleaseCores guarding to IL stripping parallelism. |
| src/tasks/MonoTargetsTasks/EmitBundleTask/EmitBundleBase.cs | Applies the same defensive core-request logic to resource bundling parallelism. |
| src/tasks/AotCompilerTask/MonoAOTCompiler.cs | Applies the same defensive core-request logic to AOT compilation parallelism. |
Comment on lines
+139
to
+144
| int requestedCores = be9.RequestCores(allowedParallelism); | ||
| if (requestedCores > 0) | ||
| allowedParallelism = requestedCores; | ||
| else | ||
| be9 = null; | ||
| } |
Comment on lines
+146
to
150
| catch (Exception) | ||
| { | ||
| // RequestCores is not implemented in TaskHostFactory | ||
| // RequestCores is not implemented in TaskHostFactory, or IBuildEngine callbacks are not supported | ||
| be9 = null; | ||
| } |
Comment on lines
+82
to
+87
| int requestedCores = be9.RequestCores(allowedParallelism); | ||
| if (requestedCores > 0) | ||
| allowedParallelism = requestedCores; | ||
| else | ||
| be9 = null; | ||
| } |
Comment on lines
+89
to
93
| catch (Exception) | ||
| { | ||
| // RequestCores is not implemented in TaskHostFactory | ||
| // RequestCores is not implemented in TaskHostFactory, or IBuildEngine callbacks are not supported | ||
| be9 = null; | ||
| } |
Comment on lines
+162
to
+167
| int requestedCores = be9.RequestCores(allowedParallelism); | ||
| if (requestedCores > 0) | ||
| allowedParallelism = requestedCores; | ||
| else | ||
| be9 = null; | ||
| } |
Comment on lines
+169
to
173
| catch (Exception) | ||
| { | ||
| // RequestCores is not implemented in TaskHostFactory | ||
| // RequestCores is not implemented in TaskHostFactory, or IBuildEngine callbacks are not supported | ||
| be9 = null; | ||
| } |
Comment on lines
+549
to
+554
| int requestedCores = be9.RequestCores(allowedParallelism); | ||
| if (requestedCores > 0) | ||
| allowedParallelism = requestedCores; | ||
| else | ||
| be9 = null; | ||
| } |
Comment on lines
+556
to
560
| catch (Exception) | ||
| { | ||
| // RequestCores is not implemented in TaskHostFactory | ||
| // RequestCores is not implemented in TaskHostFactory, or IBuildEngine callbacks are not supported | ||
| be9 = null; | ||
| } |
Member
|
Fixed by dotnet/msbuild#13345 |
This was referenced Mar 18, 2026
Open
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.
IBuildEngine9.RequestCores(n)can return 0 in resource-constrained CI Docker environments. CallingReleaseCores(0)then throwsArgumentOutOfRangeException. Additionally, when the task runs in an out-of-process MSBuild task host, IBuildEngine callbacks fail with MSB5022 — an exception type not covered by the existingcatch (NotImplementedException).Description
Four MSBuild tasks shared the same broken
IBuildEngine9usage pattern:EmccCompile(WasmAppBuilder)MonoAOTCompilerILStripEmitBundleBaseChanges applied consistently to all four:
Guard
ReleaseCores(0): Store the return value ofRequestCoresin a local; only updateallowedParallelismand keepbe9non-null when the grant is> 0. When 0 is returned, setbe9 = nullso the finally-blockbe9?.ReleaseCores(...)is skipped.Broaden exception catch:
catch (NotImplementedException)→catch (Exception)to also handle the MSB5022 error thrown by the out-of-process task host when IBuildEngine callbacks are unsupported.Customer Impact
WASM/Mono AOT publish fails with an unhandled
ArgumentOutOfRangeExceptionor MSB5022 when using/p:DisableParallelEmccCompile=true /p:DisableParallelAot=truein resource-constrained Docker CI environments. Build is completely broken in that configuration.Regression
Yes — the root regression was introduced in .NET 9 by PR #104141 (commit
f8bcb89, Jul 2024), which addedReleaseCorescalls to all four tasks without guarding againstRequestCoresreturning 0. A partial mitigation was added in .NET 10 by PR #111751 (commit2eb6459, Jan 2025), which introducedcatch (NotImplementedException)to handle task host failures and extended the pattern toEmitBundleBase, but the 0-return-value case remained unfixed. The issue was first reported against .NET 11 preview 3, where it surfaced in resource-constrained Docker CI environments.Testing
Verified all three affected projects (
WasmAppBuilder,MonoAOTCompiler,MonoTargetsTasks) build cleanly with 0 errors/warnings. The failure is environment-specific (Docker + resource constraints + task host), so automated reproduction requires that setup.Risk
Low. The fix is a narrow, consistent change to parallelism bookkeeping that is purely defensive — on the normal code path (task host supports IBuildEngine9 and grants cores), behavior is identical to before.
Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.