Skip to content

coder ssh: add retry loop for transient connection failures #24005

Closed

Description

@EhabY

coder ssh --stdio exits immediately on transient errors (DNS failure, connection timeout, API unreachable). When used as an SSH ProxyCommand, this causes VS Code's Remote SSH extension to throw a permanent SshResolverError.Create() error, which kills the reconnection loop on the first attempt. The user sees "Reload Window" even though the error would resolve itself in seconds.

Confirmed failure mode

Captured from remote.SSH.logLevel: "trace" during a real sleep/wake cycle on Linux with systemd-resolved:

  1. Wake from sleep, VS Code detects disconnect (~20s), starts reconnection
  2. Remote SSH extension spawns new SSH with ProxyCommand: coder ssh --stdio
  3. coder ssh immediately queries the workspace API
  4. DNS lookup fails: dial tcp: lookup dev.coder.com: server misbehaving (systemd-resolved not ready after wake)
  5. coder ssh exits immediately with error
  6. Remote SSH sees unparsable bash script output
  7. SshResolverError.Create() (permanent) -> "Reload Window"

DNS recovers within seconds, but coder ssh has already exited. VS Code does not retry because the error is classified as permanent.

This affects any configuration where coder ssh errors produce terminal output that the Remote SSH extension tries to parse as bash script output (i.e. when useLocalServer is false). With useLocalServer: true, the local server manages SSH with its own retry logic and doesn't depend on script parsing, making it more resilient to transient ProxyCommand failures.

A separate but related issue exists with the Coder Connect code path, where a broken tunnel causes a ~55 minute hang before coder ssh exits. The retry loop proposed here would eventually recover from that too, but a dial timeout and tailnet fallback are needed to avoid the long wait. See ENG-2346.

Proposed change

Wrap the connection establishment logic in cli/ssh.go (handler function, lines ~280-474) in a retry loop with backoff. This covers workspace resolution, API calls, agent readiness checks, and the dial to the workspace.

retryDelays := []time.Duration{0, 2 * time.Second, 5 * time.Second, 10 * time.Second, 10 * time.Second}

for attempt := range retryDelays {
    err := connectAndServe(ctx, ...)
    if err == nil {
        return nil
    }
    if !isRetryable(err) || attempt == len(retryDelays)-1 {
        return err
    }
    logger.Warn(ctx, "connection failed, retrying",
        slog.Error(err), slog.F("attempt", attempt+1))
    select {
    case <-ctx.Done():
        return ctx.Err()
    case <-time.After(retryDelays[attempt]):
    }
}

Error classification:

Error Retryable? Rationale
DNS lookup failure Yes Resolvers recover within seconds after wake
Connection refused / reset Yes Network interface may still be coming up
Connection timeout Yes Transient network issue
DialAgent failure Yes Tailnet coordinator may be temporarily unreachable
HTTP 401/403 (auth) No Credentials are wrong, retrying won't help
HTTP 404 (workspace not found) No Workspace doesn't exist
Context canceled (SIGINT/SIGHUP) No User or SSH parent requested exit

Current workaround

A vscode-coder extension PR (coder/vscode-coder#872) wraps the ProxyCommand with a shell-level retry as a stopgap. This works but is fragile (platform-specific shell behavior, can't classify errors) and only helps VS Code/Cursor users.

Why the CLI is the right layer

  • Covers all editors (VS Code, Cursor, JetBrains, plain SSH)
  • Can accurately distinguish transient vs permanent errors
  • Avoids platform-specific shell wrapper issues
  • Logs retries to the coder ssh log file for debugging
  • Removes the need for an extension-side ProxyCommand retry wrapper

Activity

  1. self-assigned this
    on Apr 3, 2026
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