Skip to content

Commit c7321a6

Browse files
fix(jetbrains): avoid connection loops
1 parent db6f5ed commit c7321a6

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodConnectionProvider.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.ide.BrowserUtil
1212
import com.intellij.openapi.components.service
1313
import com.intellij.openapi.diagnostic.thisLogger
1414
import com.intellij.openapi.progress.ProgressManager
15+
import com.intellij.openapi.ui.Messages
1516
import com.intellij.remote.RemoteCredentialsHolder
1617
import com.intellij.ssh.AskAboutHostKey
1718
import com.intellij.ssh.OpenSshLikeHostKeyVerifier
@@ -30,6 +31,7 @@ import com.jetbrains.gateway.api.*
3031
import com.jetbrains.gateway.ssh.ClientOverSshTunnelConnector
3132
import com.jetbrains.gateway.ssh.SshHostTunnelConnector
3233
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
34+
import com.jetbrains.rd.util.ConcurrentHashMap
3335
import com.jetbrains.rd.util.URI
3436
import com.jetbrains.rd.util.lifetime.Lifetime
3537
import io.gitpod.gitpodprotocol.api.entities.WorkspaceInstance
@@ -49,7 +51,7 @@ import kotlin.coroutines.coroutineContext
4951

5052
@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
5153
class GitpodConnectionProvider : GatewayConnectionProvider {
52-
54+
private val activeConnections = ConcurrentHashMap<String, Boolean>()
5355
private val gitpod = service<GitpodConnectionService>()
5456

5557
private val httpClient = HttpClient.newBuilder()
@@ -59,6 +61,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
5961
private val jacksonMapper = jacksonObjectMapper()
6062
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
6163

64+
6265
override suspend fun connect(
6366
parameters: Map<String, String>,
6467
requestor: ConnectionRequestor
@@ -74,6 +77,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
7477
parameters["workspaceId"]!!,
7578
parameters["backendPort"]
7679
)
80+
7781
val client = gitpod.obtainClient(connectParams.gitpodHost)
7882
val connectionLifetime = Lifetime.Eternal.createNested()
7983
val updates = client.listenToWorkspace(connectionLifetime, connectParams.workspaceId)
@@ -144,6 +148,24 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
144148
connectionPanel.repaint()
145149
}
146150

151+
val connectionKeyId = "${connectParams.gitpodHost}-${connectParams.workspaceId}-${connectParams.backendPort}"
152+
153+
if (activeConnections.containsKey(connectionKeyId)) {
154+
val message = "You are trying to connect to a workspace that has a session already open"
155+
val title = "Oops"
156+
val okButton = Messages.getOkButton()
157+
val options = arrayOf(okButton)
158+
val defaultIndex = 0
159+
val icon = Messages.getWarningIcon()
160+
Messages.showDialog(message, title, options, defaultIndex, icon)
161+
162+
val errMessage = "A connection to the same workspace already exists: $connectionKeyId"
163+
thisLogger().warn(errMessage)
164+
throw IllegalStateException(errMessage)
165+
} else {
166+
activeConnections.putIfAbsent(connectionKeyId, true)
167+
}
168+
147169
GlobalScope.launch {
148170
var thinClient: ThinClientHandle? = null
149171
var thinClientJob: Job? = null
@@ -219,6 +241,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
219241
val sshHostUrl = URL(update.ideUrl.replace(update.workspaceId, "${update.workspaceId}.ssh"));
220242
val hostKeys = resolveHostKeys(updatedIdeUrl, connectParams)
221243
if (hostKeys.isNullOrEmpty()) {
244+
activeConnections.remove(connectionKeyId)
222245
setErrorMessage("${connectParams.gitpodHost} installation does not allow SSH access, public keys cannot be found")
223246
return@launch
224247
}
@@ -227,6 +250,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
227250
resolveCredentials(sshHostUrl, update.workspaceId, ownerToken, hostKeys)
228251
val joinLink = resolveJoinLink(updatedIdeUrl, ownerToken, connectParams)
229252
if (joinLink.isNullOrEmpty()) {
253+
activeConnections.remove(connectionKeyId)
230254
setErrorMessage("failed to fetch JetBrains Gateway Join Link.")
231255
return@launch
232256
}
@@ -239,6 +263,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
239263
clientHandle.clientClosed.advise(connectionLifetime) {
240264
application.invokeLater {
241265
connectionLifetime.terminate()
266+
activeConnections.remove(connectionKeyId)
242267
}
243268
}
244269
clientHandle.onClientPresenceChanged.advise(connectionLifetime) {

0 commit comments

Comments
 (0)