Restore Task cannot be safely run twice in one process (when Credential providers are used)
Closed this issue · 1 comments
rainersigwald commented
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:
- Call
RestoreTask
in one project. - In the same MSBuild build session, call
RestoreTask
in another project. - 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:
- The first call to
RestoreTask
creates a new NuGetILogger
wrapping aMicrosoft.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 RestoreTask.ExecuteAsync()
callsDefaultCredentialServiceUtility.SetupDefaultCredentialService
, passing that logger.- That populates the static field
HttpHandlerResourceV3.CredentialService
, preserving a link to theTaskLoggingHelper
in an object with a longer lifetime than the task. - The first
RestoreTask
completes. - 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. - The second
RestoreTask
is called. - On this pass through,
HttpHandlerResourceV3.CredentialService
is non-null and not reset to the currentTaskLoggingHelper
. - The credential provider is invoked and attempts to log (using
$1
). - MSBuild crashes because of the logging infrastructure teardown from step 5.
rrelyea commented
Setting to 5.3 milestone for now...as we may need to service? Fix is out to partner team for testing.