-
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 2 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,53 @@ 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); | ||
| } | ||
|
|
||
| CreateAssetFile(packageId, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.AssetsFileName), _runtimeJsonPath, verbosity, targetFramework); | ||
| // Wait for the mutex with a reasonable timeout | ||
| if (!mutex.WaitOne(TimeSpan.FromMinutes(5))) | ||
| { | ||
| throw new ToolPackageException(string.Format(CliStrings.ToolInstallationTimeout, packageId, packageVersion)); | ||
| } | ||
|
||
|
|
||
| // 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.
I worry about the scenario where a computer running Windows has two Jenkins agents installed as services with separate user accounts and separate file-system directories, and the agents run
dotnet tool installon the same tool in parallel. Then this code will construct the samemutexNamestring in both processes, and because all services run in session 0, they will attempt to open the same mutex object; but because they have separate user accounts, the DACL of the mutex might not allow the second open.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.
So I'd suggest including a hash of the directory path where the tool is going to be installed.