Skip to content

Commit 705fff9

Browse files
authored
Merge pull request #895 from sproctor/fix-cancellation-exceptions
Don't swallow `CancellationException`
2 parents c4163bb + 3be5b94 commit 705fff9

File tree

9 files changed

+26
-2
lines changed

9 files changed

+26
-2
lines changed

Auth/src/commonMain/kotlin/io/github/jan/supabase/auth/Auth.kt

+3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import io.github.jan.supabase.plugins.CustomSerializationPlugin
2525
import io.github.jan.supabase.plugins.MainPlugin
2626
import io.github.jan.supabase.plugins.SupabasePluginProvider
2727
import io.ktor.client.plugins.HttpRequestTimeoutException
28+
import kotlinx.coroutines.ensureActive
2829
import kotlinx.coroutines.flow.StateFlow
2930
import kotlinx.serialization.json.JsonObject
31+
import kotlin.coroutines.coroutineContext
3032

3133
/**
3234
* Plugin to interact with the Supabase Auth API
@@ -434,6 +436,7 @@ val SupabaseClient.auth: Auth
434436
private suspend fun Auth.tryToGetUser(jwt: String) = try {
435437
retrieveUser(jwt)
436438
} catch (e: Exception) {
439+
coroutineContext.ensureActive()
437440
Auth.logger.e(e) { "Couldn't retrieve user using your custom jwt token. If you use the project secret ignore this message" }
438441
null
439442
}

Auth/src/commonMain/kotlin/io/github/jan/supabase/auth/AuthImpl.kt

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.Job
4343
import kotlinx.coroutines.cancel
4444
import kotlinx.coroutines.delay
45+
import kotlinx.coroutines.ensureActive
4546
import kotlinx.coroutines.flow.MutableStateFlow
4647
import kotlinx.coroutines.flow.StateFlow
4748
import kotlinx.coroutines.flow.asStateFlow
@@ -56,6 +57,7 @@ import kotlinx.serialization.json.encodeToJsonElement
5657
import kotlinx.serialization.json.jsonObject
5758
import kotlinx.serialization.json.jsonPrimitive
5859
import kotlinx.serialization.json.put
60+
import kotlin.coroutines.coroutineContext
5961
import kotlin.time.Duration.Companion.seconds
6062

6163
private const val SESSION_REFRESH_THRESHOLD = 0.8
@@ -466,6 +468,7 @@ internal class AuthImpl(
466468
clearSession()
467469
}
468470
} catch (e: Exception) {
471+
coroutineContext.ensureActive()
469472
Auth.logger.e(e) { "Couldn't reach Supabase. Either the address doesn't exist or the network might not be on. Retrying in ${config.retryDelay}..." }
470473
_sessionStatus.value = SessionStatus.RefreshFailure(RefreshFailureCause.NetworkError(e))
471474
delay(config.retryDelay)

Auth/src/settingsMain/kotlin/io/github/jan/supabase/auth/SettingsSessionManager.kt

+3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import com.russhwolf.settings.Settings
55
import com.russhwolf.settings.coroutines.toSuspendSettings
66
import io.github.jan.supabase.auth.user.UserSession
77
import io.github.jan.supabase.logging.e
8+
import kotlinx.coroutines.ensureActive
89
import kotlinx.serialization.encodeToString
910
import kotlinx.serialization.json.Json
1011
import kotlinx.serialization.json.JsonBuilder
12+
import kotlin.coroutines.coroutineContext
1113

1214
private val settingsJson = Json {
1315
encodeDefaults = true
@@ -54,6 +56,7 @@ class SettingsSessionManager(
5456
return try {
5557
json.decodeFromString(session)
5658
} catch(e: Exception) {
59+
coroutineContext.ensureActive()
5760
Auth.logger.e(e) { "Failed to load session" }
5861
null
5962
}

Realtime/src/commonMain/kotlin/io/github/jan/supabase/realtime/RealtimeChannelImpl.kt

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.github.jan.supabase.realtime.event.RealtimeEvent
1111
import io.ktor.client.statement.bodyAsText
1212
import io.ktor.http.headers
1313
import kotlinx.coroutines.channels.awaitClose
14+
import kotlinx.coroutines.ensureActive
1415
import kotlinx.coroutines.flow.Flow
1516
import kotlinx.coroutines.flow.MutableStateFlow
1617
import kotlinx.coroutines.flow.asStateFlow
@@ -199,6 +200,7 @@ internal class RealtimeChannelImpl(
199200
val decodedValue = try {
200201
supabaseClient.realtime.serializer.decode<T>(type, it.toString())
201202
} catch(e: Exception) {
203+
coroutineContext.ensureActive()
202204
Realtime.logger.e(e) { "Couldn't decode $it as $type. The corresponding handler wasn't called" }
203205
null
204206
}

Realtime/src/commonMain/kotlin/io/github/jan/supabase/realtime/RealtimeImpl.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlinx.coroutines.Dispatchers
2323
import kotlinx.coroutines.Job
2424
import kotlinx.coroutines.SupervisorJob
2525
import kotlinx.coroutines.delay
26+
import kotlinx.coroutines.ensureActive
2627
import kotlinx.coroutines.flow.MutableStateFlow
2728
import kotlinx.coroutines.flow.StateFlow
2829
import kotlinx.coroutines.flow.asStateFlow
@@ -36,6 +37,7 @@ import kotlinx.serialization.json.JsonObject
3637
import kotlinx.serialization.json.buildJsonObject
3738
import kotlinx.serialization.json.jsonPrimitive
3839
import kotlinx.serialization.json.longOrNull
40+
import kotlin.coroutines.coroutineContext
3941
import kotlin.io.encoding.ExperimentalEncodingApi
4042

4143
@PublishedApi internal class RealtimeImpl(override val supabaseClient: SupabaseClient, override val config: Realtime.Config) : Realtime {
@@ -83,6 +85,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
8385
rejoinChannels()
8486
}
8587
} catch(e: Exception) {
88+
coroutineContext.ensureActive()
8689
Realtime.logger.e(e) { """
8790
Error while trying to connect to realtime websocket. Trying again in ${config.reconnectDelay}
8891
URL: $websocketUrl
@@ -127,7 +130,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
127130
}
128131
}
129132
} catch(e: Exception) {
130-
if(!isActive) return@launch
133+
coroutineContext.ensureActive()
131134
Realtime.logger.e(e) { "Error while listening for messages. Trying again in ${config.reconnectDelay}" }
132135
reconnect()
133136
}
@@ -274,6 +277,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
274277
try {
275278
ws?.send(message)
276279
} catch(e: Exception) {
280+
coroutineContext.ensureActive()
277281
Realtime.logger.e(e) { "Error while sending message $message. Reconnecting in ${config.reconnectDelay}" }
278282
reconnect()
279283
}

Storage/src/commonMain/kotlin/io/github/jan/supabase/storage/resumable/ResumableUpload.kt

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import kotlinx.coroutines.CoroutineScope
2828
import kotlinx.coroutines.Dispatchers
2929
import kotlinx.coroutines.cancel
3030
import kotlinx.coroutines.delay
31+
import kotlinx.coroutines.ensureActive
3132
import kotlinx.coroutines.flow.MutableStateFlow
3233
import kotlinx.coroutines.flow.StateFlow
3334
import kotlinx.coroutines.flow.asStateFlow
@@ -122,6 +123,7 @@ internal class ResumableUploadImpl(
122123
dataStream.cancel() //cancel old data stream as we are start reading from a new offset
123124
dataStream = createDataStream(offset) //create new data stream
124125
} catch(e: Exception) {
126+
coroutineContext.ensureActive()
125127
Storage.logger.e(e) { "Error while updating server offset for $path. Retrying in ${config.retryTimeout}" }
126128
delay(config.retryTimeout)
127129
continue
@@ -132,6 +134,7 @@ internal class ResumableUploadImpl(
132134
val uploaded = uploadChunk()
133135
offset += uploaded
134136
} catch(e: Exception) {
137+
coroutineContext.ensureActive()
135138
if(e !is IllegalStateException) {
136139
Storage.logger.e(e) {"Error while uploading chunk. Retrying in ${config.retryTimeout}" }
137140
delay(config.retryTimeout)

Supabase/src/commonMain/kotlin/io/github/jan/supabase/Utils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ suspend inline fun <reified T> HttpResponse.bodyOrNull(): T? {
6161
return try {
6262
val text = bodyAsText()
6363
supabaseJson.decodeFromString<T>(text)
64-
} catch(e: Exception) {
64+
} catch(_: Exception) {
6565
null
6666
}
6767
}

plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import io.github.jan.supabase.compose.auth.hash
2626
import io.github.jan.supabase.compose.auth.signInWithGoogle
2727
import io.github.jan.supabase.logging.d
2828
import io.github.jan.supabase.logging.e
29+
import kotlinx.coroutines.ensureActive
30+
import kotlin.coroutines.coroutineContext
2931

3032
private data class GoogleRequestOptions(
3133
val config: GoogleLoginConfig?,
@@ -100,6 +102,7 @@ internal fun ComposeAuth.signInWithCM(
100102
}
101103
}
102104
} catch (e: Exception) {
105+
coroutineContext.ensureActive()
103106
onResult.invoke(NativeSignInResult.Error(e.localizedMessage ?: "error", e))
104107
ComposeAuth.logger.e(e) { "Error while logging into Supabase with Google ID Token Credential" }
105108
} finally {
@@ -135,6 +138,7 @@ private suspend fun parseCredential(
135138
)
136139
)
137140
} catch (e: Exception) {
141+
coroutineContext.ensureActive()
138142
ComposeAuth.logger.e(e) { "Error while logging into Supabase with Google ID Token Credential" }
139143
onResult.invoke(
140144
NativeSignInResult.Error(

plugins/ComposeAuth/src/appleMain/kotlin/io/github/jan/supabase/compose/auth/composable/AppleAuth.kt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.github.jan.supabase.compose.auth.hash
1212
import io.github.jan.supabase.compose.auth.signInWithApple
1313
import kotlinx.cinterop.BetaInteropApi
1414
import kotlinx.coroutines.CoroutineScope
15+
import kotlinx.coroutines.ensureActive
1516
import kotlinx.coroutines.launch
1617
import platform.AuthenticationServices.ASAuthorization
1718
import platform.AuthenticationServices.ASAuthorizationAppleIDCredential
@@ -80,6 +81,7 @@ actual fun ComposeAuth.rememberSignInWithApple(
8081
fallback.invoke()
8182
}
8283
} catch (e: Exception) {
84+
coroutineContext.ensureActive()
8385
onResult.invoke(NativeSignInResult.Error(e.message ?: "error"))
8486
} finally {
8587
state.reset()

0 commit comments

Comments
 (0)