firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

FirebaseInstallations doesn't correctly close opened stream

Open lexer opened this issue 6 years ago • 38 comments

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 3.6.1
  • Firebase Component: com.google.firebase:firebase-perf, com.google.firebase:firebase-messaging
  • Component version: 19.0.5, 20.1.2

[REQUIRED] Step 3: Describe the problem

Firebase SDK crashes if StrictMode detectLeakedRegistrationObjects() policy is enabled.

Steps to reproduce:

Strict mode configuration:

val vmBuilder = VmPolicy.Builder()
                .detectActivityLeaks()
                .detectFileUriExposure()
                .detectLeakedClosableObjects()
                .detectLeakedRegistrationObjects()
                .detectLeakedSqlLiteObjects()
                .penaltyLog()
                .penaltyDeath()
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  vmBuilder.detectContentUriWithoutPermission()
            }
 StrictMode.setVmPolicy(vmBuilder.build())

Relevant Code:

Crash log.

StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286)
        at java.util.zip.Inflater.finalize(Inflater.java:407)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276)
        at java.lang.Daemons$Daemon.run(Daemons.java:137)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:237)
        at java.util.zip.Inflater.<init>(Inflater.java:122)
        at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:64)
        at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.createFirebaseInstallation(com.google.firebase:firebase-installations@@16.0.0:154)
        at com.google.firebase.installations.FirebaseInstallations.registerFidWithServer(com.google.firebase:firebase-installations@@16.0.0:411)
        at com.google.firebase.installations.FirebaseInstallations.doRegistrationInternal(com.google.firebase:firebase-installations@@16.0.0:316)
        at com.google.firebase.installations.FirebaseInstallations.doGetId(com.google.firebase:firebase-installations@@16.0.0:280)
        at com.google.firebase.installations.FirebaseInstallations.access$lambda$0(Unknown Source:0)
        at com.google.firebase.installations.FirebaseInstallations$$Lambda$1.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919) 

lexer avatar Mar 08 '20 04:03 lexer

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Mar 08 '20 04:03 google-oss-bot

Thank you for reporting this problem. We will look into this and, if necessary, will try to release a fix next week.

andirayo avatar Mar 08 '20 17:03 andirayo

Was this a warning or error? Also, did the app crash with this stack trace?

ankitaj224 avatar Mar 09 '20 20:03 ankitaj224

I tried reproducing but couldn't. Do you get this stack trace consistently?

ankitaj224 avatar Mar 09 '20 20:03 ankitaj224

whats the okhttp library version you are using?

ankitaj224 avatar Mar 09 '20 21:03 ankitaj224

@ankitaj224 I have reproduced on an empty app. Here is instructions:

Create an App.kt

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        val vmBuilder = StrictMode.VmPolicy.Builder()
            .detectActivityLeaks()
            .detectFileUriExposure()
            .detectLeakedClosableObjects()
            .detectLeakedRegistrationObjects()
            .detectLeakedSqlLiteObjects()
            .penaltyLog()
            .penaltyDeath()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vmBuilder.detectContentUriWithoutPermission()
        }

        StrictMode.setVmPolicy(vmBuilder.build())
        StrictMode.setThreadPolicy(
            StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                // https://issuetracker.google.com/issues/63298071
                // .penaltyDeath()
                .build()
        )

        FirebaseApp.initializeApp(
            this,
            FirebaseOptions.Builder()
                .setApplicationId("com.squareup.cash.beta.debug")
                .setProjectId(getString(R.string.firebase_project_id))
                .setApiKey(getString(R.string.firebase_api_key))
                .setGcmSenderId(getString(R.string.firebase_sender_id))
                .build()
        )

        FirebasePerformance.getInstance()
            .isPerformanceCollectionEnabled = true
    }
}

project build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
  compileSdkVersion 29
  buildToolsVersion "29.0.2"

  defaultConfig {
    applicationId "com.squareup.cash"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  implementation 'androidx.appcompat:appcompat:1.0.2'
  implementation 'androidx.core:core-ktx:1.0.2'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'com.google.firebase:firebase-messaging:20.1.2'
  implementation 'com.google.firebase:firebase-perf:19.0.5'
  implementation 'com.squareup.okhttp3:okhttp:4.4.0'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

You need to make sure that valid Firebase configuration is used otherwise SDK won't send a request and app won't crash.

Let me know if you want me to archive and send entire empty project to you.

I tried reproducing but couldn't. Do you get this stack trace consistently?

Yes, this happens 100% on every start.

lexer avatar Mar 10 '20 20:03 lexer

@lexer If you could archive and send me the github link that would be very helpful. I tried with the library version but still cant reproduce the exact violation you reported.

Out of curiosity, does your app require to run with these strict mode violations or is it part of your dev testing?

ankitaj224 avatar Mar 10 '20 22:03 ankitaj224

sounds good @ankitaj224 i will create a private repo with demo project and share it directly with you.

Out of curiosity, does your app require to run with these strict mode violations or is it part of your dev testing?

Cash App team has a very high standard and tries to have 0 strict mode violations.

lexer avatar Mar 11 '20 01:03 lexer

@ankitaj224 you should have an invite to demo repo now. https://github.com/lexer/firebaseInstallations-crash-demo

lexer avatar Mar 11 '20 01:03 lexer

@lexer : Thanks for doing this! Please also invite @ChaoqunCHEN and myself.

andirayo avatar Mar 11 '20 01:03 andirayo

done :)

lexer avatar Mar 11 '20 05:03 lexer

@lexer Is the demo repo missing a commit? I tried adding the changes you mentioned in https://github.com/firebase/firebase-android-sdk/issues/1325#issuecomment-597293291 . I ran into other strict mode violations than the one you raised. I would highly appreciate if you can take another look at the demo repo. Thanks.

