Skip to content

Handle TimeoutException During Agent Replacement in agent configuration #5210

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

Merged
merged 11 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 70 additions & 20 deletions src/Agent.Listener/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Newtonsoft.Json;
using Microsoft.Win32;
using Microsoft.VisualStudio.Services.Agent.Listener.Telemetry;
using Microsoft.TeamFoundation.Test.WebApi;

namespace Microsoft.VisualStudio.Services.Agent.Listener.Configuration
{
Expand All @@ -45,6 +46,8 @@ public sealed class ConfigurationManager : AgentService, IConfigurationManager

private const string VsTelemetryRegPath = @"SOFTWARE\Microsoft\VisualStudio\Telemetry\PersistentPropertyBag\c57a9efce9b74de382d905a89852db71";
private const string VsTelemetryRegKey = "IsPipelineAgent";
private const int _maxRetries = 3;
private const int _delaySeconds = 2;

public override void Initialize(IHostContext hostContext)
{
Expand Down Expand Up @@ -252,18 +255,15 @@ public async Task ConfigureAsync(CommandSettings command)
{
// Update existing agent with new PublicKey, agent version and SystemCapabilities.
agent = UpdateExistingAgent(agent, publicKey, systemCapabilities);

try
agent = await UpdateAgentWithRetryAsync<TaskAgent>(
() => agentProvider.UpdateAgentAsync(agentSettings, agent, command),
command
);
if (agent != null)
{
agent = await agentProvider.UpdateAgentAsync(agentSettings, agent, command);
_term.WriteLine(StringUtil.Loc("AgentReplaced"));
break;
}
catch (Exception e) when (!command.Unattended())
{
_term.WriteError(e);
_term.WriteError(StringUtil.Loc("FailedToReplaceAgent"));
}
}
else if (command.Unattended())
{
Expand Down Expand Up @@ -455,6 +455,58 @@ public async Task ConfigureAsync(CommandSettings command)
}
}

private async Task<T> UpdateAgentWithRetryAsync<T>(
Func<Task<T>> operation,
CommandSettings command)
{
int attempt = 0;
while (true)
{
try
{
return await operation();
}
catch (Exception e) when (
e is TimeoutException ||
e is TaskCanceledException ||
(e is OperationCanceledException && !(e is TaskCanceledException))
)
{
attempt++;
if (command.Unattended())
{
if (attempt >= _maxRetries)
{
_term.WriteError(e);
_term.WriteError(StringUtil.Loc("FailedToReplaceAgent"));
Trace.Error($"{operation.Method.Name} failed after maximum retries. Exception: {e}");
throw new InvalidOperationException(StringUtil.Loc("FailedToReplaceAgent"), e);
}
else
{
Trace.Info($"Retrying operation, Attempt: '{attempt}'.");
int backoff = _delaySeconds * (int)Math.Pow(2, attempt - 1);
_term.WriteLine(StringUtil.Loc("RetryingReplaceAgent", attempt, _maxRetries, backoff));
await Task.Delay(TimeSpan.FromSeconds(backoff));
}
}
else
{
_term.WriteError(e);
_term.WriteError(StringUtil.Loc("FailedToReplaceAgent"));
break;
}
}
catch (Exception e)
{
_term.WriteError(e);
_term.WriteError(StringUtil.Loc("FailedToReplaceAgent"));
break;
}
}
return default(T);
}

public async Task UnconfigureAsync(CommandSettings command)
{
ArgUtil.NotNull(command, nameof(command));
Expand Down Expand Up @@ -695,19 +747,18 @@ public async Task ReAuthAsync(CommandSettings command)
}
else
{
try
agent.Authorization = new TaskAgentAuthorization
{
agent.Authorization = new TaskAgentAuthorization
{
PublicKey = new TaskAgentPublicKey(publicKey.Exponent, publicKey.Modulus),
};
agent = await agentProvider.UpdateAgentAsync(agentSettings, agent, command);
_term.WriteLine(StringUtil.Loc("AgentReplaced"));
}
catch (Exception e) when (!command.Unattended())
PublicKey = new TaskAgentPublicKey(publicKey.Exponent, publicKey.Modulus),
};
agent = await UpdateAgentWithRetryAsync<TaskAgent>(
() => agentProvider.UpdateAgentAsync(agentSettings, agent, command),
command
);
if (agent != null)
{
_term.WriteError(e);
_term.WriteError(StringUtil.Loc("FailedToReplaceAgent"));
_term.WriteLine(StringUtil.Loc("AgentReplaced"));
break;
}
}

Expand Down Expand Up @@ -838,7 +889,6 @@ private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey,
{
agent.SystemCapabilities[capability.Key] = capability.Value ?? string.Empty;
}

return agent;
}

Expand Down
1 change: 1 addition & 0 deletions src/Misc/layoutbin/en-US/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@
"RestartMessage": "Restart the machine to launch agent and for autologon settings to take effect.",
"ReStreamLogsToFilesError": "You cannot use --disableloguploads and --reStreamLogsToFiles at the same time!",
"RetryCountLimitExceeded": "The maximum allowed number of attempts is {0} but got {1}. Retry attempts count will be decreased to {0}.",
"RetryingReplaceAgent": "Retrying to replace agent (attempt {0} of {1}). Waiting {2} seconds before next attempt...",
"RMApiFailure": "Api {0} failed with an error code {1}",
"RMArtifactContainerDetailsInvalidError": "The artifact does not have valid container details: {0}",
"RMArtifactContainerDetailsNotFoundError": "The artifact does not contain container details: {0}",
Expand Down
Loading