diff --git a/src/Runner.Listener/BrokerMessageListener.cs b/src/Runner.Listener/BrokerMessageListener.cs index 595db8ace14..447439863eb 100644 --- a/src/Runner.Listener/BrokerMessageListener.cs +++ b/src/Runner.Listener/BrokerMessageListener.cs @@ -141,7 +141,9 @@ public async Task CreateSessionAsync(CancellationToken toke Trace.Error("Catch exception during create session."); Trace.Error(ex); - if (ex is VssOAuthTokenRequestException vssOAuthEx && _credsV2.Federated is VssOAuthCredential vssOAuthCred) + if (!HostContext.AllowAuthMigration && + ex is VssOAuthTokenRequestException vssOAuthEx && + _credsV2.Federated is VssOAuthCredential vssOAuthCred) { // "invalid_client" means the runner registration has been deleted from the server. if (string.Equals(vssOAuthEx.Error, "invalid_client", StringComparison.OrdinalIgnoreCase)) @@ -162,7 +164,8 @@ public async Task CreateSessionAsync(CancellationToken toke } } - if (!IsSessionCreationExceptionRetriable(ex)) + if (!HostContext.AllowAuthMigration && + !IsSessionCreationExceptionRetriable(ex)) { _term.WriteError($"Failed to create session. {ex.Message}"); if (ex is TaskAgentSessionConflictException) @@ -283,11 +286,11 @@ public async Task GetNextMessageAsync(CancellationToken token) Trace.Info("Hosted runner has been deprovisioned."); throw; } - catch (AccessDeniedException e) when (e.ErrorCode == 1) + catch (AccessDeniedException e) when (e.ErrorCode == 1 && !HostContext.AllowAuthMigration) { throw; } - catch (RunnerNotFoundException) + catch (RunnerNotFoundException) when (!HostContext.AllowAuthMigration) { throw; } @@ -296,7 +299,8 @@ public async Task GetNextMessageAsync(CancellationToken token) Trace.Error("Catch exception during get next message."); Trace.Error(ex); - if (!IsGetNextMessageExceptionRetriable(ex)) + if (!HostContext.AllowAuthMigration && + !IsGetNextMessageExceptionRetriable(ex)) { throw new NonRetryableException("Get next message failed with non-retryable error.", ex); } diff --git a/src/Runner.Listener/MessageListener.cs b/src/Runner.Listener/MessageListener.cs index 3c38ea884f5..d30ed2bf9da 100644 --- a/src/Runner.Listener/MessageListener.cs +++ b/src/Runner.Listener/MessageListener.cs @@ -315,11 +315,11 @@ public async Task GetNextMessageAsync(CancellationToken token) Trace.Info("Hosted runner has been deprovisioned."); throw; } - catch (AccessDeniedException e) when (e.ErrorCode == 1) + catch (AccessDeniedException e) when (e.ErrorCode == 1 && !HostContext.AllowAuthMigration) { throw; } - catch (RunnerNotFoundException) + catch (RunnerNotFoundException) when (!HostContext.AllowAuthMigration) { throw; } @@ -333,11 +333,14 @@ public async Task GetNextMessageAsync(CancellationToken token) message = null; // don't retry if SkipSessionRecover = true, DT service will delete agent session to stop agent from taking more jobs. - if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == CreateSessionResult.Success)) + if (!HostContext.AllowAuthMigration && + ex is TaskAgentSessionExpiredException && + !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == CreateSessionResult.Success)) { Trace.Info($"{nameof(TaskAgentSessionExpiredException)} received, recovered by recreate session."); } - else if (!IsGetNextMessageExceptionRetriable(ex)) + else if (!HostContext.AllowAuthMigration && + !IsGetNextMessageExceptionRetriable(ex)) { throw; }