Skip to content

Uncaught network exceptions in ApiVideoPlayerAnalyticsAgent crash the app #6

@Kppro

Description

@Kppro

Description

ApiVideoPlayerAnalyticsAgent can crash the host application when network errors occur during analytics batch reporting. The issue stems from two problems:

  1. No try/catch around HTTP calls in reportBatch()
  2. No SupervisorJob or CoroutineExceptionHandler on 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions