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

FIRESTORE (8.2.1) INTERNAL ASSERTION FAILED: Unexpected state #4451

Open
Benny739 opened this issue Feb 10, 2021 · 60 comments
Open

FIRESTORE (8.2.1) INTERNAL ASSERTION FAILED: Unexpected state #4451

Benny739 opened this issue Feb 10, 2021 · 60 comments

Comments

@Benny739
Copy link

We're seeing this error a lot in sentry atm.
I can not reproduce this or say where it comes from or what it consequences are.
I guess it is related to offline support.

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

@wu-hui
Copy link
Contributor

wu-hui commented Feb 12, 2021

Hi there,

Our SDK strips error messages (for sizes), and the error message you provide is the stripped one. This is not very helpful for debugging unfortunately.

Given that you cannot reproduce this yourself, the only thing we can try to to roll some of your users to a SDK without the stripping, and see if we can get some better error messages.

If I provide you instructions to get a SDK without stripping, are you willing to roll this out to a portion of your users?

Thanks

@Benny739
Copy link
Author

Hi,

thanks for your help. Yes we're willing to try it out. What are the risks when using unstripped error messages?

@bigjimhere
Copy link

bigjimhere commented Feb 14, 2021

This error started appearing in our Sentry logs some time around version 7.20.0, with offline persistence disabled. The error occurred sporadically when firebase.firestore().disableNetwork() was called seconds after app startup. Removing firebase.firestore().disableNetwork() from our code resolved the issue.

My guess is that our was app was occasionally calling disableNetwork() while Firestore was still initialising. This may have been interrupting Firebase/Firestore authentication in some way, perhaps by cutting off the connection before authentication could complete. Read and write operations consequently seemed to fail, perhaps due to an "unexpected auth state" (see below).

If you are not using disableNetwork() in your app perhaps Firestore initialisation/authentication is being interrupted in some other way? https://stackoverflow.com/questions/63319638/firestore-internal-assertion-failed-unexpected-state-when-unit-testing-with-j

Our app initialises multiple Firebase instances on startup. Each instance immediately starts reading a fairly large amount of data using Firestore onSnapshot listeners. We were seeing the following errors in Sentry, all of which were resolved when we stopped using disableNetwork():

"FIRESTORE (8.2.6) INTERNAL ASSERTION FAILED: Unexpected state"

"FirebaseError: missing stream token" (less frequently)

"TypeError: undefined is not an object (evaluating 'e.batchId')" (very rarely)

"FirebaseError: the first request in a stream cannot contain writes" (very rarely)

The "Unexpected state" error seemed to cause both reads and writes to fail ("hang") without a promise rejection. We were able to reproduce this at the time, but I'd need to do more testing to confirm. Prior to 7.20.0 we had been able to reproduce hanging writes, but in earlier versions the writes failed silently without the "Unexpected state" error. Offline persistence was enabled for those earlier tests. If the "Unexpected state" error was the cause in those earlier cases it is possible that the writes failed silently because the error messages above weren't added to the SDK until some time around 7.20.0. For example I noticed the following in the release notes for 7.23.0- "The SDK now includes more information in the error message for failed IndexedDB transactions."

The "missing stream token" error caused reads to fail but I can't confirm whether or not writes were affected. As a side note we would see onSnapshot listeners failing with the following error message around the time that this error was logged in Sentry: "FirebaseError: [code=already-exists]: Target ID already exists: 6". This error also disappeared when we stopped using disableNetwork().

I'm not sure what effect the last two errors above had on the application.

I've attached a stack trace for "FIRESTORE (8.2.1) INTERNAL ASSERTION FAILED: Unexpected state"

Firestore Unexpected State Error.txt

UPDATE:

It looks like the "Unexpected state" error was thrown when our app called disableNetwork() on multiple Firestore instances, waited a few seconds and then called enableNetwork() on those same instances. This was likely happening while Firestore was initialising.

