Skip to content

Commit

Permalink
General protection against exceptions that occur on a thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
brismithers authored and jinliu9508 committed Feb 6, 2024
1 parent 7e6fd64 commit f39fd44
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 @@ -57,8 +63,13 @@ fun suspendifyOnMain(block: suspend () -> Unit) {
*/
fun suspendifyOnThread(priority: Int = -1, block: suspend () -> Unit) {
thread(priority = priority) {
runBlocking {
block()
try {
runBlocking {
block()
}
}
catch (e: Exception) {
Logging.error("Exception on thread", e)
}
}
}
Expand All @@ -71,8 +82,13 @@ fun suspendifyOnThread(priority: Int = -1, block: suspend () -> Unit) {
*/
fun suspendifyOnThread(name: String, priority: Int = -1, 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 f39fd44

Please sign in to comment.