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

onAuthStateChanged hangs on startup at PersistenceManager.create step #8482

Open
GaurangTandon opened this issue Sep 5, 2024 · 5 comments
Open

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.

Logging details

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

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

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.

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 5, 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 5, 2024
@DellaBitta
Copy link
Contributor

Hi @GaurangTandon,

Could it be that those operations are throwing errors instead of hanging? Could you try catching any error and logging if one is encountered?

Also, there's a pretty lengthy and informative thread about Firestore and persistence in Web Workers in #7364 that's worth reviewing.

Thanks!

@GaurangTandon
Copy link
Author

Hi @DellaBitta I added more logging last week and got some updated logs today. Updated patch file:
@firebase+auth+1.7.1.patch

Inside _openDatabase function, the logs PC: 3 then IA: 0 then OD: init got printed, but none of the other logs get printed after it. This implies that either:

  1. the promise got created, but the 'success', 'upgradeneeded', or the 'success' events did not fire, or
  2. the indexedDB.open function itself threw an exception.

I read the linked issue, thanks for sharing. For Web Workers, I understand more than one website tab can be open at the same time. However, in Chrome extensions, at most one extension service worker is going to be running at a given time.

@GaurangTandon
Copy link
Author

Update: I don't think alternative 2 is possible. If indexedDB.open threw an exception, then _openDatabase() would reject, and it would be caught in the try...catch of the _isAvailable function, and we would see PC: 4 printed. But I don't see PC: 4 in the logs. OD: init is the last log line.

@DellaBitta
Copy link
Contributor

Hi @GaurangTandon,

Thanks so much for clarifying all of this. We're still working to try to reproduce the problem. In the meantime, could you add another log after the line that creates the IndexedDB request? It'd be helpful to know if that operation is returning:

tbLog('OD: init');
const request = indexedDB.open(DB_NAME, DB_VERSION);
tbLog('OD: request created');

Perhaps even log the request object as well.

Also, I've noticed that the Auth code creates a listener for the success, error, and upgradeneeded events, but not for the blocked event (MDN). Seems like a candidate for what might be going wrong. Any chance you could try registering a listener and logging if its called?

request.addEventListener('blocked', async () => {
  tbLog('OD: blocked!');
}

Thanks!

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

4 participants