Skip to content

Commit 63e533d

Browse files
jennantillajinliu9508
authored andcommitted
Merge pull request #1887 from /issues/1871-protect-thread-exceptions
General protection against exceptions that occur on a thread.
2 parents 020e512 + f39fd44 commit 63e533d

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

@@ -60,8 +66,13 @@ fun suspendifyOnThread(
6066
block: suspend () -> Unit,
6167
) {
6268
thread(priority = priority) {
63-
runBlocking {
64-
block()
69+
try {
70+
runBlocking {
71+
block()
72+
}
73+
}
74+
catch (e: Exception) {
75+
Logging.error("Exception on thread", e)
6576
}
6677
}
6778
}
@@ -78,8 +89,13 @@ fun suspendifyOnThread(
7889
block: suspend () -> Unit,
7990
) {
8091
thread(name = name, priority = priority) {
81-
runBlocking {
82-
block()
92+
try {
93+
runBlocking {
94+
block()
95+
}
96+
}
97+
catch (e: Exception) {
98+
Logging.error("Exception on thread '${name}'", e)
8399
}
84100
}
85101
}

0 commit comments

Comments
 (0)