Skip to content

Commit f39fd44

Browse files
brismithersjinliu9508
authored andcommitted
General protection against exceptions that occur on a thread.
1 parent 7e6fd64 commit f39fd44

File tree

1 file changed

+23
-7
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading

1 file changed

+23
-7
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/ThreadUtils.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.common.threading
22

3+
import com.onesignal.debug.internal.logging.Logging
34
import kotlinx.coroutines.Dispatchers
45
import kotlinx.coroutines.runBlocking
56
import kotlinx.coroutines.withContext
@@ -41,11 +42,16 @@ fun suspendifyBlocking(block: suspend () -> Unit) {
4142
*/
4243
fun suspendifyOnMain(block: suspend () -> Unit) {
4344
thread {
44-
runBlocking {
45-
withContext(Dispatchers.Main) {
46-
block()
45+
try {
46+
runBlocking {
47+
withContext(Dispatchers.Main) {
48+
block()
49+
}
4750
}
4851
}
52+
catch (e: Exception) {
53+
Logging.error("Exception on thread with switch to main", e)
54+
}
4955
}
5056
}
5157

@@ -57,8 +63,13 @@ fun suspendifyOnMain(block: suspend () -> Unit) {
5763
*/
5864
fun suspendifyOnThread(priority: Int = -1, block: suspend () -> Unit) {
5965
thread(priority = priority) {
60-
runBlocking {
61-
block()
66+
try {
67+
runBlocking {
68+
block()
69+
}
70+
}
71+
catch (e: Exception) {
72+
Logging.error("Exception on thread", e)
6273
}
6374
}
6475
}
@@ -71,8 +82,13 @@ fun suspendifyOnThread(priority: Int = -1, block: suspend () -> Unit) {
7182
*/
7283
fun suspendifyOnThread(name: String, priority: Int = -1, block: suspend () -> Unit) {
7384
thread(name = name, priority = priority) {
74-
runBlocking {
75-
block()
85+
try {
86+
runBlocking {
87+
block()
88+
}
89+
}
90+
catch (e: Exception) {
91+
Logging.error("Exception on thread '${name}'", e)
7692
}
7793
}
7894
}

0 commit comments

Comments
 (0)