-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Description
ApiVideoPlayerAnalyticsAgent can crash the host application when network errors occur during analytics batch reporting. The issue stems from two problems:
- No try/catch around HTTP calls in
reportBatch() - No
SupervisorJoborCoroutineExceptionHandleron the coroutine scope
When a network error like java.io.EOFException: unexpected end of stream occurs, the exception propagates uncaught and crashes the app.
Stack trace
java.io.EOFException: unexpected end of stream on ...
at okhttp3.internal.http1.Http1ExchangeCodec$AbstractSource.read(Http1ExchangeCodec.kt)
at io.ktor.client.engine.okhttp.OkHttpEngine...
...
at video.api.player.analytics.core.ApiVideoPlayerAnalyticsAgent$reportBatch$1.invokeSuspend(ApiVideoPlayerAnalyticsAgent.kt)
Problematic code
In ApiVideoPlayerAnalyticsAgent.kt:
// Scope without SupervisorJob - one exception cancels everything
private val scope = CoroutineScope(Dispatchers.Default)
// No try/catch around network call
private suspend fun reportBatch(batch: Batch) {
val httpResponse = client.post(options.collectorUrl) { ... }
// ...
}Suggested fix
Analytics should be "best-effort" and never crash the host app:
private val scope = CoroutineScope(
Dispatchers.Default +
SupervisorJob() +
CoroutineExceptionHandler { _, e ->
Log.w(TAG, "Analytics error (ignored): ${e.message}")
}
)
private suspend fun reportBatch(batch: Batch) {
try {
val httpResponse = client.post(options.collectorUrl) { ... }
// ...
} catch (e: Exception) {
Log.w(TAG, "Failed to send analytics batch: ${e.message}")
}
}Environment
- Library version: 3.0.0
- Android versions affected: All
- Ktor engine: Android (OkHttp)
Workaround
We've forked the player-analytics-core module locally with the above fix applied via Gradle dependency substitution.
Happy to submit a PR if you'd like!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels