Skip to content

Don't swallow CancellationException #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import io.github.jan.supabase.plugins.CustomSerializationPlugin
import io.github.jan.supabase.plugins.MainPlugin
import io.github.jan.supabase.plugins.SupabasePluginProvider
import io.ktor.client.plugins.HttpRequestTimeoutException
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.StateFlow
import kotlinx.serialization.json.JsonObject
import kotlin.coroutines.coroutineContext

/**
* Plugin to interact with the Supabase Auth API
Expand Down Expand Up @@ -434,6 +436,7 @@ val SupabaseClient.auth: Auth
private suspend fun Auth.tryToGetUser(jwt: String) = try {
retrieveUser(jwt)
} catch (e: Exception) {
coroutineContext.ensureActive()
Auth.logger.e(e) { "Couldn't retrieve user using your custom jwt token. If you use the project secret ignore this message" }
null
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -56,6 +57,7 @@ import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import kotlin.coroutines.coroutineContext
import kotlin.time.Duration.Companion.seconds

private const val SESSION_REFRESH_THRESHOLD = 0.8
Expand Down Expand Up @@ -466,6 +468,7 @@ internal class AuthImpl(
clearSession()
}
} catch (e: Exception) {
coroutineContext.ensureActive()
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}..." }
_sessionStatus.value = SessionStatus.RefreshFailure(RefreshFailureCause.NetworkError(e))
delay(config.retryDelay)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.russhwolf.settings.Settings
import com.russhwolf.settings.coroutines.toSuspendSettings
import io.github.jan.supabase.auth.user.UserSession
import io.github.jan.supabase.logging.e
import kotlinx.coroutines.ensureActive
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder
import kotlin.coroutines.coroutineContext

private val settingsJson = Json {
encodeDefaults = true
Expand Down Expand Up @@ -54,6 +56,7 @@ class SettingsSessionManager(
return try {
json.decodeFromString(session)
} catch(e: Exception) {
coroutineContext.ensureActive()
Auth.logger.e(e) { "Failed to load session" }
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.github.jan.supabase.realtime.event.RealtimeEvent
import io.ktor.client.statement.bodyAsText
import io.ktor.http.headers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -199,6 +200,7 @@ internal class RealtimeChannelImpl(
val decodedValue = try {
supabaseClient.realtime.serializer.decode<T>(type, it.toString())
} catch(e: Exception) {
coroutineContext.ensureActive()
Realtime.logger.e(e) { "Couldn't decode $it as $type. The corresponding handler wasn't called" }
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -36,6 +37,7 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.longOrNull
import kotlin.coroutines.coroutineContext
import kotlin.io.encoding.ExperimentalEncodingApi

@PublishedApi internal class RealtimeImpl(override val supabaseClient: SupabaseClient, override val config: Realtime.Config) : Realtime {
Expand Down Expand Up @@ -83,6 +85,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
rejoinChannels()
}
} catch(e: Exception) {
coroutineContext.ensureActive()
Realtime.logger.e(e) { """
Error while trying to connect to realtime websocket. Trying again in ${config.reconnectDelay}
URL: $websocketUrl
Expand Down Expand Up @@ -127,7 +130,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
}
}
} catch(e: Exception) {
if(!isActive) return@launch
coroutineContext.ensureActive()
Realtime.logger.e(e) { "Error while listening for messages. Trying again in ${config.reconnectDelay}" }
reconnect()
}
Expand Down Expand Up @@ -274,6 +277,7 @@ import kotlin.io.encoding.ExperimentalEncodingApi
try {
ws?.send(message)
} catch(e: Exception) {
coroutineContext.ensureActive()
Realtime.logger.e(e) { "Error while sending message $message. Reconnecting in ${config.reconnectDelay}" }
reconnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -122,6 +123,7 @@ internal class ResumableUploadImpl(
dataStream.cancel() //cancel old data stream as we are start reading from a new offset
dataStream = createDataStream(offset) //create new data stream
} catch(e: Exception) {
coroutineContext.ensureActive()
Storage.logger.e(e) { "Error while updating server offset for $path. Retrying in ${config.retryTimeout}" }
delay(config.retryTimeout)
continue
Expand All @@ -132,6 +134,7 @@ internal class ResumableUploadImpl(
val uploaded = uploadChunk()
offset += uploaded
} catch(e: Exception) {
coroutineContext.ensureActive()
if(e !is IllegalStateException) {
Storage.logger.e(e) {"Error while uploading chunk. Retrying in ${config.retryTimeout}" }
delay(config.retryTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ suspend inline fun <reified T> HttpResponse.bodyOrNull(): T? {
return try {
val text = bodyAsText()
supabaseJson.decodeFromString<T>(text)
} catch(e: Exception) {
} catch(_: Exception) {
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import io.github.jan.supabase.compose.auth.hash
import io.github.jan.supabase.compose.auth.signInWithGoogle
import io.github.jan.supabase.logging.d
import io.github.jan.supabase.logging.e
import kotlinx.coroutines.ensureActive
import kotlin.coroutines.coroutineContext

private data class GoogleRequestOptions(
val config: GoogleLoginConfig?,
Expand Down Expand Up @@ -100,6 +102,7 @@ internal fun ComposeAuth.signInWithCM(
}
}
} catch (e: Exception) {
coroutineContext.ensureActive()
onResult.invoke(NativeSignInResult.Error(e.localizedMessage ?: "error", e))
ComposeAuth.logger.e(e) { "Error while logging into Supabase with Google ID Token Credential" }
} finally {
Expand Down Expand Up @@ -135,6 +138,7 @@ private suspend fun parseCredential(
)
)
} catch (e: Exception) {
coroutineContext.ensureActive()
ComposeAuth.logger.e(e) { "Error while logging into Supabase with Google ID Token Credential" }
onResult.invoke(
NativeSignInResult.Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.github.jan.supabase.compose.auth.hash
import io.github.jan.supabase.compose.auth.signInWithApple
import kotlinx.cinterop.BetaInteropApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import platform.AuthenticationServices.ASAuthorization
import platform.AuthenticationServices.ASAuthorizationAppleIDCredential
Expand Down Expand Up @@ -80,6 +81,7 @@ actual fun ComposeAuth.rememberSignInWithApple(
fallback.invoke()
}
} catch (e: Exception) {
coroutineContext.ensureActive()
onResult.invoke(NativeSignInResult.Error(e.message ?: "error"))
} finally {
state.reset()
Expand Down