Skip to content

Make in-process ResolveAssemblyReference cancellable - #14959

Merged
rainersigwald merged 6 commits into
dotnet:mainfrom
JanProvaznik:janprovaznik-cancellable-assembly-resolution
Oct 1, 2026
Merged

rainersigwald merged 6 commits into
dotnet:mainfrom
JanProvaznik:janprovaznik-cancellable-assembly-resolution

Conversation

@JanProvaznik

Copy link
Copy Markdown
Member

Fixes #12268

Context

Canceling a build currently waits for ResolveAssemblyReference to finish its work. This is particularly noticeable with large reference sets and dependency graphs. Add cooperative cancellation for in-process RAR without changing normal resolution behavior.

Changes Made

  • Implement ICancelableTask and propagate a cancellation token through reference resolution, dependency traversal, conflict handling, and output/logging loops. Handle this task's cancellation by returning false without introducing cancellation diagnostics.
  • Keep cache serialization uninterrupted once it starts, and balance the affected tracing events when execution unwinds.
  • Fix a pre-existing cache-isolation problem found while reviewing cancellation safety: dependency remapping modified arrays and nested remapping sets owned by the process-wide metadata cache. Remapping now uses task-local results so later tasks do not inherit another invocation's remappings.
  • Add deterministic cancellation and recovery tests, including shared folder-cache reuse, cold cache initialization, complete output metadata comparisons, and positive cache-write controls. Document cancellation semantics.

Testing

  • Full repository build and bootstrap sample build succeeded.
  • Scoped RAR suites passed: 52 tests on net11.0 and 433 on net472, including 18 cancellation cases on each runtime. Four cache-isolation regressions failed before the isolation fix and passed afterward.
  • Local end-to-end BuildManager stress probe: 20,000 references across 64 search paths. Eight cancellation runs, triggered after RAR entered closure computation, returned from EndBuild() in 57-73 ms. The non-cancellable SDK RAR baseline on the same engine took 33 seconds after cancellation.
  • Local same-process recovery probe: 512 generated signed primary assemblies, transitive dependencies, a real version conflict, and related/satellite/serializer outputs. Cold and warm cancellation, subsequent recovery, and timestamp invalidation preserved the expected output paths and metadata. Cancellation preserved the existing state file; timestamp invalidation updated it. CWD, PATH, and cultures remained unchanged.

The end-to-end probes are local validation artifacts, not committed integration tests.

Notes

  • In-process only; out-of-process RAR behavior and communication are unchanged.
  • Cancellation is cooperative, not a guarantee of bounded latency during synchronous I/O or cache initialization. A cache write already in progress may finish before the task returns false.
  • No new warnings/errors or ChangeWave. Uncancelled resolution semantics are preserved apart from preventing shared-cache contamination.

JanProvaznik and others added 2 commits September 7, 2026 14:22
Keep cached dependency arrays and nested remapping sets unchanged. Add cancellation recovery regressions and meaningful cache-write controls.

Co-authored-by: Copilot App <[email protected]>
@JanProvaznik

Copy link
Copy Markdown
Member Author

@YuliiaKovalova inspired by our conversation

@JanProvaznik

Copy link
Copy Markdown
Member Author

supersedes #14235

@JanProvaznik
JanProvaznik marked this pull request as ready for review September 22, 2026 14:44
Copilot AI lite review requested due to automatic review settings September 22, 2026 14:44

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.

Copilot review overview

🟡 Changes recommended

Unresolved cancellation coverage gaps and a critical assembly-identity remapping issue must be addressed.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 3 Medium severity

Open (4)
What changed in this PR

Adds cooperative cancellation to in-process ResolveAssemblyReference while isolating remapping state and preserving normal resolution behavior.

Changes:

  • Propagates cancellation through resolution, traversal, conflicts, logging, and output.
  • Prevents shared metadata-cache contamination during remapping.
  • Adds cancellation/recovery tests and documentation.
File Description
src/​Tasks/​AssemblyDependency/​Resolver.cs Adds cancellation checks during resolution.
src/​Tasks/​AssemblyDependency/​ResolveAssemblyReference.cs Implements cancellation orchestration and handling.
src/​Tasks/​AssemblyDependency/​ReferenceTable.cs Propagates cancellation and isolates remapping state.
src/​Tasks/​AssemblyDependency/​AssemblyResolution.cs Passes cancellation through resolver creation.
src/​Tasks.UnitTests/​AssemblyDependency/​CancellationTests.cs Adds cancellation, recovery, and cache-isolation tests.
documentation/​wiki/​ResolveAssemblyReference.md Documents cancellation behavior.

@JanProvaznik

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

❌ Expert Code Review (command) failed. Please review the logs for details.

Warning

Threat Detection Engine Failure — The analysis engine could not complete. This is a tooling failure, not a security finding.

What happened

