Skip to content

Commit 5d20ce5

Browse files
0xadam-browncodex
andcommitted
fix(samples): Close route work OkHttp responses
Close Retrofit response bodies for both synchronous and asynchronous route-work requests so repeated navigation cannot leak OkHttp connections. Co-Authored-By: Codex <noreply@openai.com>
1 parent 01a55ca commit 5d20ce5

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/common/RouteWorkApi.kt‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,29 @@ internal object RouteWorkApi {
3232

3333
private val service: RouteWorkService = retrofit.create(RouteWorkService::class.java)
3434

35-
fun enqueueRequest(callback: retrofit2.Callback<ResponseBody>) {
36-
service.request(ROUTE_WORK_URL).enqueue(callback)
35+
fun enqueueRequest(onSuccess: () -> Unit, onFailure: (Throwable) -> Unit) {
36+
service
37+
.request(ROUTE_WORK_URL)
38+
.enqueue(
39+
object : retrofit2.Callback<ResponseBody> {
40+
override fun onResponse(
41+
call: retrofit2.Call<ResponseBody>,
42+
response: retrofit2.Response<ResponseBody>,
43+
) {
44+
response.body()?.close()
45+
response.errorBody()?.close()
46+
onSuccess()
47+
}
48+
49+
override fun onFailure(call: retrofit2.Call<ResponseBody>, t: Throwable) {
50+
onFailure(t)
51+
}
52+
}
53+
)
3754
}
3855

3956
suspend fun runRequest() {
40-
service.requestAsync(ROUTE_WORK_URL)
57+
service.requestAsync(ROUTE_WORK_URL).use {}
4158
}
4259
}
4360

‎sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav2/Nav2Activity.kt‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ import io.sentry.samples.android.navigation.common.hasOnlyActivityUiLoadTransact
4545
import io.sentry.samples.android.navigation.common.showRouteWorkDialog
4646
import io.sentry.samples.android.navigation.nav2.Nav2Destination.Home
4747
import io.sentry.samples.android.navigation.nav2.Nav2Destination.Landing
48-
import okhttp3.ResponseBody
49-
import retrofit2.Call
50-
import retrofit2.Callback
51-
import retrofit2.Response
5248

5349
/**
5450
* Sample activity for testing Sentry's
@@ -396,16 +392,11 @@ class Nav2Activity : AppCompatActivity() {
396392
when (option) {
397393
RouteWorkOption.HTTP_REQUEST -> {
398394
RouteWorkApi.enqueueRequest(
399-
object : Callback<ResponseBody> {
400-
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
401-
Thread { Sentry.flush(SENTRY_FLUSH_TIMEOUT_MILLIS) }.start()
402-
}
403-
404-
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
405-
Sentry.captureException(t)
406-
Thread { Sentry.flush(SENTRY_FLUSH_TIMEOUT_MILLIS) }.start()
407-
}
408-
}
395+
onSuccess = { Thread { Sentry.flush(SENTRY_FLUSH_TIMEOUT_MILLIS) }.start() },
396+
onFailure = { t ->
397+
Sentry.captureException(t)
398+
Thread { Sentry.flush(SENTRY_FLUSH_TIMEOUT_MILLIS) }.start()
399+
},
409400
)
410401
}
411402

0 commit comments

Comments
 (0)