Skip to content

Commit 5b85111

Browse files
Address PR feedback: remove unused s_guard, add jitter, clarify docs
- Remove unused s_guard field (would cause CS0414 warning/build failure) - Add random jitter (0-50ms) to retry delays to prevent thundering-herd collisions between processes retrying simultaneously - Clarify MaxRetryAttempts doc to distinguish retries from total attempts Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3ea5317 commit 5b85111

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/Cli/dotnet/Commands/Workload/List/VisualStudioWorkloads.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ namespace Microsoft.DotNet.Cli.Commands.Workload.List;
2121
#endif
2222
internal static class VisualStudioWorkloads
2323
{
24-
private static readonly object s_guard = new();
25-
2624
/// <summary>
2725
/// Named mutex used to serialize cross-process access to the VS Setup Configuration COM API,
2826
/// which is not safe for concurrent calls from multiple processes.
@@ -31,15 +29,21 @@ internal static class VisualStudioWorkloads
3129
private const string VsSetupConfigurationMutexName = @"Global\DotNetSdk_VSSetupConfiguration";
3230

3331
/// <summary>
34-
/// Maximum number of retry attempts when the COM API fails due to concurrent access.
32+
/// Maximum number of retries after the initial attempt fails due to concurrent access.
33+
/// Total attempts = 1 (initial) + MaxRetryAttempts.
3534
/// </summary>
3635
private const int MaxRetryAttempts = 3;
3736

3837
/// <summary>
39-
/// Base delay in milliseconds between retry attempts (doubled on each retry).
38+
/// Base delay in milliseconds between retry attempts (doubled on each retry, plus a random offset).
4039
/// </summary>
4140
private const int RetryBaseDelayMs = 100;
4241

42+
/// <summary>
43+
/// Maximum random offset in milliseconds added to retry delays to stagger concurrent processes.
44+
/// </summary>
45+
private const int RetryRandomOffsetMaxMs = 50;
46+
4347
/// <summary>
4448
/// Visual Studio product ID filters. We dont' want to query SKUs such as Server, TeamExplorer, TestAgent
4549
/// TestController and BuildTools.
@@ -161,8 +165,9 @@ internal static unsafe void GetInstalledWorkloads(
161165
}
162166
catch (Exception) when (attempt < MaxRetryAttempts)
163167
{
164-
// Retry with exponential backoff for transient COM failures.
165-
Thread.Sleep(RetryBaseDelayMs * (1 << attempt));
168+
// Retry with exponential backoff plus a random offset for transient COM failures.
169+
int delay = RetryBaseDelayMs * (1 << attempt) + Random.Shared.Next(0, RetryRandomOffsetMaxMs);
170+
Thread.Sleep(delay);
166171
}
167172
}
168173
}

0 commit comments

Comments
 (0)