ankitaj224 avatar Mar 11 '20 17:03 ankitaj224

@ankitaj224 demo repo should be up to date. Ran on Pixel 3 emulator.

I just re ran the app and get following crash that matches the previous one I mentioned:

D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286)
        at java.util.zip.Inflater.finalize(Inflater.java:407)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276)
        at java.lang.Daemons$Daemon.run(Daemons.java:137)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:237)
        at java.util.zip.Inflater.<init>(Inflater.java:122)
        at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:64)
        at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.createFirebaseInstallation(com.google.firebase:firebase-installations@@16.0.0:154)
        at com.google.firebase.installations.FirebaseInstallations.registerFidWithServer(com.google.firebase:firebase-installations@@16.0.0:411)
        at com.google.firebase.installations.FirebaseInstallations.doRegistrationInternal(com.google.firebase:firebase-installations@@16.0.0:316)
        at com.google.firebase.installations.FirebaseInstallations.doGetId(com.google.firebase:firebase-installations@@16.0.0:280)
        at com.google.firebase.installations.FirebaseInstallations.access$lambda$0(Unknown Source:0)
        at com.google.firebase.installations.FirebaseInstallations$$Lambda$1.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919) 
W/System.err: StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
I/Process: Sending signal. PID: 1990 SIG: 9

lexer avatar Mar 11 '20 19:03 lexer

@ankitaj224

I re ran it on Google pixel 4 device and get another error you might be talking about. Previous test run was on Pixel 3 emulator:

D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286)
        at java.util.zip.Inflater.finalize(Inflater.java:407)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276)
        at java.lang.Daemons$Daemon.run(Daemons.java:137)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:237)
        at java.util.zip.Inflater.<init>(Inflater.java:122)
        at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:64)
        at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHttpClient.fetch(com.google.firebase:firebase-config@@19.0.4:186)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackend(com.google.firebase:firebase-config@@19.0.4:278)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackendAndCacheResponse(com.google.firebase:firebase-config@@19.0.4:251)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchIfCacheExpiredAndNotThrottled(com.google.firebase:firebase-config@@19.0.4:191)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.lambda$fetch$0(com.google.firebase:firebase-config@@19.0.4:160)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler$$Lambda$1.then(Unknown Source:4)
        at com.google.android.gms.tasks.zzf.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919) 
W/System.err: StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
I/Process: Sending signal. PID: 4829 SIG: 9

lexer avatar Mar 11 '20 20:03 lexer

While stack traces are different I think it's a similar issue caused by not closing http stream.

lexer avatar Mar 11 '20 20:03 lexer

Since both jobs are running in parallel it is just a matter of what will crash first.

lexer avatar Mar 11 '20 20:03 lexer

@lexer repo is still not updated. Can you please recheck the push once? Fix for this reported issue seems like closing the input stream

ankitaj224 avatar Mar 11 '20 20:03 ankitaj224

@ankitaj224 i did not update the repo. I think I misunderstood your ask.

Do you want me to increase firebase version?

lexer avatar Mar 11 '20 20:03 lexer

@lexer https://github.com/lexer/firebaseInstallations-crash-demo/blob/master/app/build.gradle

Repo is just an empty app with no strict violations enabled and firebase dependencies. Is that intended?

ankitaj224 avatar Mar 11 '20 20:03 ankitaj224

There are two separate jobs in firebase sdk that violate close stream strict mode check:

2020-03-12 07:28:17.796 2694-2711/com.squareup.cash.beta.debug D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286)
        at java.util.zip.Inflater.finalize(Inflater.java:407)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276)
        at java.lang.Daemons$Daemon.run(Daemons.java:137)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:237)
        at java.util.zip.Inflater.<init>(Inflater.java:122)
        at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:64)
        at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.google.firebase.installations.remote.FirebaseInstallationServiceClient.createFirebaseInstallation(com.google.firebase:firebase-installations@@16.0.0:154)
        at com.google.firebase.installations.FirebaseInstallations.registerFidWithServer(com.google.firebase:firebase-installations@@16.0.0:411)
        at com.google.firebase.installations.FirebaseInstallations.doRegistrationInternal(com.google.firebase:firebase-installations@@16.0.0:316)
        at com.google.firebase.installations.FirebaseInstallations.doGetId(com.google.firebase:firebase-installations@@16.0.0:280)
        at com.google.firebase.installations.FirebaseInstallations.access$lambda$0(Unknown Source:0)
        at com.google.firebase.installations.FirebaseInstallations$$Lambda$1.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919) 
2020-03-12 07:28:17.796 2694-2711/com.squareup.cash.beta.debug D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1877)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:286)
        at java.util.zip.Inflater.finalize(Inflater.java:407)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:289)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:276)
        at java.lang.Daemons$Daemon.run(Daemons.java:137)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:237)
        at java.util.zip.Inflater.<init>(Inflater.java:122)
        at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:64)
        at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:484)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:661)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:475)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHttpClient.fetch(com.google.firebase:firebase-config@@19.0.4:186)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackend(com.google.firebase:firebase-config@@19.0.4:278)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackendAndCacheResponse(com.google.firebase:firebase-config@@19.0.4:251)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchIfCacheExpiredAndNotThrottled(com.google.firebase:firebase-config@@19.0.4:191)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.lambda$fetch$0(com.google.firebase:firebase-config@@19.0.4:160)
        at com.google.firebase.remoteconfig.internal.ConfigFetchHandler$$Lambda$1.then(Unknown Source:4)
        at com.google.android.gms.tasks.zzf.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919) 

lexer avatar Mar 11 '20 20:03 lexer