Skip to content

Avoid deadlock during credential service initialization - #7681

Merged
nkolev92 merged 2 commits into
NuGet:devfrom
sean-mcmanus:dev-sean-mcmanus-devbox2-wsl-agent78-fixCredentialDeadlock
Sep 15, 2026
Merged

nkolev92 merged 2 commits into
NuGet:devfrom
sean-mcmanus:dev-sean-mcmanus-devbox2-wsl-agent78-fixCredentialDeadlock

Conversation

@sean-mcmanus

@sean-mcmanus sean-mcmanus commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Bug

Fixes: NuGet/Home#8248

Related: NuGet/Home#9447

Related: #7676

Description

HttpHandlerResourceV3.CredentialService is exposed as a synchronous Lazy<ICredentialService>, but its deferred factory performs asynchronous Visual Studio work. When a background caller owns the outer Lazy<T> monitor while that work needs the UI thread, a UI-thread caller can block on the monitor before it has an opportunity to join the in-flight JTF operation.

This change preserves deferred initialization and the existing Lazy<ICredentialService> contract while moving the asynchronous work into one JTF-backed AsyncLazy<ICredentialService>. The compatibility Lazy<T> uses LazyThreadSafetyMode.PublicationOnly, allowing concurrent callers to reach and join the same inner task instead of serializing on the outer monitor. The helper also asserts that a JTF is present rather than silently constructing a non-joinable AsyncLazy<T>. The now-obsolete VSTHRD011 suppression is removed.

Because the public protocol surface remains synchronous, callers still block while AsyncLazy<T>.GetValue() joins the inner operation. Eliminating that blocking and addressing resolver paths with no shared JTF context would require a new cancellation-aware asynchronous credential API and migration of the protocol consumers; that broader design is outside this fix.

NuGet/Home#8248 was closed by stale cleanup without a fixing pull request and has since been reopened. NuGet/Home#9447 tracks the broader SDK-resolver authentication scenario; this change addresses the credential-initialization edge but does not claim to resolve every resolver scheduling path. NuGet.Client#7676 independently removes one obsolete UI-thread dependency from credential setup.

Testing

Added a deterministic Windows .NET Framework regression test that:

  1. Starts credential-service initialization from a background thread.
  2. Pauses the factory before it switches to the UI thread.
  3. Synchronously requests the same lazy value from the UI thread.
  4. Verifies both callers receive the same service and the provider factory runs exactly once.

The unchanged test was run against both implementations:

  • Previous implementation: exactly one test executed and failed after the 10-second bound with The main thread deadlocked waiting for credential service initialization.
  • Current implementation: the same test executed once and passed in approximately one second.

The normally signed NuGet.PackageManagement.VisualStudio production project also built successfully with threading analyzers enabled, including VSTHRD011. All current-head pull-request checks passed. A patched VSIX was not manually launched against a full Visual Studio solution; the regression test directly and deterministically exercises the background-initialization/UI-caller cycle addressed by this change.

This PR was investigated and created by GitHub Copilot in VS Code. Any message starting with ✨Copilot: was sent by GitHub Copilot.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc. (Not applicable; this does not change settings, environment variables, or add a feature.)

@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Sep 1, 2026
@sean-mcmanus

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Microsoft"

@sean-mcmanus
sean-mcmanus marked this pull request as ready for review September 1, 2026 21:35
@sean-mcmanus
sean-mcmanus requested a review from a team as a code owner September 1, 2026 21:35

@lifengl lifengl 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.

This PR is valid, and potentially addresses some narrow deadlock chances. Although it has no effect on the deadlock VC team ran into.

@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Sep 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 30 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@sean-mcmanus

Copy link
Copy Markdown
Contributor Author

@dotnet-bot I'm registering activity?

@dotnet-policy-service dotnet-policy-service Bot removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Sep 9, 2026
@nkolev92
nkolev92 merged commit eaf703a into NuGet:dev Sep 15, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy<> with value factory needing UI thread causes deadlock

4 participants