@wu-hui
Copy link
Contributor

wu-hui commented Feb 16, 2021

@bigjimhere Are you describing the same project as @Benny739 here, or seeing similar issues with your own project?

Either way, thanks for the information. I will try to call disableNetwork with multiple firebase instance myself.

@Benny739 I will provide a SDK with all error messages enabled, hopefully soon.

@bigjimhere
Copy link

@wu-hui I'm referring to issues with my own project.

@mlahp7
Copy link

mlahp7 commented Feb 16, 2021

We too got a ton of errors in our Sentry that begin with FIRESTORE (8.2.4) INTERNAL ASSERTION FAILED: Unexpected state. It seems to have started happening for us yesterday.
@bigjimhere @Benny739 is the error coming from just one user or multiple?

@mcarriere
Copy link

We're experiencing this issue with FIRESTORE (8.2.4) INTERNAL ASSERTION FAILED but we're not calling disableNetwork anywhere in our code. It happened over 43k times from a single browser today. Luckily it was an internal user so we could tell them to refresh the page but it's not really something we want to tell our clients.

@bigjimhere
Copy link

@mlahp7 In our case multiple users were affected. The issue occurred on a wide range of devices and browsers (desktop and mobile). Looking at the Sentry reports I suspect that a weak internet connection may have been involved. The user that experienced this issue by far the most was reportedly accessing our app through a small business VPN. Others were using our hybrid mobile app in areas where connection is known to be unreliable. We tried experimentalForceLongPolling and autoDetectLongPolling but these settings did not solve the issue. For us only removing our implementation of disableNetwork and enableNetwork solved the issue.

@bigjimhere
Copy link

The issue could be caused by losing or disabling the connection while firebase.auth().currentUser.getIdTokenResult or firebase.auth().currentUser.getIdToken are in progress. I haven't done any testing so this is only speculative. I noticed that our code was calling both of these functions around the same time as disableNetwork.

@wu-hui
Copy link
Contributor

wu-hui commented Feb 17, 2021

To everyone who are reporting issues, please include full stacktrace, this will help me find exactly where the error happens.

@Benny739 in particular:

I realized you don't need to try a custom build of the SDK, if you can provide below information full stacktrace of the error report, which version you use and which build (CDN or NPM).

If you are using NPM build and also does obfuscation on top of Firebase, you probably need to first remove your obfuscation using something like https://github.com/mifi/stacktracify

The stacktrace we can parse should have traces like node_modules/@firebase/firestore/dist/index.esm.js:5782:31 for example.

To others:

Please note the error message here is very generic, and we cannot help if we cannot point them to exactly where the error happens, your issue may or may not be related to what the original issue is about. Please try to provide full stack trace as well.

@bigjimhere
Copy link

I've now seen this error in a different context to the one I mentioned above. The error occurred on 8.2.7 on iOS 14.4 with offline persistence enabled. The error was preceded by another error: "INTERNAL UNHANDLED ERROR: e is not a function. (In 'e()', 'e' is undefined)"

Stack trace for both errors:

Unexpected State Error Stack Trace.txt

@bigjimhere
Copy link

We just saw the Unexpected state error multiple times in a single session on OSX 10.15.4 running Safari 13.1.

We're monitoring online presence using '.info/connected' (Firebase RTDB). Both "Unexpected state" and "e is not a function. (In 'e()', 'e' is undefined)" occurred multiple times in this session when RTDB went offline:

Firebase Error


Firebase Error 2

In my first post here I mentioned that the Unexpected state error was occurring when we called disableNetwork and enableNetwork with offline persistence disabled- it's worth noting that we were calling those methods whenever RTDB went offline/online.

The "Unexpected state" error was also thrown multiple times in this session when trying to set up an onSnapshot listener with offline persistence enabled.

The error "Connection to Indexed Database server lost. Refresh the page to try again" also occurred early on in this session. Could be related to https://bugs.webkit.org/show_bug.cgi?id=197050

