Adds exception handlers for managed coroutine scopes

This commit is contained in:
Vitor Pamplona
2025-08-05 11:47:03 -04:00
parent e72ae565d3
commit a747f25906
3 changed files with 34 additions and 4 deletions
@@ -21,9 +21,11 @@
package com.vitorpamplona.amethyst.service.playback.playerPool package com.vitorpamplona.amethyst.service.playback.playerPool
import android.content.Context import android.content.Context
import android.util.Log
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
@@ -39,7 +41,14 @@ class ExoPlayerPool(
private val playerPool = ConcurrentLinkedQueue<ExoPlayer>() private val playerPool = ConcurrentLinkedQueue<ExoPlayer>()
private val poolSize = SimultaneousPlaybackCalculator.max() private val poolSize = SimultaneousPlaybackCalculator.max()
private val poolStartingSize = 3 private val poolStartingSize = 3
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
// Exists to avoid exceptions stopping the coroutine
val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
Log.e("BundledInsert", "Caught exception: ${throwable.message}", throwable)
}
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main + exceptionHandler)
private val mutex = Mutex() private val mutex = Mutex()
@@ -32,6 +32,7 @@ import com.vitorpamplona.amethyst.service.notifications.NotificationUtils.getOrC
import com.vitorpamplona.amethyst.service.notifications.NotificationUtils.getOrCreateZapChannel import com.vitorpamplona.amethyst.service.notifications.NotificationUtils.getOrCreateZapChannel
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
@@ -39,7 +40,13 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class PushNotificationReceiverService : FirebaseMessagingService() { class PushNotificationReceiverService : FirebaseMessagingService() {
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) // Exists to avoid exceptions stopping the coroutine
val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
Log.e("AmethystCoroutine", "Caught exception: ${throwable.message}", throwable)
}
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob() + exceptionHandler)
private val eventCache = LruCache<String, String>(100) private val eventCache = LruCache<String, String>(100)
// this is called when a message is received // this is called when a message is received
@@ -20,9 +20,11 @@
*/ */
package com.vitorpamplona.ammolite.relays package com.vitorpamplona.ammolite.relays
import android.util.Log
import androidx.compose.runtime.Stable import androidx.compose.runtime.Stable
import com.vitorpamplona.ammolite.service.checkNotInMainThread import com.vitorpamplona.ammolite.service.checkNotInMainThread
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.NonCancellable
@@ -40,7 +42,13 @@ class BundledUpdate(
val delay: Long, val delay: Long,
val dispatcher: CoroutineDispatcher = Dispatchers.Default, val dispatcher: CoroutineDispatcher = Dispatchers.Default,
) { ) {
val scope = CoroutineScope(dispatcher + SupervisorJob()) // Exists to avoid exceptions stopping the coroutine
val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
Log.e("BundledUpdate", "Caught exception: ${throwable.message}", throwable)
}
val scope = CoroutineScope(dispatcher + SupervisorJob() + exceptionHandler)
private var onlyOneInBlock = AtomicBoolean() private var onlyOneInBlock = AtomicBoolean()
private var invalidatesAgain = false private var invalidatesAgain = false
@@ -83,7 +91,13 @@ class BundledInsert<T>(
val delay: Long, val delay: Long,
val dispatcher: CoroutineDispatcher = Dispatchers.Default, val dispatcher: CoroutineDispatcher = Dispatchers.Default,
) { ) {
val scope = CoroutineScope(dispatcher + SupervisorJob()) // Exists to avoid exceptions stopping the coroutine
val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
Log.e("BundledInsert", "Caught exception: ${throwable.message}", throwable)
}
val scope = CoroutineScope(dispatcher + SupervisorJob() + exceptionHandler)
private var onlyOneInBlock = AtomicBoolean() private var onlyOneInBlock = AtomicBoolean()
private var queue = LinkedBlockingQueue<T>() private var queue = LinkedBlockingQueue<T>()