Skip to content

Commit e22c18f

Browse files
fix: cancel eb coroutine job when we shutdown (#303)
1 parent d042c77 commit e22c18f

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/main/kotlin/com/statsig/sdk/ErrorBoundary.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.statsig.sdk
22

33
import com.google.gson.Gson
4-
import kotlinx.coroutines.CoroutineExceptionHandler
5-
import kotlinx.coroutines.CoroutineScope
6-
import kotlinx.coroutines.Dispatchers
7-
import kotlinx.coroutines.launch
4+
import kotlinx.coroutines.*
85
import okhttp3.Call
96
import okhttp3.Callback
107
import okhttp3.MediaType.Companion.toMediaType
@@ -25,6 +22,7 @@ internal class ErrorBoundary(private val apiKey: String, private val options: St
2522
OkHttpClient()
2623
}
2724
internal var diagnostics: Diagnostics? = null
25+
private val coroutineScope = CoroutineScope(this.getNoopExceptionHandler() + Dispatchers.IO)
2826

2927
private companion object {
3028
val MEDIA_TYPE = "application/json; charset=utf-8".toMediaType()
@@ -51,6 +49,7 @@ internal class ErrorBoundary(private val apiKey: String, private val options: St
5149
fun shutdown() {
5250
// Properly close the OkHttpClient to release resources
5351
try {
52+
coroutineScope.cancel()
5453
client.dispatcher.executorService.shutdown()
5554
client.connectionPool.evictAll()
5655
client.cache?.close()
@@ -92,7 +91,7 @@ internal class ErrorBoundary(private val apiKey: String, private val options: St
9291

9392
internal fun logException(tag: String, ex: Throwable, configName: String? = null, extraInfo: String? = null, bypassDedupe: Boolean = false) {
9493
try {
95-
CoroutineScope(this.getNoopExceptionHandler() + Dispatchers.IO).launch {
94+
coroutineScope.launch {
9695
if (options.localMode || options.disableAllLogging) {
9796
return@launch
9897
}

0 commit comments

Comments
 (0)