-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix concurrent tool installation race conditions with named mutex #51834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,26 +272,60 @@ protected void DownloadTool( | |
| string? targetFramework, | ||
| VerbosityOptions verbosity) | ||
| { | ||
| // Use a named mutex to serialize concurrent installations of the same tool package | ||
| string mutexName = GetToolInstallMutexName(packageId, packageVersion); | ||
| using var mutex = new Mutex(false, mutexName); | ||
|
|
||
| if (!IsPackageInstalled(packageId, packageVersion, packageDownloadDir.Value)) | ||
| try | ||
| { | ||
| DownloadAndExtractPackage(packageId, nugetPackageDownloader, packageDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: givenSpecificVersion, verbosity: verbosity); | ||
| } | ||
| // First try a quick check to see if the mutex is immediately available | ||
| if (!mutex.WaitOne(TimeSpan.FromMilliseconds(50))) | ||
| { | ||
| // Mutex is held by another process - inform the user | ||
| Reporter.Error.WriteLine(string.Format(CliStrings.ToolInstallationWaiting, packageId, packageVersion)); | ||
|
|
||
| CreateAssetFile(packageId, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.AssetsFileName), _runtimeJsonPath, verbosity, targetFramework); | ||
| // Now wait for the longer duration | ||
| if (!mutex.WaitOne(TimeSpan.FromMinutes(5))) | ||
| { | ||
| throw new ToolPackageException(string.Format(CliStrings.ToolInstallationTimeout, packageId, packageVersion)); | ||
| } | ||
| } | ||
|
Comment on lines
+279
to
+292
|
||
|
|
||
| // Also download RID-specific package if needed | ||
| if (ResolveRidSpecificPackage(packageId, packageVersion, packageDownloadDir, assetFileDirectory, verbosity) is PackageId ridSpecificPackage) | ||
| { | ||
| if (!IsPackageInstalled(ridSpecificPackage, packageVersion, packageDownloadDir.Value)) | ||
| if (!IsPackageInstalled(packageId, packageVersion, packageDownloadDir.Value)) | ||
| { | ||
| DownloadAndExtractPackage(ridSpecificPackage, nugetPackageDownloader, packageDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: true, verbosity: verbosity); | ||
| DownloadAndExtractPackage(packageId, nugetPackageDownloader, packageDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: givenSpecificVersion, verbosity: verbosity); | ||
| } | ||
|
|
||
| CreateAssetFile(ridSpecificPackage, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.RidSpecificPackageAssetsFileName), _runtimeJsonPath, verbosity, targetFramework); | ||
| CreateAssetFile(packageId, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.AssetsFileName), _runtimeJsonPath, verbosity, targetFramework); | ||
|
|
||
| // Also download RID-specific package if needed | ||
| if (ResolveRidSpecificPackage(packageId, packageVersion, packageDownloadDir, assetFileDirectory, verbosity) is PackageId ridSpecificPackage) | ||
| { | ||
| if (!IsPackageInstalled(ridSpecificPackage, packageVersion, packageDownloadDir.Value)) | ||
| { | ||
| DownloadAndExtractPackage(ridSpecificPackage, nugetPackageDownloader, packageDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: true, verbosity: verbosity); | ||
| } | ||
|
|
||
| CreateAssetFile(ridSpecificPackage, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.RidSpecificPackageAssetsFileName), _runtimeJsonPath, verbosity, targetFramework); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| mutex.ReleaseMutex(); | ||
| } | ||
|
Comment on lines
+312
to
315
|
||
| } | ||
|
|
||
| private static string GetToolInstallMutexName(PackageId packageId, NuGetVersion packageVersion) | ||
| { | ||
| // Create a mutex name in the format: tool-install-{packageId}-{packageVersion} | ||
| // Replace characters that are invalid in mutex names with underscores | ||
| string safeName = $"tool-install-{packageId}-{packageVersion.ToNormalizedString()}" | ||
| .Replace('/', '_') | ||
| .Replace('\\', '_'); | ||
|
|
||
| return safeName; | ||
| } | ||
|
Comment on lines
+318
to
+327
|
||
|
|
||
| public bool TryGetDownloadedTool( | ||
| PackageId packageId, | ||
| NuGetVersion packageVersion, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The waiting message is written to
Reporter.Error, but this is informational rather than an error. According to the PR description, this message is meant to provide "transparency about what's happening." Consider usingReporter.Outputinstead, as this is normal operational information, not an error condition.