Skip to content

Fix static state bugs in build tasks - #17428

Merged
ViktorHofer merged 1 commit into
dotnet:mainfrom
ViktorHofer:fix-static-task-state
Aug 28, 2026
Merged

ViktorHofer merged 1 commit into
dotnet:mainfrom
ViktorHofer:fix-static-task-state

Conversation

@ViktorHofer

@ViktorHofer ViktorHofer commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

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. Now only successful loads are cached.
  • GetCompatiblePackageTargetFrameworks accumulates into static collections from Initialize(), which runs on every Execute() and never clears them, so repeated invocations keep appending duplicates. Made 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. Switched to Random.Shared.

Also switched the two remaining static task caches to ConcurrentDictionary, which lets TargetFrameworkResolver.CreateOrGet collapse to GetOrAdd.

Shrinks the diff for #17378.

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
Copilot AI lite review requested due to automatic review settings August 27, 2026 16:41

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.

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 GetCompatiblePackageTargetFrameworks task state instance-scoped to prevent cross-invocation accumulation/duplication.
  • Fixed GetPackageDescription to only cache successful loads (avoids memoizing transient I/O failures) and moved the cache to ConcurrentDictionary.
  • Removed a shared static Random from AzureDevOpsTask retry backoff and switched to Random.Shared; updated TargetFrameworkResolver cache to ConcurrentDictionary with GetOrAdd.
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
ViktorHofer enabled auto-merge (squash) August 27, 2026 16:44
@ViktorHofer
ViktorHofer merged commit d0b892b into dotnet:main Aug 28, 2026
17 checks passed
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
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 29, 2026
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.

3 participants