Skip to content

Feature Request: Configurable Retry and Backoff for External Auth Refresh Token Operations #18811

Closed

Description

@bjornrobertsson

Problem Statement

External auth token refresh failures in Coder result in immediate token deletion (https://github.com/coder/coder/blob/main/coderd/externalauth/externalauth.go#L145) without retry mechanisms, causing:

  • Workspace Authentication Failure: Users lose access until manual re-authentication
  • Workspace Startup Delays: Git operations hang indefinitely with expired tokens, when using external auth exclusively.
  • Poor User Experience: Frequent manual re-authentication required during provider maintenance
    Operational Burden: High support volume for "expired token" issues.

This aligns closely with the requirements for Bug, since the removal of the token was done to remediate the behaviour when hitting GitHub rate limits, but ultimately affects (non-GitHub, all) users negatively - when the OIDC Provider has a long outage or maintenance. This goes counter to the idea of the refresh-token having no expiration, and by removing the refresh-token can be seen as violating this idea of a 'remaining refresh-token'.

Where this diverges from regression and asking for the previous behaviour to be re-instated (to the detriment of GitHub users), the request is to add backoff, configuration settings and observability.

Current Behaviour - or lack of

No retry attempts for transient failures

  • Does not allow for temporary networking issue, transient access or OIDC/SAML Upstream provider having other problems.
    No UI recovery mechanism other than re-authenticate
    Limited visibility or none (i.e. startup script) into failure causes

Proposed Solution

  • Configurable Retry ParametersAdd environment variables for each external auth provider:
# Base configuration (existing)
CODER_EXTERNAL_AUTH_0_ID="aws-gitlab"
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET="xxx"

New retry configuration

CODER_EXTERNAL_AUTH_0_RETRY_ATTEMPTS=5        # Default: 3
CODER_EXTERNAL_AUTH_0_RETRY_INITIAL_DELAY=30s # Default: 30s
CODER_EXTERNAL_AUTH_0_RETRY_MAX_DELAY=300s    # Default: 5 minutes
CODER_EXTERNAL_AUTH_0_RETRY_BACKOFF_FACTOR=2  # Default: 2
CODER_EXTERNAL_AUTH_0_RETRY_JITTER=true       # Default: true

2. Exponential Backoff with Jitter
Implement truncated exponential backoff:

Start with initial delay (30s)
Apply configurable multiplier (2x)
Cap at maximum delay (5 minutes)
Add randomization to prevent thundering herd

3. Token Preservation Strategy
Critical: Preserve refresh tokens during retry window

Maintain workspace access during provider outages
Enable manual re-authentication during retries
Delete only after all attempts exhausted and sufficient time passes that it's unlikely to ever succeed (since refresh-tokens do not expire, this period should be extensive, counted in days)

4. Enhanced Observability

Structured logging for retry operations:

[info] external-auth: attempting token refresh provider=aws-gitlab [email protected] attempt=1/5
[warn] external-auth: token refresh failed provider=aws-gitlab [email protected] attempt=1/5 error="context canceled" next_retry_in=30s
[info] external-auth: token refresh succeeded provider=aws-gitlab [email protected] attempt=2/5
[error] external-auth: token refresh exhausted all attempts provider=aws-gitlab [email protected], clearing refresh token

5. Background Retry Process

Implement background service for:

Periodic failed token checks
Scheduled retry attempts
Database status updates
User failure notifications

Multi-Provider Support
Support multiple providers using existing suffix pattern:

# GitLab provider
CODER_EXTERNAL_AUTH_0_RETRY_ATTEMPTS=5
CODER_EXTERNAL_AUTH_0_RETRY_MAX_DELAY=300s

# GitHub provider
CODER_EXTERNAL_AUTH_1_RETRY_ATTEMPTS=3
CODER_EXTERNAL_AUTH_1_RETRY_MAX_DELAY=120s

Implementation Requirements

Backward Compatibility

Sensible defaults for all retry parameters
Must not cause service interruptions if the retry is not configured, or use 'similar' behaviour to avoid breaking changes.

Success Criteria

Reduced Support Burden: Eliminate frequent "expired token" requests
Improved Workspace Reliability: Successful Git operations during upstream provider outages
Better User Experience: Continuous access during brief maintenance windows
Operational Visibility: Clear retry attempt logging
Configurable Behavior: Provider-specific retry strategies

Related Issues
Issue #17069: GitHub external auth intermittently fails to refresh token
Issue #12787: No way to fetch external auth refresh token
Issue #14982: GitHub Rate limit

Activity

  1. added
    bug risk
    customer-reported
    customer-requested
    on Jul 9, 2025
  2. removed
    customer-reported
    customer-requested
    on Jul 24, 2025
  3. Emyrk commented on Aug 12, 2025

    @Emyrk
    Member

    Spoke with @bjornrobertsson.

    We think what is happening is the error message presented is a consequence of a previous error.

    The code reads, "if a token fails to refresh, clear it's refresh token".

    token, err := c.TokenSource(ctx, existingToken).Token()
    if err != nil {
    // TokenSource can fail for numerous reasons. If it fails because of
    // a bad refresh token, then the refresh token is invalid, and we should
    // get rid of it. Keeping it around will cause additional refresh
    // attempts that will fail and cost us api rate limits.
    if isFailedRefresh(existingToken, err) {
    dbExecErr := db.UpdateExternalAuthLinkRefreshToken(ctx, database.UpdateExternalAuthLinkRefreshTokenParams{
    OAuthRefreshToken: "", // It is better to clear the refresh token than to keep retrying.
    OAuthRefreshTokenKeyID: externalAuthLink.OAuthRefreshTokenKeyID.String,
    UpdatedAt: dbtime.Now(),
    ProviderID: externalAuthLink.ProviderID,
    UserID: externalAuthLink.UserID,
    })
    if dbExecErr != nil {
    // This error should be rare.
    return externalAuthLink, InvalidTokenError(fmt.Sprintf("refresh token failed: %q, then removing refresh token failed: %q", err.Error(), dbExecErr.Error()))
    }
    // The refresh token was cleared
    externalAuthLink.OAuthRefreshToken = ""
    }
    // Unfortunately have to match exactly on the error message string.
    // Improve the error message to account refresh tokens are deleted if
    // invalid on our end.
    if err.Error() == "oauth2: token expired and refresh token is not set" {
    return externalAuthLink, InvalidTokenError("token expired, refreshing is either disabled or refreshing failed and will not be retried")
    }
    // TokenSource(...).Token() will always return the current token if the token is not expired.
    // So this error is only returned if a refresh of the token failed.
    return externalAuthLink, InvalidTokenError(fmt.Sprintf("refresh token: %s", err.Error()))
    }

    An internal error or rate limit response do not constitute a refresh failure:

    case http.StatusInternalServerError, http.StatusTooManyRequests:

    However, if you hit a refresh error, the token is cleared. On the next RefreshToken attempt, the token is not invalid and now no refresh token is set.

    So the oauth error is this:

    if err.Error() == "oauth2: token expired and refresh token is not set" {

    So every error will now report as this. The original error is lost, and only the subsequent is set.


    In summary (assuming the code is right, I will add a test), the problem is the original refresh error is lost. So debugging is challenging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug risk

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions