Skip to content

fix(cli): retry transient connection failures during SSH setup - #24010

Merged
EhabY merged 1 commit into
mainfrom
fix/ssh-stdio-retry
Apr 7, 2026
Merged

EhabY merged 1 commit into
mainfrom
fix/ssh-stdio-retry

Conversation

@EhabY

@EhabY EhabY commented Apr 3, 2026 •

Copy link
Copy Markdown
Collaborator

When coder ssh connects to a workspace after laptop wake, DNS or the control plane may be briefly unavailable. Previously this caused an immediate failure, which VS Code Remote SSH classified as permanent ("Reload Window").

Wrap each network step (workspace resolution, template version fetch, agent connection info, Coder Connect dial, tailnet dial) with retryWithInterval so transient errors (DNS, connection refused, 5xx) are retried individually. Non-retryable errors (auth, 404) and context cancellation stop immediately. Data transfer is never retried.

Fixes #24005

@github-actions github-actions Bot added the community Pull Requests and issues created by the community. label Apr 3, 2026
@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch from 23b6fee to 052d779 Compare April 3, 2026 12:10
@EhabY EhabY changed the title fix(cli/ssh): add retry loop for transient connection failures in stdio mode fix(cli): add retry loop for transient connection failures in stdio mode Apr 3, 2026
@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch from 052d779 to 6667c95 Compare April 3, 2026 12:21
@EhabY
EhabY marked this pull request as ready for review April 3, 2026 12:22
@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch 3 times, most recently from e3d3703 to b689ea8 Compare April 3, 2026 13:39
Comment thread cli/ssh.go Resolved
@EhabY EhabY changed the title fix(cli): add retry loop for transient connection failures in stdio mode fix(cli/ssh): add stdio retry with proper tunnel/SSH layering Apr 6, 2026
@EhabY EhabY changed the title fix(cli/ssh): add stdio retry with proper tunnel/SSH layering fix(cli): add stdio retry with proper tunnel/SSH layering Apr 6, 2026
@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch 2 times, most recently from 3aaba90 to d7e0867 Compare April 6, 2026 13:44
@spikecurtis

Copy link
Copy Markdown
Collaborator

This still feels half-baked.

  1. Just wrapping the whole thing in a retry is a hack. If step 3 fails in some way that is retryable, why do we need to go back to step 1?
  2. Why are we only retrying on --stdio and not other SSH use cases? Having different logic in those two paths adds a lot of maintenance burden.

@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch from d7e0867 to ebe4912 Compare April 6, 2026 13:55
@EhabY

EhabY commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator Author

The issue is this code here is very monolithic and sequential which makes the maintenance here harder than it should be.

Just wrapping the whole thing in a retry is a hack. If step 3 fails in some way that is retryable, why do we need to go back to step 1?

True, if the dial fails there's no reason to re-resolve the workspace. The resolve result could be cached and only the dial retried. Right now the establishment function bundles resolve + dial together, so the retry re-does both. I can split it so the resolve happens once and only the dial is retried. Is this worth doing?

Why are we only retrying on --stdio and not other SSH use cases? Having different logic in those two paths adds a lot of maintenance burden.

In interactive mode the user sees the error and can retry manually. In stdio mode, coder ssh --stdio runs as a ProxyCommand (usually spawned by VS Code), so the user doesn't see the error directly and just gets "connection failed." Transparent retry makes the ProxyCommand more resilient on flaky networks. Happy to add retry for interactive mode too if you think it's useful.

@spikecurtis

Copy link
Copy Markdown
Collaborator

I agree that the code is not well structured and difficult to maintain. Let's not make the problem worse.

I'm in the process of rearchitecting this code right now, to use a state machine and a new watch on the workspace to replace the workspace and agent resolution code. https://www.notion.so/coderhq/coder-ssh-watch-and-FSM-329d579be59280e3979bfae59051a54e?d=309d579be59283609af183ec14f89244

Still, there will be some initial API requests that get server settings and the like. If we need to include retries, I would much rather wrap individual steps in retries, with an overall context timeout so we don't retry forever. That way you can decide on a step-by-step basis what is transient/retryable, and you don't have to restart the whole process if you hit an error part way through.

@EhabY

EhabY commented Apr 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

Oh fair enough, I wasn't aware of this. Should've discussed this with someone beforehand. Should I close this for now and we can handle it after the restructure? Or maybe add a very minimal solution like retrying ONLY Coder Connect TCP dial (dialer.DialContext) AND Tailnet dial (separately for stdio only)

How about I add per-step retry, can be cancelled by the user (Ctrl + C or killing the parent process etc), and it retries up to X times for each step? I can add a total timeout but I'm not sure about this value because what if this process used to take 60s? 100s? Maybe even more (it had no timeout before).

A follow up here is, do we want to retry only in stdio mode or both?

@spikecurtis

Copy link
Copy Markdown
Collaborator

How about I add per-step retry, can be cancelled by the user (Ctrl + C or killing the parent process etc), and it retries up to X times for each step? I can add a total timeout but I'm not sure about this value because what if this process used to take 60s? 100s? Maybe even more (it had no timeout before).

Per step retry sounds fine. Total timeout might get tricky since we wait for the build, so a per-step retry count is probably fine.

A follow up here is, do we want to retry only in stdio mode or both?

I'd prefer to keep things going down the same code paths as we bring up the tunnel. stdio is an application-layer concern, so really don't want different behavior down at the tailnet and TCP layer because of it.

@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch from ebe4912 to 8c14f28 Compare April 7, 2026 08:19
Comment thread cli/ssh.go Resolved
Wrap each network step in the SSH connection flow with
retryWithBackoff so transient errors (DNS, connection refused,
server 5xx) are retried individually with exponential backoff.
Both stdio and interactive modes retry identically since tunnel
establishment is not an application-layer concern.

Retried steps: workspace resolution, template version fetch,
agent connection info, Coder Connect dial, and tailnet dial.
Data transfer (copier.copy/Bicopy) is never retried.
@EhabY
EhabY force-pushed the fix/ssh-stdio-retry branch from 8c14f28 to 8e19be8 Compare April 7, 2026 08:40
@EhabY EhabY changed the title fix(cli): add stdio retry with proper tunnel/SSH layering fix(cli/ssh): retry transient connection failures during SSH setup Apr 7, 2026
@EhabY EhabY changed the title fix(cli/ssh): retry transient connection failures during SSH setup fix(cli): retry transient connection failures during SSH setup Apr 7, 2026
@EhabY EhabY removed the community Pull Requests and issues created by the community. label Apr 7, 2026
@EhabY
EhabY requested a review from spikecurtis April 7, 2026 09:08
@EhabY
EhabY merged commit d00f148 into main Apr 7, 2026
40 of 42 checks passed
@EhabY
EhabY deleted the fix/ssh-stdio-retry branch April 7, 2026 21:59
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

coder ssh: add retry loop for transient connection failures

2 participants