Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirebaseApp.UserUnlockReceiver.unregister crashes #5775

Open
consp1racy opened this issue Mar 12, 2024 · 4 comments · May be fixed by #5776
Open

FirebaseApp.UserUnlockReceiver.unregister crashes #5775

consp1racy opened this issue Mar 12, 2024 · 4 comments · May be fixed by #5776

Comments

@consp1racy
Copy link

  • 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:

public void onReceive(Context context, Intent intent) {
// API initialization is idempotent.
synchronized (LOCK) {
for (FirebaseApp app : INSTANCES.values()) {
app.initializeAllApis();
}
}
unregister();
}
public void unregister() {
applicationContext.unregisterReceiver(this);
}

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.
  }
}
@google-oss-bot
Copy link
Contributor

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

@consp1racy
Copy link
Author

consp1racy commented Mar 12, 2024

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 ...

@lehcar09
Copy link
Contributor

lehcar09 commented Apr 8, 2024

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.

@rlazo
Copy link
Collaborator

rlazo commented Apr 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants