NuGet/Home

Restore Task cannot be safely run twice in one process (when Credential providers are used)

Closed this issue · 1 comments

Details about Problem

This is the root cause of dotnet/msbuild#4792 and the related .NET Core servicing build failures.

NuGet product used: .NET Core SDK 3.0.100-preview6 in Arcade

NuGet version: 5.2.0.6020

OS version (i.e. win10 v1607 (14393.321)): Windows

Worked before? If so, with which NuGet version: no

Detailed repro steps so we can see the same problem

I experienced this on a repro machine provided by @nkolev92. I don't have a minimized repro but I believe it looks something like this:

  1. Call RestoreTask in one project.
  2. In the same MSBuild build session, call RestoreTask in another project.
  3. Experience MSBuild crash MSB0001: Internal MSBuild Error: ContextID 10 should have been in the ID-to-project file mapping but wasn't!

What's happening here is this:

  1. The first call to RestoreTask creates a new NuGet ILogger wrapping a Microsoft.Build.Utilities.TaskLoggingHelper associated with that task invocation -- call this logger $1.
    https://github.com/NuGet/NuGet.Client/blob/840b09a4cecda6ae8fd1c07967874fe8061ece63/src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs#L103
  2. RestoreTask.ExecuteAsync() calls DefaultCredentialServiceUtility.SetupDefaultCredentialService, passing that logger.
  3. That populates the static field HttpHandlerResourceV3.CredentialService, preserving a link to the TaskLoggingHelper in an object with a longer lifetime than the task.
  4. The first RestoreTask completes.
  5. MSBuild tears down logging infrastructure required to log messages from the context of the first RestoreTask. Normally this would make $1 eligible for GC, but it's now referenced from a static.
  6. The second RestoreTask is called.
  7. On this pass through, HttpHandlerResourceV3.CredentialService is non-null and not reset to the current TaskLoggingHelper.
  8. The credential provider is invoked and attempts to log (using $1).
  9. MSBuild crashes because of the logging infrastructure teardown from step 5.

Setting to 5.3 milestone for now...as we may need to service? Fix is out to partner team for testing.