@wu-hui
Copy link
Contributor

wu-hui commented Mar 4, 2021

Thanks for the stack trace.

Your first error 'e' is undefined seems to be a IDB transaction failure error. The second one is a webchannel failure, which might be related to the network conditions.

The bug you posted could certainly be the cause of the first error though. Do you only observe it from safari?

Pasting the converted stack trace for further investigation:

[2021-02-18T07:08:38.970Z]  @firebase/firestore: Firestore (8.2.7): INTERNAL UNHANDLED ERROR:  e is not a function. (In 'e()', 'e' is undefined)
Xr@../firestore/dist/prebuilt.js:5313:56
Xr@../../node_modules/tslib/tslib.es6.js:100:27
Xr@../../node_modules/tslib/tslib.es6.js:72:65

Xr@https://www.gstatic.com/firebasejs/8.2.7/firebase.js in Qv@ionic: at line 1:473665
../firestore/dist/prebuilt.js:15793:19
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/events/eventtarget.js:351:27
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/events/eventtarget.js:481:11
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/labs/net/webchannel/webchannelbasetransport.js:368:2
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/labs/net/webchannel/webchannelbase.js:2219:49
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/labs/net/webchannel/channelrequest.js:822:6
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/labs/net/webchannel/channelrequest.js:580:6
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/events/eventtarget.js:351:27
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/events/eventtarget.js:481:11
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/net/xhrio.js:884:4
../webchannel-wrapper/dist/temp/node_modules/google-closure-library/closure/goog/net/xhrio.js:841:2

@bigjimhere
Copy link

@wu-hui it's easiest to summarise:

Case 1: start app with offline persistence disabled > call disableNetwork across multiple instances > call enableNetwork across multiple instances > Unexpected state error. Seen on many devices and browsers (iOS, Android, Edge on Windows 10, Chrome on Mac OSX etc). We can reproduce the error in this case.

Case 2: offline persistence enabled. Unexpected state error seen in conjunction with 'e' is undefined and Connection to Indexed Database server lost. Only seen on iOS 14.4 and Safari 13.1 on Mac OSX. We can't reproduce the error in this case.

