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

Snapshot stops working after Capacitor app being minimized for 5m #8068

Open
vojto opened this issue Mar 14, 2024 · 10 comments
Open

Snapshot stops working after Capacitor app being minimized for 5m #8068

vojto opened this issue Mar 14, 2024 · 10 comments

Comments

@vojto
Copy link

vojto commented Mar 14, 2024

Operating System

iOS 17.4

Browser Version

Safari 604.1

Firebase SDK Version

10.9.0, 10.8.1

Firebase SDK Product:

Firestore

Describe your project's tooling

React, Next.js, Capacitor

Describe the problem

Over the months, we noticed some errors were always logged in Sentry, such as:

  1. UnknownError: Attempt to get a record from database without an in-progress transaction
  2. UnknownError: Attempt to get all index records from database without an in-progress transaction
  3. UnknownError: Connection to Indexed Database server lost. Refresh the page to try again
  4. FIRESTORE (10.7.0) INTERNAL ASSERTION FAILED: Unexpected state
  5. Non-Error promise rejection captured with value: null

We always saw them, but never received any bug reports about broken sync, so we just shrugged them off as recoverable.

iOS 17.4

Since latest iOS, we noticed that sync stopped working sometimes. After focusing on this problem, it turned out it's likely to fail if app is running in the background, and is brought to foreground.

I want to stress that we are handling onError in our snapshot listeners, and restart the subscriptions in such case. However, the onError callback was never getting called, it would just stop working.

Coming back to app running in the background would often look like this:

So it shows the error, and snapshots are not working anymore.

Previous versions of iOS

To be clear, the errors above were also logged pre-iOS 17.4. But we didn't receive reports of broken sync before, and the errors were also less frequent.

For example, the "Connection to Indexed Database server lost" error is now most common on 17.4 after only few days that it's been out:

Logged errors

Whenever I encountered a failure, it always came with the INTERNAL ASSERTION FAILED error.

Sometimes, I logged other errors mentioned at the top - while app was still in the background.

Logging

I wanted to capture more information, but strangely whenever I set logging to firestore.setLogLevel('debug'), I couldn't repro once. And I tried many times.

After switching back to firestore.setLogLevel('error'), I was able to repro again.

iPhone state

I haven't noticed anything special about the state - things such as battery %, battery saving mode don't seem to have any effect. Also it didn't matter whether I flooded memory with other apps or not. (Obviously I didn't flood the memory enough for app to be terminated.)

Firebase versions

Reproduced on both 10.8.1 and 10.7.0.

Steps and code to reproduce issue

We don't have a repo with clean reproduction, but I want to explain how we can reproduce it in our app.

  1. Run the Capacitor app on a phone
  2. Switch to other apps. Use the device normally, but don't reopen the app
  3. Wait 5 minuts
  4. Make changes on another device
  5. Reopen the app. The changes aren't showing up, and the FIRESTORE (10.7.0) INTERNAL ASSERTION FAILED: Unexpected state is thrown at the time when app is reopened.
@vojto vojto added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Mar 14, 2024
@vojto vojto changed the title Snapshot stops working after Capacitor app being minimized for 5mTitle for the bug Snapshot stops working after Capacitor app being minimized for 5m Mar 14, 2024
@vojto
Copy link
Author

vojto commented Mar 14, 2024

cc @maccman

@jbalidiong jbalidiong added needs-attention and removed new A new issue that hasn't be categoirzed as question, bug or feature request labels Mar 14, 2024
@ehsannas ehsannas self-assigned this Mar 14, 2024
@vojto
Copy link
Author

vojto commented Mar 18, 2024

Tested on 10.9.0, not fixed.

@vojto
Copy link
Author

vojto commented Mar 18, 2024

I can consistently reproduce it by just:

  1. Open the app
  2. Switch to another app, let it run in the background
  3. After ~3m, it'll throw INTERNAL ASSERTION FAILED and sync won't work after that

If I keep the app in the foreground, everything works fine.

Here's what the error looks like from remote Safari inspector:

CleanShot 2024-03-18 at 13 22 30@2x

@ehsannas
Copy link
Contributor

Thanks for reporting @vojto . Can you point me to the repo that I can clone to reproduce?

@vojto
Copy link
Author

vojto commented Mar 19, 2024

@ehsannas Sadly we don't have a repo that reproduces the problem. It happens in our app where we use IndexedDb persistence.

@vojto
Copy link
Author

vojto commented Mar 20, 2024

@ehsannas
Copy link
Contributor

Thanks for the Loom, @vojto. It appears to be an IndexedDB bug. We could try to confirm this by using a different iOS version [perhaps using the iOS simulator?]

@vojto
Copy link
Author

vojto commented Mar 22, 2024

@ehsannas So we've managed to reproduce the problem in a separate project: https://github.com/vojto/trips The description contains steps to reproduce, and also a Loom video showing how to do it.

We also noticed that removing Sentry fixes the problem.

Our Sentry setup looks like this - there's @sentry/nextjs and @sentry/capacitor - so Sentry is actually running as Swift code on the native side, and JS side is only sending events to it.

I want to note that Sentry has an option to use IndexedDb for errors collected while offline, but we are not using this option. So Sentry in our setup doesn't use IndexedDb.

We found out about 3 ways to fix the error:

  1. Remove Sentry entirely
  2. Remove @sentry/capacitor and only use @sentry/nextjs
  3. Disable breadcrumbs (!)

Disabling breadcrumbs

Sentry tracks each fetch request, and each one is being sent to native side:

CleanShot 2024-03-22 at 09 35 26@2x

There's a simple way to disable this, just configure Sentry like this:

import * as Sentry from "@sentry/capacitor";
import * as SentryNext from "@sentry/nextjs";

let dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
Sentry.init(
  {
    dsn,
    beforeBreadcrumb: () => {
      return null;
    },
  },
  SentryNext.init
);

When we do this, the error seems resolved.


My hunch is that this is somehow related to logging. When I tried switching to debug log level, I could not reproduce the error.

It would be great if you could look into why was this happening in the first place. How is it possible that Sentry capturing breadcrumbs causes Firebase to completely fail?

Let us know if we can help figuring out more.

@luke-rogers
Copy link

luke-rogers commented Mar 25, 2024

We also see the same issue with our Capacitor app running Firebase 9.x release.

Seems like the only way forward is to disable the breadcrumbs for now, not ideal but better than complete removal of Sentry.

@luke-rogers
Copy link

@vojto don't suppose you have any further information on this?

We just upgraded to the latest Firebase version (10.10.0) and disabled the breadcrumbs but are still seeing this issue. We are going to try removing @sentry/capacitor next and just use @sentry/angular-ivy.

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

5 participants