The threat detection engine failed to produce results.

Review the workflow run logs for details.

Warning

Firewall blocked 3 domains

The following domains were blocked by the firewall during workflow execution:

  • api.github.com
  • github.com
  • raw.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-proxy

See 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"
    - "raw.githubusercontent.com"

See Network Configuration for more information.

Generated by Expert Code Review (command) for #14959

Reject pre-canceled remote dispatch, preserve completed state-file bookkeeping, avoid repeated remapping-set copies, and observe cancellation in remaining reference/output scans. Keep focused cancellation, cache recovery, and remapping isolation regressions.

Co-authored-by: Copilot App <[email protected]>
@JanProvaznik
JanProvaznik enabled auto-merge (squash) September 29, 2026 12:11
@rainersigwald
rainersigwald disabled auto-merge October 1, 2026 15:23
@rainersigwald
rainersigwald merged commit cfbb310 into dotnet:main Oct 1, 2026
14 checks passed
@JanProvaznik

Copy link
Copy Markdown
Member Author

/backport to vs18.12

@JanProvaznik

Copy link
Copy Markdown
Member Author

/backport to vs18.11

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Started backporting to vs18.12 (link to workflow run)

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Started backporting to vs18.11 (link to workflow run)

JanProvaznik added a commit that referenced this pull request Oct 14, 2026
Fixes #12268

### Context

Canceling a build currently waits for ResolveAssemblyReference to finish
its work. This is particularly noticeable with large reference sets and
dependency graphs. Add cooperative cancellation for in-process RAR
without changing normal resolution behavior.

### Changes Made

- Implement `ICancelableTask` and propagate a cancellation token through
reference resolution, dependency traversal, conflict handling, and
output/logging loops. Handle this task's cancellation by returning
`false` without introducing cancellation diagnostics.
- Keep cache serialization uninterrupted once it starts, and balance the
affected tracing events when execution unwinds.
- Fix a pre-existing cache-isolation problem found while reviewing
cancellation safety: dependency remapping modified arrays and nested
remapping sets owned by the process-wide metadata cache. Remapping now
uses task-local results so later tasks do not inherit another
invocation's remappings.
- Add deterministic cancellation and recovery tests, including shared
folder-cache reuse, cold cache initialization, complete output metadata
comparisons, and positive cache-write controls. Document cancellation
semantics.

### Testing

- Full repository build and bootstrap sample build succeeded.
- Scoped RAR suites passed: 52 tests on `net11.0` and 433 on `net472`,
including 18 cancellation cases on each runtime. Four cache-isolation
regressions failed before the isolation fix and passed afterward.
- Local end-to-end `BuildManager` stress probe: 20,000 references across
64 search paths. Eight cancellation runs, triggered after RAR entered
closure computation, returned from `EndBuild()` in 57-73 ms. The
non-cancellable SDK RAR baseline on the same engine took 33 seconds
after cancellation.
- Local same-process recovery probe: 512 generated signed primary
assemblies, transitive dependencies, a real version conflict, and
related/satellite/serializer outputs. Cold and warm cancellation,
subsequent recovery, and timestamp invalidation preserved the expected
output paths and metadata. Cancellation preserved the existing state
file; timestamp invalidation updated it. CWD, PATH, and cultures
remained unchanged.

The end-to-end probes are local validation artifacts, not committed
integration tests.

### Notes

- In-process only; out-of-process RAR behavior and communication are
unchanged.
- Cancellation is cooperative, not a guarantee of bounded latency during
synchronous I/O or cache initialization. A cache write already in
progress may finish before the task returns `false`.
- No new warnings/errors or ChangeWave. Uncancelled resolution semantics
are preserved apart from preventing shared-cache contamination.

---------

Co-authored-by: Copilot App <[email protected]>
JanProvaznik added a commit that referenced this pull request Oct 14, 2026
# NET 11 Ask Mode

## Description
RAR is a long running MSBuild task. When you cancel a build, it needs to
finish running before MSBuild exits (seconds-minutes in large builds).
This is perceived by customers as nonresponsivity/hang, especially in VS
(which motivates this backport primarily since it's a top hang trace
https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3074347),
The experience in non-VS CLI is also bad, that after cancelation from
customer point of view nothing happens until RAR finishes.
This change adds a cancelation token that can be signaled in many places
of the RAR task, making the shutdown fast when requested.
## Regression? (was it working in a previous release or preview?)
No

## Risk (see
[taxonomy](https://dev.azure.com/devdiv/DevDiv/_wiki/wikis/DevDiv.wiki/545/NET-Servicing#risk-taxonomy))
Low


Backport of #14959 to vs18.12

---------

Co-authored-by: Jan Provazník <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: b298eb1e-ffbe-414b-a2ad-f4255a11c4a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResolveAssemblyReferences should be cancellable

4 participants