summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorAlbin <albin@mullvad.net>2024-08-15 10:30:28 +0200
committerAlbin <albin@mullvad.net>2024-08-15 10:30:28 +0200
commit90cb3c32545566164614fade40fe47971b6f0061 (patch)
treec1a1653c14bec4e7e053fda7e47cca02d7df1351 /android
parentad713fe8f86ee2e8c1cfd16a9fbb41f2a9b253e8 (diff)
parent62e16e8dbd19a8c5a6bc37e3f4b14713ebc9a7b6 (diff)
downloadmullvadvpn-90cb3c32545566164614fade40fe47971b6f0061.tar.xz
mullvadvpn-90cb3c32545566164614fade40fe47971b6f0061.zip
Merge branch 'fix-potential-crash-in-connection-state-function-droid-1224'
Diffstat (limited to 'android')
-rw-r--r--android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util/ManagedChannel.kt18
1 files changed, 11 insertions, 7 deletions
diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util/ManagedChannel.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util/ManagedChannel.kt
index 3f98ae93d8..a0e5bd5fd1 100644
--- a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util/ManagedChannel.kt
+++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/util/ManagedChannel.kt
@@ -1,24 +1,28 @@
package net.mullvad.mullvadvpn.lib.daemon.grpc.util
+import co.touchlab.kermit.Logger
import io.grpc.ConnectivityState
import io.grpc.ManagedChannel
-import kotlin.coroutines.resume
-import kotlin.coroutines.suspendCoroutine
+import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.isActive
+import kotlinx.coroutines.suspendCancellableCoroutine
+@OptIn(ExperimentalCoroutinesApi::class)
internal fun ManagedChannel.connectivityFlow(): Flow<ConnectivityState> {
return callbackFlow {
var currentState = getState(false)
- send(currentState)
while (isActive) {
- currentState =
- suspendCoroutine<ConnectivityState> {
- notifyWhenStateChanged(currentState) { it.resume(getState(false)) }
- }
+ // Check that we are active before sending
send(currentState)
+ currentState = suspendCancellableCoroutine {
+ notifyWhenStateChanged(currentState) {
+ // If we are cancelled we will just log
+ it.resume(getState(false)) { Logger.w("Resume while cancelled", it) }
+ }
+ }
}
}
}