From 915772f4a31b291acc9e7dc43384eac884c8cdfb Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Tue, 4 Apr 2023 17:56:59 -0400 Subject: [PATCH] Workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/3328 --- .../browser/storage/sync/PlacesStorage.kt | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync/PlacesStorage.kt b/android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync/PlacesStorage.kt index aba475ffd63f..d47eaeccce06 100644 --- a/android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync/PlacesStorage.kt +++ b/android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync/PlacesStorage.kt @@ -6,11 +6,7 @@ package mozilla.components.browser.storage.sync import android.content.Context import androidx.annotation.VisibleForTesting -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.asCoroutineDispatcher -import kotlinx.coroutines.cancelChildren -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* import mozilla.appservices.places.PlacesReaderConnection import mozilla.appservices.places.PlacesWriterConnection import mozilla.appservices.places.uniffi.PlacesApiException @@ -36,10 +32,30 @@ abstract class PlacesStorage( Executors.newSingleThreadExecutor( NamedThreadFactory("PlacesStorageWriteScope"), ).asCoroutineDispatcher(), - ) + ) + + CoroutineExceptionHandler { _, throwable -> + // Workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/3328 to prevent + // adding DiagnosticCoroutineContextException as suppressed exception which is not + // serializable in 1.6.4. We are otherwise not able to serialize the exception and + // pass it to our crash reporter. Without a handler on the context, the global handler + // will otherwise add: + // runCatching { exception.addSuppressed(DiagnosticCoroutineContextException(context)) } + val currentThread = Thread.currentThread() + currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, throwable) + } @VisibleForTesting internal set - internal var readScope = CoroutineScope(Dispatchers.IO) + internal var readScope = CoroutineScope(Dispatchers.IO) + + CoroutineExceptionHandler { _, throwable -> + // Workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/3328 to prevent + // adding DiagnosticCoroutineContextException as suppressed exception which is not + // serializable in 1.6.4. We are otherwise not able to serialize the exception and + // pass it to our crash reporter. Without a handler on the context, the global handler + // will otherwise add: + // runCatching { exception.addSuppressed(DiagnosticCoroutineContextException(context)) } + val currentThread = Thread.currentThread() + currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, throwable) + } @VisibleForTesting internal set private val storageDir by lazy { context.filesDir }