Skip to content

Commit

Permalink
Merge pull request #1887 from /issues/1871-protect-thread-exceptions
Browse files Browse the repository at this point in the history
General protection against exceptions that occur on a thread.
  • Loading branch information
jennantilla authored and jinliu9508 committed Jan 31, 2024
2 parents 7a03c23 + 3983039 commit 48d0e58
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onesignal.common.threading

import com.onesignal.debug.internal.logging.Logging
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -41,11 +42,16 @@ fun suspendifyBlocking(block: suspend () -> Unit) {
*/
fun suspendifyOnMain(block: suspend () -> Unit) {
thread {
runBlocking {
withContext(Dispatchers.Main) {
block()
try {
runBlocking {
withContext(Dispatchers.Main) {
block()
}
}
}
catch (e: Exception) {
Logging.error("Exception on thread with switch to main", e)
}
}
}

Expand All @@ -60,8 +66,13 @@ fun suspendifyOnThread(
block: suspend () -> Unit,
) {
thread(priority = priority) {
runBlocking {
block()
try {
runBlocking {
block()
}
}
catch (e: Exception) {
Logging.error("Exception on thread", e)
}
}
}
Expand All @@ -78,8 +89,13 @@ fun suspendifyOnThread(
block: suspend () -> Unit,
) {
thread(name = name, priority = priority) {
runBlocking {
block()
try {
runBlocking {
block()
}
}
catch (e: Exception) {
Logging.error("Exception on thread '${name}'", e)
}
}
}

0 comments on commit 48d0e58

Please sign in to comment.