firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.28k stars 578 forks source link

HttpMetric::stop throws some exceptions #5505

Open eduardbosch-jt opened 1 year ago

eduardbosch-jt commented 1 year ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

I cannot reproduce it.

For some reason, we are seeing some crashes when we call HttpMetric::stop. Should this method ever throw an exception?

It looks like it's related to Protobuf not being able to update some data. Sometimes it says it's not readable and some other times it tries to write outside the current range.

Here you can see some stack traces:

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
       at com.google.protobuf.ProtobufArrayList.add(ProtobufArrayList.java:88)
       at com.google.protobuf.AbstractMessageLite$Builder.addAllCheckingNulls(AbstractMessageLite.java:384)
       at com.google.protobuf.AbstractMessageLite$Builder.addAll(AbstractMessageLite.java:437)
       at com.google.protobuf.AbstractMessageLite.addAll(AbstractMessageLite.java:146)
       at com.google.firebase.perf.v1.NetworkRequestMetric.addAllPerfSessions(NetworkRequestMetric.java:1228)
       at com.google.firebase.perf.v1.NetworkRequestMetric.access$2900(NetworkRequestMetric.java:15)
       at com.google.firebase.perf.v1.NetworkRequestMetric$Builder.addAllPerfSessions(NetworkRequestMetric.java:2346)
       at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.build(NetworkRequestMetricBuilder.java:318)
       at com.google.firebase.perf.metrics.HttpMetric.stop(HttpMetric.java:151)
       ...
Fatal Exception: java.lang.UnsupportedOperationException:
       at com.google.protobuf.AbstractProtobufList.ensureIsMutable(AbstractProtobufList.java:177)
       at com.google.protobuf.ProtobufArrayList.add(ProtobufArrayList.java:78)
       at com.google.protobuf.AbstractMessageLite$Builder.addAllCheckingNulls(AbstractMessageLite.java:384)
       at com.google.protobuf.AbstractMessageLite$Builder.addAll(AbstractMessageLite.java:437)
       at com.google.protobuf.AbstractMessageLite.addAll(AbstractMessageLite.java:146)
       at com.google.firebase.perf.v1.NetworkRequestMetric.addAllPerfSessions(NetworkRequestMetric.java:1228)
       at com.google.firebase.perf.v1.NetworkRequestMetric.access$2900(NetworkRequestMetric.java:15)
       at com.google.firebase.perf.v1.NetworkRequestMetric$Builder.addAllPerfSessions(NetworkRequestMetric.java:2346)
       at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.build(NetworkRequestMetricBuilder.java:318)
       at com.google.firebase.perf.metrics.HttpMetric.stop(HttpMetric.java:151)
       ...

Relevant Code:

We are using a simple custom interceptor to send the data. I'm not sharing the real code, as it has some abstractions, but it should be this:

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val url = request.url.toString()
        val httpMethod = request.method

        val httpMetric = firebasePerformance.newHttpMetric(name, httpMethod)
        httpMetric.putAttribute("http_method", httpMethod)
        httpMetric.start()
        httpMetric.requestSize(request.body?.contentLength() ?: 0L)
        try {
            val response = chain.proceed(chain.request())
            httpMetric.responseCode(response.code)
            httpMetric.payloadSize(response.body?.contentLength() ?: 0L)
            httpMetric.stop() <-- This is the next line in the previous stack traces always
            return response
        } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
            httpMetric.putAttribute("error_request", "exception")
            httpMetric.stop()
            throw e
        }
    }
exaby73 commented 2 weeks ago

Hello @eduardbosch-jt. Are you still able to see crash logs related to this issue with the latest version of Firebase Performace?

eduardbosch-jt commented 2 weeks ago

Hey @exaby73 I'm sorry but we removed the interceptor to monitor the network requests performance so I cannot verify it anymore.

visumickey commented 1 week ago

Here is the part of the code that is relevant towards the crash: https://github.com/firebase/firebase-android-sdk/blob/main/firebase-perf/src/main/java/com/google/firebase/perf/metrics/NetworkRequestMetricBuilder.java#L315-L319.

Looking at the code, we don't see anything suspicious that could cause this crash. The potential situation could be a crash arising because of a different version of protobuf lite. Can you please share us the version of the protobuf-lite that is being used in this application to debug this further?

google-oss-bot commented 1 day ago

Hey @eduardbosch-jt. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!