Repository navigation
Fix static state bugs in build tasks - #17428
Merged
Merged
Conversation
Static caches in MSBuild tasks outlive a single task invocation, and with node reuse they outlive the build itself. Three of them are wrong today: - GetPackageDescription caches whatever LoadDescriptions returns, which is null after it logs an IOException or UnauthorizedAccessException. That memoizes a transient failure, so every later invocation on the node reports "Unable to find description for package X" without ever saying why. Only cache successful loads. - GetCompatiblePackageTargetFrameworks accumulates into static collections from Initialize(), which runs on every Execute() and never clears them, so repeated invocations keep appending duplicates. Make the state instance state. - AzureDevOpsTask shares one static Random across its async retry paths. Random's instance methods are not thread safe, and concurrent use can wedge it into returning zero. Use Random.Shared. Also switch the two remaining static task caches to ConcurrentDictionary, which lets TargetFrameworkResolver.CreateOrGet collapse to GetOrAdd. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect static state in several Arcade/MSBuild tasks so behavior is correct under MSBuild node reuse and (future) multi-threaded task execution.
Changes:
- Made
GetCompatiblePackageTargetFrameworkstask state instance-scoped to prevent cross-invocation accumulation/duplication. - Fixed
GetPackageDescriptionto only cache successful loads (avoids memoizing transient I/O failures) and moved the cache toConcurrentDictionary. - Removed a shared
static RandomfromAzureDevOpsTaskretry backoff and switched toRandom.Shared; updatedTargetFrameworkResolvercache toConcurrentDictionarywithGetOrAdd.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs | Moves previously-static collections to instance state to prevent duplicate growth across invocations. |
| src/Microsoft.DotNet.PackageTesting.Tests/GetCompatibilePackageTargetFrameworksTests.cs | Updates tests to use an instance task API instead of static methods. |
| src/Microsoft.DotNet.Helix/Sdk/AzureDevOpsTask.cs | Makes retry backoff RNG thread-safe by using Random.Shared. |
| src/Microsoft.DotNet.Build.Tasks.TargetFramework/src/TargetFrameworkResolver.cs | Makes the resolver cache thread-safe and simplifies creation via GetOrAdd. |
| src/Microsoft.DotNet.Build.Tasks.Packaging/src/GetPackageDescription.cs | Avoids caching failed reads and makes the cache thread-safe with ConcurrentDictionary. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
ViktorHofer
enabled auto-merge (squash)
August 27, 2026 16:44
AlesProkop
approved these changes
Aug 28, 2026
ViktorHofer
added a commit
to ViktorHofer/arcade
that referenced
this pull request
Aug 28, 2026
Resolves conflicts with the four extracted PRs that have since merged: dotnet#17412 (console output to build log), dotnet#17426 (unused build tasks), dotnet#17427 (BuildTask base class) and dotnet#17428 (static task state). Conflict resolution: - Base classes take main's spelling. Main derives tasks from the short `Task` with Microsoft.Build.Utilities imported, except in the four files that also import System.Threading.Tasks and therefore keep the fully qualified name. The multithreading attribute and IMultiThreadableTask from this branch are layered on top. - The three tasks deleted as dead on main stay deleted, dropping their annotations with them. - GenAPITask.LogTextWriter and several using blocks were duplicated by the automatic merge; kept main's copy in each case. Main's LogTextWriter logs from a dedicated LogPendingLine rather than overriding Flush, so an explicit Flush() no longer emits a partial line. - Kept main's wording for the GetPackageDescription cache comment and its fix for the "erreleasea" typo. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
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.
Static caches in MSBuild tasks outlive a single task invocation, and with node reuse they outlive the build itself. Three of them are wrong today:
GetPackageDescriptioncaches whateverLoadDescriptionsreturns — which isnullafter it logs anIOExceptionorUnauthorizedAccessException. That memoizes a transient failure, so every later invocation on the node reportsUnable to find description for package Xwithout ever saying why. Now only successful loads are cached.GetCompatiblePackageTargetFrameworksaccumulates into static collections fromInitialize(), which runs on everyExecute()and never clears them, so repeated invocations keep appending duplicates. Made the state instance state.AzureDevOpsTaskshares onestatic Randomacross its async retry paths.Random's instance methods are not thread-safe and concurrent use can wedge it into returning zero. Switched toRandom.Shared.Also switched the two remaining static task caches to
ConcurrentDictionary, which letsTargetFrameworkResolver.CreateOrGetcollapse toGetOrAdd.Shrinks the diff for #17378.