Skip to content

Exponential backoff caps at ~25 days on headers-present retry path #20126

Closed as not planned

Description

@OliPetry

Bug

When the upstream API returns an error with HTTP response headers but without a retry-after header, the SessionRetry logic in src/session/retry.ts uses an exponential backoff capped at 2,147,483,647 ms (~24.86 days / ~3 weeks). This makes the session appear permanently stuck after ~22 failed attempts.

In contrast, the no-headers code path correctly caps at 30,000 ms (30 seconds).

How to reproduce

  1. Have the upstream proxy return repeated 502 Bad Gateway responses with standard HTTP headers but no retry-after header
  2. Let the retry loop run past attempt ~22
  3. The UI will display "retrying in ~3 weeks" and the session becomes effectively frozen

In my case, this was a corporate proxy returning 502s. By attempt #45, the backoff had long since hit the ceiling.

Observed behavior

The error message reads:

Bad Gateway: Failed to proxy request: Post "https://..." 
(click to expand) [retrying in ~3 weeks attempt #45]

Root cause

In the embedded src/session/retry.ts:

RETRY_INITIAL_DELAY = 2000            // 2 seconds
RETRY_BACKOFF_FACTOR = 2              // exponential base
RETRY_MAX_DELAY_NO_HEADERS = 30000    // 30 seconds (sane)
RETRY_MAX_DELAY = 2147483647          // 2^31 - 1 ms ≈ 24.86 days (not sane)

The cap() function uses RETRY_MAX_DELAY (the ~25 day value) when response headers are present, even if there's no retry-after header. The exponential formula 2000 * 2^(attempt-1) exceeds this cap at attempt 22, so from attempt 22 onward every retry waits ~25 days.

There is also no maximum attempt count -- the retry loop continues indefinitely.

Suggested fix

  • Cap RETRY_MAX_DELAY at something reasonable (e.g., 60 seconds or 120 seconds)
  • Consider adding a maximum attempt count, or at minimum, resetting the backoff after some threshold
  • When no retry-after header is present, use the same 30-second cap regardless of whether other headers exist

Workaround

Press Escape twice to interrupt the retry fiber, then re-send the message. This resets the attempt counter for that turn without losing session context.

Environment

  • OpenCode v1.3.6 (arm64, macOS)
  • Upstream proxy returning 502 Bad Gateway

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 30, 2026
  2. github-actions commented on Mar 30, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of an existing issue. Please check:

  3. removed
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  4. github-actions commented on Jul 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions