From a0ba8fd399c3ae54ba38b74710f8795969feb13c Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Tue, 18 Mar 2025 10:33:40 -0400 Subject: [PATCH] Exit hosted runner cleanly during deprovisioning. (#3755) --- src/Runner.Common/BrokerServer.cs | 2 +- src/Runner.Listener/BrokerMessageListener.cs | 5 ++++ src/Runner.Listener/MessageListener.cs | 5 ++++ src/Runner.Listener/Runner.cs | 4 ++++ src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs | 1 + src/Sdk/WebApi/WebApi/BrokerHttpClient.cs | 2 ++ .../HostedRunnerDeprovisionedException.cs | 23 +++++++++++++++++++ 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/Sdk/WebApi/WebApi/Exceptions/HostedRunnerDeprovisionedException.cs diff --git a/src/Runner.Common/BrokerServer.cs b/src/Runner.Common/BrokerServer.cs index 7ca4713f7..b0774178f 100644 --- a/src/Runner.Common/BrokerServer.cs +++ b/src/Runner.Common/BrokerServer.cs @@ -93,7 +93,7 @@ namespace GitHub.Runner.Common public bool ShouldRetryException(Exception ex) { - if (ex is AccessDeniedException || ex is RunnerNotFoundException) + if (ex is AccessDeniedException || ex is RunnerNotFoundException || ex is HostedRunnerDeprovisionedException) { return false; } diff --git a/src/Runner.Listener/BrokerMessageListener.cs b/src/Runner.Listener/BrokerMessageListener.cs index 7a0fe903d..2a994631e 100644 --- a/src/Runner.Listener/BrokerMessageListener.cs +++ b/src/Runner.Listener/BrokerMessageListener.cs @@ -249,6 +249,11 @@ namespace GitHub.Runner.Listener Trace.Info("Runner OAuth token has been revoked. Unable to pull message."); throw; } + catch (HostedRunnerDeprovisionedException) + { + Trace.Info("Hosted runner has been deprovisioned."); + throw; + } catch (AccessDeniedException e) when (e.ErrorCode == 1) { throw; diff --git a/src/Runner.Listener/MessageListener.cs b/src/Runner.Listener/MessageListener.cs index 9ea44b3f5..d7bf9ba32 100644 --- a/src/Runner.Listener/MessageListener.cs +++ b/src/Runner.Listener/MessageListener.cs @@ -304,6 +304,11 @@ namespace GitHub.Runner.Listener _accessTokenRevoked = true; throw; } + catch (HostedRunnerDeprovisionedException) + { + Trace.Info("Hosted runner has been deprovisioned."); + throw; + } catch (AccessDeniedException e) when (e.ErrorCode == 1) { throw; diff --git a/src/Runner.Listener/Runner.cs b/src/Runner.Listener/Runner.cs index 834c249e3..850f128c0 100644 --- a/src/Runner.Listener/Runner.cs +++ b/src/Runner.Listener/Runner.cs @@ -697,6 +697,10 @@ namespace GitHub.Runner.Listener { Trace.Info("Runner OAuth token has been revoked. Shutting down."); } + catch (HostedRunnerDeprovisionedException) + { + Trace.Info("Hosted runner has been deprovisioned. Shutting down."); + } return Constants.Runner.ReturnCode.Success; } diff --git a/src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs b/src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs index 12022a839..48295a1a1 100644 --- a/src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs +++ b/src/Sdk/RSWebApi/Contracts/BrokerErrorKind.cs @@ -7,5 +7,6 @@ namespace GitHub.Actions.RunService.WebApi { public const string RunnerNotFound = "RunnerNotFound"; public const string RunnerVersionTooOld = "RunnerVersionTooOld"; + public const string HostedRunnerDeprovisioned = "HostedRunnerDeprovisioned"; } } diff --git a/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs b/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs index e445b4738..cd16c1e73 100644 --- a/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs +++ b/src/Sdk/WebApi/WebApi/BrokerHttpClient.cs @@ -122,6 +122,8 @@ namespace GitHub.Actions.RunService.WebApi { ErrorCode = 1 }; + case BrokerErrorKind.HostedRunnerDeprovisioned: + throw new HostedRunnerDeprovisionedException(brokerError.Message); default: break; } diff --git a/src/Sdk/WebApi/WebApi/Exceptions/HostedRunnerDeprovisionedException.cs b/src/Sdk/WebApi/WebApi/Exceptions/HostedRunnerDeprovisionedException.cs new file mode 100644 index 000000000..c9ae7e4b4 --- /dev/null +++ b/src/Sdk/WebApi/WebApi/Exceptions/HostedRunnerDeprovisionedException.cs @@ -0,0 +1,23 @@ +using System; + +namespace GitHub.Services.WebApi +{ + [Serializable] + public sealed class HostedRunnerDeprovisionedException : Exception + { + public HostedRunnerDeprovisionedException() + : base() + { + } + + public HostedRunnerDeprovisionedException(String message) + : base(message) + { + } + + public HostedRunnerDeprovisionedException(String message, Exception innerException) + : base(message, innerException) + { + } + } +}