Skip to content

Commit 1fb74d9

Browse files
felladrinroboquat
authored andcommitted
Fix to observe ports while the service is active
1 parent 6fba354 commit 1fb74d9

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortForwardingServiceImpl.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.diagnostic.thisLogger
1010
import com.intellij.remoteDev.util.onTerminationOrNow
1111
import com.intellij.ui.RowIcon
1212
import com.intellij.util.application
13+
import com.jetbrains.rd.framework.util.launch
1314
import com.jetbrains.rd.platform.codeWithMe.portForwarding.*
1415
import com.jetbrains.rd.util.URI
1516
import com.jetbrains.rd.util.lifetime.Lifetime
@@ -22,10 +23,13 @@ import io.gitpod.supervisor.api.StatusServiceGrpc
2223
import io.grpc.stub.ClientCallStreamObserver
2324
import io.grpc.stub.ClientResponseObserver
2425
import io.ktor.utils.io.*
26+
import kotlinx.coroutines.cancel
27+
import kotlinx.coroutines.delay
28+
import kotlinx.coroutines.future.asDeferred
29+
import kotlinx.coroutines.isActive
2530
import org.apache.http.client.utils.URIBuilder
2631
import java.util.*
2732
import java.util.concurrent.CompletableFuture
28-
import java.util.concurrent.TimeUnit
2933

3034
@Suppress("UnstableApiUsage")
3135
class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
@@ -46,22 +50,25 @@ class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
4650
observePortsListWhileProjectIsOpen()
4751
}
4852

49-
private fun observePortsListWhileProjectIsOpen() = application.executeOnPooledThread {
50-
lifetime.executeIfAlive {
53+
private fun observePortsListWhileProjectIsOpen() = lifetime.launch {
54+
while (isActive) {
5155
try {
52-
observePortsList().get()
56+
observePortsList().asDeferred().await()
5357
} catch (throwable: Throwable) {
5458
when (throwable) {
55-
is InterruptedException, is CancellationException -> lifetime.terminate()
56-
else -> thisLogger().error(
57-
"gitpod: Got an error while trying to get ports list from Supervisor. " +
58-
"Going to try again in a second.",
59-
throwable
60-
)
59+
is InterruptedException, is CancellationException -> {
60+
cancel("gitpod: Stopped observing ports list due to an expected interruption.")
61+
}
62+
else -> {
63+
thisLogger().warn(
64+
"gitpod: Got an error while trying to get ports list from Supervisor. " +
65+
"Going to try again in a second.",
66+
throwable
67+
)
68+
delay(1000)
69+
}
6170
}
6271
}
63-
64-
TimeUnit.SECONDS.sleep(1)
6572
}
6673
}
6774

@@ -75,7 +82,7 @@ class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
7582
val portsStatusResponseObserver = object :
7683
ClientResponseObserver<Status.PortsStatusRequest, Status.PortsStatusResponse> {
7784
override fun beforeStart(request: ClientCallStreamObserver<Status.PortsStatusRequest>) {
78-
lifetime.onTerminationOrNow { request.cancel("gitpod: Project terminated.", null) }
85+
lifetime.onTerminationOrNow { request.cancel("gitpod: Service lifetime terminated.", null) }
7986
}
8087

8188
override fun onNext(response: Status.PortsStatusResponse) {

0 commit comments

Comments
 (0)