Skip to content

Commit ec35f43

Browse files
committed
1 parent e912335 commit ec35f43

File tree

1 file changed

+24
-2
lines changed
  • android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync

1 file changed

+24
-2
lines changed

android-components/components/browser/storage-sync/src/main/java/mozilla/components/browser/storage/sync/PlacesStorage.kt

+24-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import androidx.annotation.VisibleForTesting
99
import kotlinx.coroutines.CoroutineScope
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.asCoroutineDispatcher
12+
import kotlinx.coroutines.CoroutineExceptionHandler
1213
import kotlinx.coroutines.cancelChildren
14+
import kotlinx.coroutines.plus
1315
import kotlinx.coroutines.withContext
1416
import mozilla.appservices.places.PlacesReaderConnection
1517
import mozilla.appservices.places.PlacesWriterConnection
@@ -36,10 +38,30 @@ abstract class PlacesStorage(
3638
Executors.newSingleThreadExecutor(
3739
NamedThreadFactory("PlacesStorageWriteScope"),
3840
).asCoroutineDispatcher(),
39-
)
41+
) +
42+
CoroutineExceptionHandler { _, throwable ->
43+
// Workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/3328 to prevent
44+
// adding DiagnosticCoroutineContextException as suppressed exception which is not
45+
// serializable in 1.6.4. We are otherwise not able to serialize the exception and
46+
// pass it to our crash reporter. Without a handler on the context, the global handler
47+
// will otherwise add:
48+
// runCatching { exception.addSuppressed(DiagnosticCoroutineContextException(context)) }
49+
val currentThread = Thread.currentThread()
50+
currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, throwable)
51+
}
4052
@VisibleForTesting internal set
4153

42-
internal var readScope = CoroutineScope(Dispatchers.IO)
54+
internal var readScope = CoroutineScope(Dispatchers.IO) +
55+
CoroutineExceptionHandler { _, throwable ->
56+
// Workaround for https://github.com/Kotlin/kotlinx.coroutines/issues/3328 to prevent
57+
// adding DiagnosticCoroutineContextException as suppressed exception which is not
58+
// serializable in 1.6.4. We are otherwise not able to serialize the exception and
59+
// pass it to our crash reporter. Without a handler on the context, the global handler
60+
// will otherwise add:
61+
// runCatching { exception.addSuppressed(DiagnosticCoroutineContextException(context)) }
62+
val currentThread = Thread.currentThread()
63+
currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, throwable)
64+
}
4365
@VisibleForTesting internal set
4466
private val storageDir by lazy { context.filesDir }
4567

0 commit comments

Comments
 (0)