Skip to content

impl: enhanced workflow for network disruptions #162

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 5 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- improved workflow when network connection is flaky

## 0.5.2 - 2025-07-22

### Fixed
Expand Down
19 changes: 14 additions & 5 deletions src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class CoderRemoteEnvironment(
private val proxyCommandHandle = SshCommandProcessHandle(context)
private var pollJob: Job? = null

init {
if (context.settingsStore.shouldAutoConnect(id)) {
context.logger.info("resuming SSH connection to $id — last session was still active.")
startSshConnection()
}
}

fun asPairOfWorkspaceAndAgent(): Pair<Workspace, WorkspaceAgent> = Pair(workspace, agent)

private fun getAvailableActions(): List<ActionDescription> {
Expand Down Expand Up @@ -158,6 +165,7 @@ class CoderRemoteEnvironment(
override fun beforeConnection() {
context.logger.info("Connecting to $id...")
isConnected.update { true }
context.settingsStore.updateAutoConnect(this.id, true)
pollJob = pollNetworkMetrics()
}

Expand All @@ -180,12 +188,9 @@ class CoderRemoteEnvironment(
}
context.logger.debug("Loading metrics from ${metricsFile.absolutePath} for $id")
try {
val metrics = networkMetricsMarshaller.fromJson(metricsFile.readText())
if (metrics == null) {
return@launch
}
val metrics = networkMetricsMarshaller.fromJson(metricsFile.readText()) ?: return@launch
context.logger.debug("$id metrics: $metrics")
additionalEnvironmentInformation.put(context.i18n.ptrl("Network Status"), metrics.toPretty())
additionalEnvironmentInformation[context.i18n.ptrl("Network Status")] = metrics.toPretty()
} catch (e: Exception) {
context.logger.error(
e,
Expand All @@ -203,6 +208,10 @@ class CoderRemoteEnvironment(
pollJob?.cancel()
this.connectionRequest.update { false }
isConnected.update { false }
if (isManual) {
// if the user manually disconnects the ssh connection we should not connect automatically
context.settingsStore.updateAutoConnect(this.id, false)
}
Comment on lines +211 to +214
Copy link
Member

Choose a reason for hiding this comment

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

Why this choice? If the user manually disconnects and then connects again, we should still try to resume the last active connection if possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you take a look at beforeConnection we set auto-connect to true. In other words:

  • user connects the ssh, and then TBX is closed -> at the next restart we automatically reconnect.
  • user connects to the ssh, manually disconnects and then TBX is closed -> at the next restart we no longer reconnect automatically because the user made a conscious choice to disconnect.
  • user connects to the ssh, manually disconnects, then reconnects again the ssh session, and then TBX is closed -> at the next restart we automatically reconnect.

context.logger.info("Disconnected from $id")
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ class CoderRemoteProvider(
context.logger.error(ex, "workspace polling error encountered, trying to auto-login")
if (ex is APIResponseException && ex.isTokenExpired) {
WorkspaceConnectionManager.shouldEstablishWorkspaceConnections = true
close()
// force auto-login
firstRun = true
context.envPageManager.showPluginEnvironmentsPage()
break
}
close()
// force auto-login
firstRun = true
context.envPageManager.showPluginEnvironmentsPage()
break
}
}

// TODO: Listening on a web socket might be better?
select<Unit> {
select {
onTimeout(POLL_INTERVAL) {
context.logger.trace("workspace poller waked up by the $POLL_INTERVAL timeout")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ interface ReadOnlyCoderSettings {
* Return the URL and token from the config, if they exist.
*/
fun readConfig(dir: Path): Pair<String?, String?>

/**
* Returns whether the SSH connection should be automatically established.
*/
fun shouldAutoConnect(workspaceId: String): Boolean
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/coder/toolbox/store/CoderSettingsStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class CoderSettingsStore(
}
}

override fun shouldAutoConnect(workspaceId: String): Boolean {
return store["$SSH_AUTO_CONNECT_PREFIX$workspaceId"]?.toBooleanStrictOrNull() ?: false
}

// a readonly cast
fun readOnly(): ReadOnlyCoderSettings = this

Expand Down Expand Up @@ -213,6 +217,10 @@ class CoderSettingsStore(
store[SSH_CONFIG_OPTIONS] = options
}

fun updateAutoConnect(workspaceId: String, autoConnect: Boolean) {
store["$SSH_AUTO_CONNECT_PREFIX$workspaceId"] = autoConnect.toString()
}

private fun getDefaultGlobalDataDir(): Path {
return when (getOS()) {
OS.WINDOWS -> Paths.get(env.get("LOCALAPPDATA"), "coder-toolbox")
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/coder/toolbox/store/StoreKeys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ internal const val SSH_CONFIG_OPTIONS = "sshConfigOptions"

internal const val NETWORK_INFO_DIR = "networkInfoDir"

internal const val SSH_AUTO_CONNECT_PREFIX = "ssh_auto_connect_"

Loading