Skip to content

Commit 0f02d8f

Browse files
Pururunalbin-mullvad
authored andcommitted
Fix potential crash in connection state flow
1 parent 3562753 commit 0f02d8f

File tree

1 file changed

+11
-7
lines changed
  • android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util

1 file changed

+11
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package net.mullvad.mullvadvpn.lib.daemon.grpc.util
22

3+
import co.touchlab.kermit.Logger
34
import io.grpc.ConnectivityState
45
import io.grpc.ManagedChannel
5-
import kotlin.coroutines.resume
6-
import kotlin.coroutines.suspendCoroutine
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
77
import kotlinx.coroutines.flow.Flow
88
import kotlinx.coroutines.flow.callbackFlow
99
import kotlinx.coroutines.isActive
10+
import kotlinx.coroutines.suspendCancellableCoroutine
1011

12+
@OptIn(ExperimentalCoroutinesApi::class)
1113
internal fun ManagedChannel.connectivityFlow(): Flow<ConnectivityState> {
1214
return callbackFlow {
1315
var currentState = getState(false)
14-
send(currentState)
1516

1617
while (isActive) {
17-
currentState =
18-
suspendCoroutine<ConnectivityState> {
19-
notifyWhenStateChanged(currentState) { it.resume(getState(false)) }
20-
}
18+
// Check that we are active before sending
2119
send(currentState)
20+
currentState = suspendCancellableCoroutine {
21+
notifyWhenStateChanged(currentState) {
22+
// If we are cancelled we will just log
23+
it.resume(getState(false)) { Logger.w("Resume while cancelled", it) }
24+
}
25+
}
2226
}
2327
}
2428
}

0 commit comments

Comments
 (0)