It seems like poor network conditions or simulated poor network conditions (disableNetwork) are common between the two cases. That said, in case 2 background/foreground activity may have also played a role. This is evidence for the WebKit bug being involved (https://bugs.webkit.org/show_bug.cgi?id=197050).

Both cases caused commit operations to fail (hang) without a promise rejection.

@schmidt-sebastian
Copy link
Contributor

FWIW, the corresponding source code is:

 async runTransaction(t, e, n, s) {
        const i = "readonly" === e;
        let r = 0;
        for (;;) {
            ++r;
            try {
                this.db = await this.Hn(t);
                const e = Es.open(this.db, t, i ? "readonly" : "readwrite", n), r = s(e).catch((t => (
                e.abort(t), ws.reject(t)))).On();
                return r.catch((() => {})), 
                await e.qn, r;
            } catch (t) {
                const e = "FirebaseError" !== t.name && r < 3;
                if (R("SimpleDb", "Transaction failed with error:", t.message, "Retrying:", e), 
                this.close(), !e) return Promise.reject(t);  // This is Xr@../firestore/dist/prebuilt.js:5313:56
            }
        }
  }

This translates to

. e is the boolean retryable.

@bigjimhere
Copy link

More evidence for the Webkit bug being a cause of this error, this time on iOS 12:

Screen Shot 2021-03-18 at 12 52 21 pm

UnknownError: Connection to Indexed Database server lost. occurred shortly after.

@bigjimhere
Copy link

We just saw 'e' is not a function immediately followed by Unexpected state on Chrome 89.0.4389 on Mac OSX 10.15.7, with offline persistence enabled. This is the first time we've seen an 'e'-related bug outside of iOS/Safari. The user was resuming a long-running session so it's probably safe to assume that the tab was in the background immediately before the error was thrown.

This error was also being thrown regularly around the same time as the above errors:

Firestore (8.2.7): Connection WebChannel transport errored: [object Object]

@marcoancona
Copy link

We also saw this error in our Sentry report. This so far happened for one user only, triggering ~1.8K of such errors in a few seconds.

@zeevl
Copy link

zeevl commented May 15, 2021

We're seeing this as well, version 8.4.1, electron app with persistence enabled:

[92m(errors) Error: FIRESTORE (8.4.1) INTERNAL ASSERTION FAILED: Unexpected state
    at Mr (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:46421)
    at sp.Tc (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:268833)
    at sp.enqueue (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:267636)
    at sp.enqueueAndForget (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:267321)
    at file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:300518
    at ag (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:300724)
    at og (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:299086)
    at Rg.update (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/vendor/firebase/firebase-firestore.js:1:312801)
    at R (file:///Applications/ScribeWare%205.app/Contents/Resources/app.asar/dist/static/js/main.bf484a7e.chunk.js:2:858748)

@zeevl
Copy link

zeevl commented Jun 10, 2021

@wu-hui We are seeing this with an increasing number of users now, and it's causing data loss and requires an application restart.

As such, it's becoming a very severe and urgent bug for us. I'm happy to help diagnose however I can, please let me know?

@bigjimhere
Copy link

Same here. We activated offline persistence around a month ago on a cross-platform app with around 200 daily users. You can see the Sentry logs here, the issue is happening fairly frequently:

Firebase Errors

An application restart is not always a great solution. Sometimes the error occurs right as the user is saving data, and that data is lost when the app resets without the changes having been saved into offline persistence.

I repeat @zeevl 's sentiment- let us know if we can help to diagnose further.

@wu-hui
Copy link
Contributor

wu-hui commented Jun 16, 2021

Hi @zeevl

The stack trace you posted here is basically saying the SDK's internal work queue is failed already when trying to schedule work. Are there other errors in your report? The error message will always be "Unexpected state" but they will have different stack trace, so it is not easy to find all of them, but they can be helpful to identify what went wrong in the first place.

@bigjimhere

Thanks for your analysis! Yeah, there could be multiple issues going on in this thread, our stripped error message does not help here.

For your case 1, you said you could reproduce this. Can you provide a mini reproduction for me to try out?

You are right that the e is not a function error might be related to underlying IndexedDB implementation, and it is known to be problematic for some browsers.


Below is the actual stack trace from zeevl's report, pasting here for future reference:

[92m(errors) Error: FIRESTORE (8.4.1) INTERNAL ASSERTION FAILED: Unexpected state
    at  (../src/util/assert.ts:40:8)
    at fail (../src/util/async_queue_impl.ts:220:6)
    at __PRIVATE_verifyNotFailed (../src/util/async_queue_impl.ts:114:9)
    at enqueue (../src/util/async_queue_impl.ts:89:9)
    at enqueueAndForget (../src/core/firestore_client.ts:438:20)
    at ensureFirestoreConfigured (../src/exp/reference_impl.ts:771:17)
    at executeWrite (../src/exp/reference_impl.ts:366:9)
    at updateDoc (../src/api/database.ts:730:15)

@augustmuir
Copy link

I experienced the same error message on 9.6.6 with NextJS. I believe this error message could be presented due to a range of errors - as I see 100+ Stackoverflow questions with this error message.

After lots of debugging I realized I accidently used SQL capitalization:

.orderBy('time', 'ASC') = "INTERNAL ASSERTION FAILED: Unexpected state"
.orderBy('time', 'asc') = No Errors!

This was a pain to debug, and my mistake was so small. Maybe better error reporting is needed in cases like this? When you get then Google this error message it easily leads you down a path of debugging things completely irrelevant to the real error.

@schmidt-sebastian
Copy link
Contributor

We have gotten a lot of feedback about our SDK size, which is one of the reason we removed API input validation from our surface. We only want to show API usage errors that TypeScript can't tell you about. @augustkimo if you use TypeScript it would help to know whether you saw this error or not.

We could also add an additional build target to our release that does not minify error messages away (if there is interest)/

@AshleyMedway
Copy link

We could also add an additional build target to our release that does not minify error messages away (if there is interest)/

Yes please! We are using an ionic application and a larger binary is acceptable for additional information.

@augustmuir
Copy link

We have gotten a lot of feedback about our SDK size, which is one of the reason we removed API input validation from our surface. We only want to show API usage errors that TypeScript can't tell you about. @augustkimo if you use TypeScript it would help to know whether you saw this error or not.

We could also add an additional build target to our release that does not minify error messages away (if there is interest)/

Good to know that Typescript would've caught this, I was using Javascript when I got the error.

It would have saved me from some confusion if the error was a generic Firebase API Error: Error messages stripped to reduce package size instead of the nondescript message. Not a huge deal to me personally, after tons of Firebase Javascript usage I only ran into this error once.

@Animus-Surge
Copy link

I'm having similar problems to the ones in this thread in version 9.6.7. I get several INTERNAL ASSERTION FAILED: Unexpected state errors bound to this line in a script: const news = await getDocs(q); where q is a query: const q = query(newsData, orderBy("date", "dsc"), limit(2)); I have not encountered this error before, and I don't know what I was doing wrong.

@augustmuir
Copy link

augustmuir commented Feb 24, 2022

@Animus-Surge you made a small syntax mistake on the query. It should be orderBy("date", "desc") not orderBy("date", "dsc"). See my original comment on this issue.

@Animus-Surge
Copy link

@Animus-Surge you made a small syntax mistake on the query. It should be orderBy("date", "desc") not orderBy("date", "dsc"). See my original comment on this issue.

Oops, I guess I have. Sorry about that

@AshleyMedway
Copy link

We could also add an additional build target to our release that does not minify error messages away (if there is interest)/

@schmidt-sebastian Any update on this? We have upgraded through firebase V7 - V9 and still getting seemingly random INTERNAL ASSERTION FAILED: Unexpected state but only from iOS devices. I believe the issue is related to indexedDB.

Currently trying to produce 9.6.8 debug build to get more information

@schmidt-sebastian
Copy link
Contributor

I talked to our JS Build team earlier this week. We will include a debug build with in either the next or the next next release.

@AshleyMedway
Copy link

AshleyMedway commented Mar 14, 2022

With the debug build we see this error:

@firebase/firestore: Firestore (9.6.8): FIRESTORE (9.6.8) INTERNAL ASSERTION FAILED: AsyncQueue is already failed: Attempt to iterate a cursor that doesn't exist

Then looking back through the log this appears to be the original error:

@firebase/firestore: Firestore (9.6.8): AsyncQueue Initialization of query 'Query(target=Target(Databases/3f2ff7b5-70c1-4cff-81d5-222b0174ef98/Employees, orderBy: [__name__ (asc)]); limitType=F)' failed: UnknownError: Attempt to iterate a cursor that doesn't exist

Also

@firebase/firestore: Firestore (9.6.8): INTERNAL UNHANDLED ERROR: Attempt to iterate a cursor that doesn't exist

It might also be worth noting this issue only occurs on iOS devices.

Additionally, when we detect any error with a firestore query we terminate, initialise and rerun the query, but that does not seem to help

@AshleyMedway
Copy link

Any update on the above errors? This is still an on going issues for our iOS users. Seems like the connection to the IndexedDB will randomly fail and the only way to resolve is to quit and reopen the application.

@cherylEnkidu
Copy link
Contributor

Hi @AshleyMedway,

  1. If you are running in web browser, Safari IndexedDb implementation has been having issues for years. At this point we don’t have a solution. If your app do not need offline or multi-tab support, you can try is to use memory persistence instead.

  2. If you build the iOS App on React Native, you can try turn on long polling :

const db = initializeFirestore(app, {
  experimentalAutoDetectLongPolling: true
})

@joker7391
Copy link

I have the same error

@cherylEnkidu
Copy link
Contributor

Hi @joker7391,

We don't have recent updates on solving this problem. Please try out the suggestions in #4451 (comment).

@AshleyMedway
Copy link

If you are running on ionic capacitor we’ve written this plugin to access the native libraries. https://github.com/ProteanSoftware/capacitor-firestore

@arnotixe
Copy link

arnotixe commented Jan 12, 2023

1. If you are running in web browser, Safari IndexedDb implementation has been having issues for years.

I also got this on Firefox just now; it did not help restarting the browser either. Chrome works fine.

This is an angular app that works well otherwise, but on one specific page it barfs -- seems to me it can be related to a certain query that somehow fails. Will update if I find more clues.

AsyncQueue Initialization of query 'Query(target=Target(userRequest, filters: [uid == gn1rUTxJyFfk1d___SG0rcD3, site == Ctm9l6ch1____V4hTBV, processed == false], orderBy: [name (asc)]); limitType=F)' failed: UnknownError: The operation failed for reasons unrelated to the database itself and not covered by any other error code.

@kaktuspalme
Copy link

kaktuspalme commented May 26, 2023

Is there a reason I see those messages and write attempts every couple of seconds?:
[2023-05-26T05:15:52.772Z] @firebase/firestore: Firestore (9.22.0): SimpleDb PUT owner owner {"ownerId":"someid","allowTabSynchronization":false,"leaseTimestampMs":1685078152772} [2023-05-26T05:16:41.058Z] @firebase/firestore: Firestore (9.22.0): IndexedDbPersistence Client 'someid' is not zombied in LocalStorage

I tried to circumvent the problem by using disableNetwork and preventing writes from my app while the App is in the background on Safari. But I believe because those periodic writes to the IndexedDb happen it still breaks.

Edit:
To give a little bit more context. I have a capacitor app which uses bluetooth connection also while the app is in background. Whenever I locked the iPad after some seconds I got the INTERNAL ASSERTION FAILED error.

I just noticed that if I stop all bluetooth notifications it looks like the app gets paused by ios when the iPad is locked. And therefore no read and writes to the indexeddb anymore. So far I couldn't trigger the error anymore if I do it like that. I would still be glad if there was another method like pausing those firestore background tasks so that I could still listen to bluetooth notifications.

@Zeusmist
Copy link

Hi @wu-hui @schmidt-sebastian @cherylEnkidu
I get this error when I access a feature that uses Firestore listeners on multiple docs. It happens both when I'm using Firestore emulator and also in production.
Mine is 9.18.0

@pcl
Copy link

pcl commented Dec 6, 2023

If you are running on ionic capacitor we’ve written this plugin to access the native libraries. https://github.com/ProteanSoftware/capacitor-firestore

Cool!

@AshleyMedway : why did you go the route of a native firestore implementation instead of just a native Persistence implementation to replace indexedDBLocalPersistence? Seems like that would perhaps be a narrower API to support. And are there any caveats I should know about when working with capacitor-firestore?

@misael1976
Copy link

well in my case i had installed firebase with npm i deleted the cloned repo, when later i cloned a new one i yarn install but got the error above mentioned it was fixed with npm install

@luke-rogers
Copy link

@kaktuspalme I've had more thoughts and added a comment in a new issue I raised.

Have you got any more information about this issue?

@kaktuspalme
Copy link

@luke-rogers No I unfortunately don't have more information. I ended up disabling Firestore offline and implemented my own offline and sync to firestore implementation.

@madmacc
Copy link

madmacc commented Oct 6, 2024

Also have this issue.
Here are two related threads:
#8250
#8068

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