Skip to content

feat: auto reconnect the terminal #18796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 9, 2025
Merged

Conversation

BrunoQuaresma
Copy link
Collaborator

@BrunoQuaresma BrunoQuaresma commented Jul 8, 2025

Changes:

  • Use websocket-ts to have auto reconnection out of the box 🙏
  • Update the disconnected alert message to "Trying to connect..." since the connection is always trying to reconnect
  • Remove useWithRetry because it is not necessary anymore

Other topics:

  • The disconnected alert is displaying a simple message, but we can include more info such as the number of attemtps
  • The reconnection feature is in a good state and adding value. IMO, any improvement can be done after getting this merged

Closes coder/internal#659

@BrunoQuaresma BrunoQuaresma requested a review from code-asher July 8, 2025 14:38
@BrunoQuaresma BrunoQuaresma self-assigned this Jul 8, 2025
@BrunoQuaresma
Copy link
Collaborator Author

@code-asher I'm wondering if we can stop adding error messages to the terminal since we already have the alert. Wdyt?

Screenshot 2025-07-08 at 11 58 37

@code-asher
Copy link
Member

I'm wondering if we can stop adding error messages to the terminal since we already have the alert. Wdyt?

Makes perfect sense to me!

Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising! I tried it out and it seems to reconnect but then I was unable to see anything show up in the terminal. Is that the same for you? I can see messages being sent and received on the web socket, so it seems like we are just not printing to the terminal anymore. Maybe we have to rebind something? Or maybe the terminal is being recreated or something.

@@ -259,9 +266,11 @@ const TerminalPage: FC = () => {
if (disposed) {
return; // Unmounted while we waited for the async call.
}
websocket = new WebSocket(url);
websocket = new WebsocketBuilder(url)
.withBackoff(new ConstantBackoff(1000))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should switch this to exponential (ideally with some jitter) soon, if not in this PR. A fixed reconnect interval is a good way to DDoS yourself 😆

@code-asher
Copy link
Member

Ah actually it seems the data is a blob after reconnect instead of a array buffer? Does websocket-ts not preserve the binaryType? Maybe we have to set it again on reconnect.

Also we should probably send the size on reconnect in case the user resized the terminal while offline.

@BrunoQuaresma
Copy link
Collaborator Author

@code-asher fixed the issues you pointed out. I tested using the following steps:

  • Open the terminal
  • Run top
  • Put network on offline using the dev console (in network tab)
  • Enable network again

Do you know if this is a good recipe for testing these changes? Let me know if you think I should test this differently.

Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works! I wish their backoff implementation had jitter, but what can ya do. We can add our own backoff implementation later if necessary.

Put network on offline using the dev console (in network tab)

My experience has been that this setting does not affect existing connections (like web sockets). I tried just now to be sure (in Chromium) but the "trying to connect" message never appears and I can still use the terminal even when "offline".

What I have been doing is killing coderd, waiting for the reconnect message, then starting coderd again.

@BrunoQuaresma BrunoQuaresma merged commit 5a8a19b into main Jul 9, 2025
31 checks passed
@BrunoQuaresma BrunoQuaresma deleted the bq/auto-reconnect-terminal branch July 9, 2025 18:04
@github-actions github-actions bot locked and limited conversation to collaborators Jul 9, 2025
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.

tasks: terminal should try to reconnect
2 participants