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

FirebaseApp.UserUnlockReceiver.unregister crashes

Open consp1racy opened this issue 2 years ago • 4 comments

  • Firebase Component: Common
  • Component version: 20.4.2

Steps to reproduce:

Crashlytics notified us about a crash in FirebaseApp.UserUnlockReceiver.unregister. The method is only called from FirebaseApp.UserUnlockReceiver.onReceive which in turn only responds to android.intent.action.USER_UNLOCKED. I don't know how many times the system sends the broadcast but it apparently isn't just once.

Relevant Code:

https://github.com/firebase/firebase-android-sdk/blob/b77c2188e5ec7c53c2a955fc4663b301e6c98c83/firebase-common/src/main/java/com/google/firebase/FirebaseApp.java#L668-L680

FirebaseApp initialization may be idempotent but Context.unregisterReceiver isn't. The method crashes if the receiver isn't registered.

Suggested solution

public void unregister() {
  try {
    applicationContext.unregisterReceiver(this);
  } catch (IllegalArgumentException ignore) {
    // The receiver isn't registered.
  }
}

consp1racy avatar Mar 12 '24 11:03 consp1racy

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 12 '24 11:03 google-oss-bot

Workaround

val context = object : ContextWrapper(this) {
    override fun getApplicationContext(): ContextWrapper {
        return this
    }

    override fun unregisterReceiver(receiver: BroadcastReceiver?) {
        try {
            super.unregisterReceiver(receiver)
        } catch (_:IllegalArgumentException) {
        }
    }
}
FirebaseApp.initializeApp(context)

EDIT: Maybe it won't be that easy. I got this log

E/FirebaseSessions: Failed to register lifecycle callbacks, unexpected context class ...

consp1racy avatar Mar 12 '24 12:03 consp1racy

Hi @consp1racy, thank you for reaching out. We appreciate you investigating the issue and providing the fix. I'll inform our engineers about this with your pull request.

lehcar09 avatar Apr 08 '24 11:04 lehcar09

Hi @consp1racy thanks for reaching out, and providing a fix! I'll review the code and run the tests required to include it in the codebase.

rlazo avatar Apr 09 '24 17:04 rlazo