Skip to content

Commit 4a7a287

Browse files
committed
fix: ssh connection to a workspace is established only once
- right now ssh automatically connects to the workspace when a user selects it from the main env. page. However, if the user manually disconnects from the hamburger menu, the SSH is no longer automatically established afterward. - the reason for this behavior was that the connection request flag that triggered the ssh connection behind the scenes was never reset. In order to reset we have to know when a user hits the disconnect button. - fortunately Toolbox provides two callbacks, one before establishing the ssh connection and another one right after disconnect. Even though we don't know the reason for disconnect it's still a good opportunity to reset the request flag. - resolves #38
1 parent 35c6259 commit 4a7a287

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- SSH connection to a Workspace is no longer established only once
8+
59
### Changed
610

711
- action buttons on the token input step were swapped to achieve better keyboard navigation

Diff for: src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt

+22-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
99
import com.coder.toolbox.util.withPath
1010
import com.coder.toolbox.views.Action
1111
import com.coder.toolbox.views.EnvironmentView
12+
import com.jetbrains.toolbox.api.remoteDev.AfterDisconnectHook
13+
import com.jetbrains.toolbox.api.remoteDev.BeforeConnectionHook
1214
import com.jetbrains.toolbox.api.remoteDev.DeleteEnvironmentConfirmationParams
1315
import com.jetbrains.toolbox.api.remoteDev.EnvironmentVisibilityState
1416
import com.jetbrains.toolbox.api.remoteDev.RemoteProviderEnvironment
@@ -35,7 +37,7 @@ class CoderRemoteEnvironment(
3537
private val client: CoderRestClient,
3638
private var workspace: Workspace,
3739
private var agent: WorkspaceAgent,
38-
) : RemoteProviderEnvironment("${workspace.name}.${agent.name}") {
40+
) : RemoteProviderEnvironment("${workspace.name}.${agent.name}"), BeforeConnectionHook, AfterDisconnectHook {
3941
private var wsRawStatus = WorkspaceAndAgentStatus.from(workspace, agent)
4042

4143
override var name: String = "${workspace.name}.${agent.name}"
@@ -109,6 +111,21 @@ class CoderRemoteEnvironment(
109111
return actions
110112
}
111113

114+
override fun getBeforeConnectionHooks(): List<BeforeConnectionHook> = listOf(this)
115+
116+
override fun getAfterDisconnectHooks(): List<AfterDisconnectHook> = listOf(this)
117+
118+
override fun beforeConnection() {
119+
context.logger.info("Connecting to $id...")
120+
this.isConnected = true
121+
}
122+
123+
override fun afterDisconnect() {
124+
this.connectionRequest.update { false }
125+
this.isConnected = false
126+
context.logger.info("Disconnected from $id")
127+
}
128+
112129
/**
113130
* Update the workspace/agent status to the listeners, if it has changed.
114131
*/
@@ -140,7 +157,8 @@ class CoderRemoteEnvironment(
140157
agent
141158
)
142159

143-
override val connectionRequest: MutableStateFlow<Boolean>? = MutableStateFlow(false)
160+
private var isConnected = false
161+
override val connectionRequest: MutableStateFlow<Boolean> = MutableStateFlow(false)
144162

145163
/**
146164
* Does nothing. In theory, we could do something like start the workspace
@@ -149,10 +167,9 @@ class CoderRemoteEnvironment(
149167
* to be much value.
150168
*/
151169
override fun setVisible(visibilityState: EnvironmentVisibilityState) {
152-
if (wsRawStatus.ready() && visibilityState.contentsVisible == true && visibilityState.isBackendConnected == false) {
153-
context.logger.info("Connecting to $id...")
170+
if (wsRawStatus.ready() && visibilityState.contentsVisible == true && isConnected == false) {
154171
context.cs.launch {
155-
connectionRequest?.update {
172+
connectionRequest.update {
156173
true
157174
}
158175
}

0 commit comments

Comments
 (0)