Skip to content

Commit a4882ff

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 a4882ff

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

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

Lines changed: 12 additions & 7 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,14 +29,20 @@ 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).
39+
/// </summary>
40+
private const int RetryBaseDelayMilliseconds = 100;
41+
42+
/// <summary>
43+
/// Maximum random offset in milliseconds added to retry delays to stagger concurrent processes.
4044
/// </summary>
41-
private const int RetryBaseDelayMs = 100;
45+
private const int RetryRandomOffsetMaxMilliseconds = 50;
4246

4347
/// <summary>
4448
/// Visual Studio product ID filters. We dont' want to query SKUs such as Server, TeamExplorer, TestAgent
@@ -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 = RetryBaseDelayMilliseconds * (1 << attempt) + Random.Shared.Next(0, RetryRandomOffsetMaxMilliseconds);
170+
Thread.Sleep(delay);
166171
}
167172
}
168173
}

0 commit comments

Comments
 (0)