Skip to content

Commit 40c209d

Browse files
committed
fix: avoid bind exception when reconnecting after failure
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 98be0a6 commit 40c209d

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/main/kotlin/com/redhat/devtools/gateway/DevSpacesConnection.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
3232
if (devSpacesContext.isConnected)
3333
throw IOException(String.format("Already connected to %s", devSpacesContext.devWorkspace.metadata.name))
3434

35-
devSpacesContext.isConnected = true
3635
try {
36+
devSpacesContext.isConnected = true
3737
return doConnection(onConnected, onDevWorkspaceStopped, onDisconnected)
3838
} catch (e: Exception) {
3939
devSpacesContext.isConnected = false
@@ -51,33 +51,35 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
5151
startAndWaitDevWorkspace()
5252

5353
val remoteIdeServer = RemoteIDEServer(devSpacesContext)
54-
val remoteIdeServerStatus = remoteIdeServer.getStatus()
54+
val joinLink = remoteIdeServer.getStatus().joinLink
55+
?: throw IOException("Could not connect: no join link present")
5556

5657
val client = LinkedClientManager
5758
.getInstance()
5859
.startNewClient(
5960
Lifetime.Eternal,
60-
URI(remoteIdeServerStatus.joinLink),
61+
URI(joinLink),
6162
"",
6263
onConnected,
6364
false
6465
)
6566

66-
val forwarder = Pods(devSpacesContext.client).forward(remoteIdeServer.pod, 5990, 5990)
67+
val forwarder = Pods(devSpacesContext.client)
68+
.forward(remoteIdeServer.pod, 5990, 5990)
6769

6870
client.run {
69-
lifetime.onTermination { forwarder.close() }
7071
lifetime.onTermination {
72+
forwarder.close()
7173
if (remoteIdeServer.waitServerTerminated())
7274
DevWorkspaces(devSpacesContext.client)
7375
.stop(
7476
devSpacesContext.devWorkspace.metadata.namespace,
7577
devSpacesContext.devWorkspace.metadata.name
7678
)
7779
.also { onDevWorkspaceStopped() }
80+
devSpacesContext.isConnected = false
81+
onDisconnected.invoke()
7882
}
79-
lifetime.onTermination { devSpacesContext.isConnected = false }
80-
lifetime.onTermination(onDisconnected)
8183
}
8284

8385
return client

src/main/kotlin/com/redhat/devtools/gateway/DevSpacesConnectionProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
164164
parameters: Map<String, String>,
165165
requestor: ConnectionRequestor,
166166
indicator: ProgressIndicator? = null
167-
): GatewayConnectionHandle? {
167+
): GatewayConnectionHandle {
168168
thisLogger().debug("Launched Dev Spaces connection provider", parameters)
169169

170170
indicator?.text2 = "Preparing connection environment…"

src/main/kotlin/com/redhat/devtools/gateway/openshift/Pods.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import java.io.IOException
3838
import java.io.InputStream
3939
import java.io.OutputStream
4040
import java.net.InetAddress
41+
import java.net.InetSocketAddress
4142
import java.net.ServerSocket
4243
import java.net.Socket
4344
import java.util.concurrent.TimeUnit
@@ -99,16 +100,15 @@ class Pods(private val client: ApiClient) {
99100
// https://github.com/kubernetes-client/java/blob/master/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/PortForwardExample.java
100101
@Throws(IOException::class)
101102
fun forward(pod: V1Pod, localPort: Int, remotePort: Int): Closeable {
102-
val serverSocket = ServerSocket(localPort, 50, InetAddress.getLoopbackAddress())
103-
103+
val serverSocket = ServerSocket()
104+
serverSocket.bind(InetSocketAddress(InetAddress.getLoopbackAddress(), localPort))
104105
val scope = CoroutineScope(Dispatchers.IO)
105106
scope.launch {
106107
supervisorScope {
107108
launch {
108109
val clientSocket = serverSocket.accept()
109-
val forwardResult = PortForward(client).forward(pod, listOf(remotePort))
110-
111110
try {
111+
val forwardResult = PortForward(client).forward(pod, listOf(remotePort))
112112
copyStreams(clientSocket, forwardResult, remotePort)
113113
} catch (e: IOException) {
114114
if (coroutineContext.isActive) throw e

src/main/kotlin/com/redhat/devtools/gateway/server/RemoteIDEServerStatus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package com.redhat.devtools.gateway.server
1414

1515
data class RemoteIDEServerStatus(
16-
val joinLink: String,
16+
val joinLink: String?,
1717
val httpLink: String,
1818
val gatewayLink: String,
1919
val appVersion: String,

0 commit comments

Comments
 (0)