-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix RequestCores/ReleaseCores fallback in OOP TaskHost: throw NotImplementedException instead of logging error #13345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JanProvaznik
merged 2 commits into
dotnet:main
from
JanProvaznik:fix/requestcores-fallback
Mar 10, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/Build.UnitTests/BackEnd/RequestCoresWithFallbackTask.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| namespace Microsoft.Build.UnitTests.BackEnd | ||
| { | ||
| /// <summary> | ||
| /// A test task that calls IBuildEngine9.RequestCores with the same fallback pattern | ||
| /// used by real callers (MonoAOTCompiler, EmccCompile, ILStrip, EmitBundleBase): | ||
| /// try { cores = be9.RequestCores(N); } | ||
| /// catch (NotImplementedException) { be9 = null; /* use own estimate */ } | ||
| /// finally { be9?.ReleaseCores(cores); } | ||
| /// Used by regression tests for https://github.com/dotnet/msbuild/issues/13333 | ||
| /// </summary> | ||
| public class RequestCoresWithFallbackTask : Task | ||
| { | ||
| /// <summary> | ||
| /// Number of cores to request. Defaults to 1. | ||
| /// </summary> | ||
| public int CoreCount { get; set; } = 1; | ||
|
|
||
| /// <summary> | ||
| /// Whether to call ReleaseCores after the parallel work (via be9?.ReleaseCores pattern). | ||
| /// </summary> | ||
| public bool ReleaseAfter { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of cores granted (either from RequestCores or the fallback value). | ||
| /// </summary> | ||
| [Output] | ||
| public int GrantedCores { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// True if NotImplementedException was caught and the fallback was used. | ||
| /// </summary> | ||
| [Output] | ||
| public bool UsedFallback { get; set; } | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| int allowedParallelism = CoreCount; | ||
| IBuildEngine9? be9 = BuildEngine as IBuildEngine9; | ||
| try | ||
| { | ||
| if (be9 is not null) | ||
| { | ||
| allowedParallelism = be9.RequestCores(CoreCount); | ||
| } | ||
| } | ||
| catch (NotImplementedException) | ||
| { | ||
| // This is the expected path when callbacks are not supported. | ||
| // Real callers null out be9 so ReleaseCores is skipped. | ||
| Log.LogMessage(MessageImportance.High, "RequestCores threw NotImplementedException, using fallback"); | ||
| be9 = null; | ||
| UsedFallback = true; | ||
| } | ||
|
|
||
| GrantedCores = allowedParallelism; | ||
| Log.LogMessage(MessageImportance.High, $"GrantedCores = {GrantedCores}"); | ||
|
|
||
| // Simulate parallel work... | ||
|
|
||
| if (ReleaseAfter) | ||
| { | ||
| // Real callers use be9?.ReleaseCores — skipped when be9 was nulled in catch. | ||
| if (be9 is not null) | ||
| { | ||
| be9.ReleaseCores(allowedParallelism); | ||
| Log.LogMessage(MessageImportance.High, $"ReleaseCores({allowedParallelism}) completed"); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.