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

Firebase onAuthStateChanged hangs on startup #8478

Closed
GaurangTandon opened this issue Sep 4, 2024 · 2 comments
Closed

Firebase onAuthStateChanged hangs on startup #8478

GaurangTandon opened this issue Sep 4, 2024 · 2 comments

Comments

@GaurangTandon
Copy link

Operating System

Windows

Environment (if applicable)

Chrome 128

Firebase SDK Version

10.11.0

Firebase SDK Product(s)

Auth

Project Tooling

MV3 Chrome extension service worker built using webpack

Detailed Problem Description

Problem

onAuthStateChanged() hangs on extension service worker startup and then remains hanged until the extension service worker is restarted (either by restarting the browser or by manually reloading the extension). Note that we are using indexeddb persistence so we expect the Firebase instance to get logged in automatically whenever the service worker restarts.

This issue happens more often when the user logs into computer in the morning of a workday. In that case, the user's Firebase ID token from yesterday has expired. Based on that, and based on the logs given below, we suspect the hang is when Firebase is regenerating the ID token. We are not sure why that is, maybe due to network conditions? Our hypothesis could be wrong though.

Logging details

We added logging into the underlying @firebase/auth package using npx patch-package, and got the hang on these two cases:

Case 1:

tbLog('IWP: creating'); // <-- this line got printed
this.persistenceManager = await PersistenceUserManager.create(this, persistenceHierarchy); // <-- hanged here
tbLog('IWP: created'); // <-- this line never got printed

Case 2:

/// in _initializeWithPersistence
tbLog('IWP: iniatilizing current user'); // <-- this line got printed
await this.initializeCurrentUser(popupRedirectResolver);
tbLog('IWP: finished current user'); // <-- this line never got printed

/// in initializeCurrentUser
if (futureCurrentUser) {
  tbLog('ICU: c1'); // <-- this line got printed
  return this.reloadAndSetCurrentUserOrClear(futureCurrentUser); // <-- hanged here
}

Questions:

Is there an easy way - for our code outside the Firebase package - to restart the Firebase app initialization in case it hangs like this? For example: is there a restartApp equivalent to initializeApp. We would wait for a fix in the official package, but we really need a temporary workaround as this issue is severely impacting the user experience

PS: Let us know if you need additional logging inside the Firebase Auth package, and what exactly. It can take us a few days to get the data as this happens rarely and only for some users. So we would still need the workaround.

Steps and code to reproduce issue

We have roughly the following setup:

import { getAuth, onAuthStateChanged, } from 'firebase/auth/web-extension';

const firebaseApp = initializeApp(firebaseConfig);
const firestore = initializeFirestore(firebaseApp, {
  experimentalForceLongPolling: true
});
onAuthStateChanged(getAuth(firebaseApp), (user) => {
  console.log(user);
  if (user) {
    enableIndexedDbPersistence(firestore, { forceOwnership: true });
  }
}});

Note that we are using the web-extension entrypoint into firebase/auth. Note that we initialize persistence after auth has enabled. I'm not sure if it should be the other way around, I couldn't find any official docs around it, or if that matters at all.

I have attached the patch file generated using patch-package.
@firebase+auth+1.7.1.patch

@GaurangTandon GaurangTandon added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Sep 4, 2024
@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.

@jbalidiong jbalidiong added api: auth needs-attention and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Sep 4, 2024
@GaurangTandon
Copy link
Author

I have opened two separate issues now because these are two separate blocks of code in the project. Sorry about the trouble.

#8482
#8481

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

No branches or pull requests

3 participants