firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

FIRESTORE INTERNAL ASSERTION FAILED: Failed to open DB

Open lammertw opened this issue 5 years ago • 52 comments

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 11.4.1
  • Firebase SDK version: 6.21.0
  • Firebase Component: Firestore
  • Component version: 6.21.0
  • Installation method: CocoaPods

[REQUIRED] Step 2: Describe the problem

My app is crashing with a fatal exception due to Firestore.

I know several of these crashes has been reported before but since they are locked I could not comment on them.

So far this issue is only happening in production but it's happening in about 1% of all cases (about 2k crashes so far) so it's a pretty big problem for us now.

Fatal Exception: NSInternalInconsistencyException
FIRESTORE INTERNAL ASSERTION FAILED: Failed to open DB: Internal: Failed to open LevelDB database at /var/mobile/Containers/Data/Application/2211CDC3-4C78-4450-86AE-A1AC24D902BD/Library/Application Support/firestore/__FIRAPP_DEFAULT/eneco-360/main: LevelDB error: IO error: /var/mobile/Containers/Data/Application/2211CDC3-4C78-4450-86AE-A1AC24D902BD/Library/Application Support/firestore/__FIRAPP_DEFAULT/eneco-360/main/LOCK: Operation not permitted (expected created.ok())
Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x1acd22a48 __exceptionPreprocess
1  libobjc.A.dylib                0x1aca49fa4 objc_exception_throw
2  CoreFoundation                 0x1acc24e88 +[_CFXNotificationTokenRegistration keyCallbacks]
3  Foundation                     0x1ad0a9044 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]
4  Eneco                          0x100581828 firebase::firestore::util::ObjcThrowHandler(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 59 (exception_apple.mm:59)
5  Eneco                          0x1005814dc firebase::firestore::util::Throw(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 90 (exception.cc:90)
6  Eneco                          0x1005cc7d0 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 42 (hard_assert.cc:42)
7  Eneco                          0x1005cc894 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 49 (hard_assert.cc:49)
8  Eneco                          0x1005a1a30 firebase::firestore::core::FirestoreClient::Initialize(firebase::firestore::auth::User const&, firebase::firestore::api::Settings const&) + 177 (firestore_client.cc:177)
9  Eneco                          0x100562724 firebase::firestore::util::AsyncQueue::ExecuteBlocking(std::__1::function<void ()> const&) + 957 (atomic:957)
10 Eneco                          0x100582854 firebase::firestore::util::internal::DispatchAsync(NSObject<OS_dispatch_queue>*, std::__1::function<void ()>&&)::$_0::__invoke(void*)
11 libdispatch.dylib              0x1ac9ef184 _dispatch_client_callout
12 libdispatch.dylib              0x1ac9cc710 _dispatch_lane_serial_drain$VARIANT$armv81
13 libdispatch.dylib              0x1ac9cd128 _dispatch_lane_invoke$VARIANT$armv81
14 libdispatch.dylib              0x1ac9d643c _dispatch_workloop_worker_thread
15 libsystem_pthread.dylib        0x1aca3eb88 _pthread_wqthread
16 libsystem_pthread.dylib        0x1aca41760 start_wqthread
image

lammertw avatar Apr 21 '20 06:04 lammertw

Are you setting any file protection policies on the database directory?

morganchen12 avatar Apr 21 '20 17:04 morganchen12

Hi and sorry for your troubles.

What this stack says is when Firestore is trying to open its underlying database it's failing with an "Operation not permitted" error (EPERM).

We've seen this happen before when apps are in the background and the filesystem is locked, most notably in #2846, wherein Firestore's own idle activity could cause the failure while an application was in the background (this has since been fixed).

You may be running into a similar issue, wherein your application is taking some action that's trying to use Firestore while in the background. There are a few ways this can happen:

  • Actually implementing a background task
  • Responding push notification

If you've changed the com.apple.developer.default-data-protection entitlement you'll definitely see this error. Even if you haven't, it's possible to observe it if you try to run before the user has unlocked their device.

wilhuff avatar Apr 21 '20 17:04 wilhuff

Thanks for the answer.

We're not setting or changing the com.apple.developer.default-data-protection entitlement.

We do also use FCM and we also have background fetch enabled so background tasks could be running. However, according to the Crashlytics dashboard only 12% of the crashes occur when the device is in background.

Is there any other info you'd like me to send or anything you'd like me to try on our side?

lammertw avatar Apr 21 '20 19:04 lammertw

In my experience the "background" state reflected in Crashlytics isn't terribly accurate. If you can observe some crashes in the background then it's worth starting down the path of trying to figure that out. If, after fixing that there are still non-background crashes going on that would be a follow-up.

Firestore is incompatible with locked filesystems. In the future we'll make this better by failing cleanly, but that will just push the failure out as an error out to your code--the failure will still be there.

So you have essentially two paths forward:

  • Identify code paths in your system that might try to access Firestore in response to a push notification or background task and avoid making those calls, or
  • Reduce the file protection level for Firestore's backing data (as outlined in https://github.com/firebase/firebase-ios-sdk/issues/2846#issuecomment-484291699) (though note that since #4609, Firestore now stores its data in .applicationSupportDirectory, not .documentsDirectory). This is only valid if your local data is not sensitive.

wilhuff avatar Apr 21 '20 19:04 wilhuff

Since the data in Firestore is public data I'll use the approach of reducing the file protection level for now. I'll report back later to let you know if that helped or not.

lammertw avatar Apr 21 '20 20:04 lammertw

Hey @lammertw. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Apr 28 '20 01:04 google-oss-bot

We just released a new version of our app a couple of days ago that reduces the file protection level for Firestore's backing data. Unfortunately we're still getting the same crash just as much as before.

lammertw avatar Apr 28 '20 06:04 lammertw

I'm experiencing the same crash with a brand new macOS project using the (unofficial) Firebase pods. I first configure Firebase in the application delegate, then fetch a document from Firestore if the current user is set. All of this happens on the main thread and without any modification to the entitlements.

vizaiapp avatar Aug 13 '20 00:08 vizaiapp

Receiving this error too:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Failed to open DB: Internal: Failed to open LevelDB database at /Users/user/Library/Developer/CoreSimulator/Devices/75E41D4A-1A35-4B35-A073-9591F9932BDF/data/Containers/Data/Application/F9074D72-BDD3-4A9F-8AB6-6E77DA2571C3/Library/Application Support/firestore/__FIRAPP_DEFAULT/project-25f32/main: LevelDB error: IO error: lock /Users/user/Library/Developer/CoreSimulator/Devices/75E41D4A-1A35-4B35-A073-9591F9932BDF/data/Containers/Data/Application/F9074D72-BDD3-4A9F-8AB6-6E77DA2571C3/Library/Application Support/firestore/__FIRAPP_DEFAULT/project-25f32/main/LOCK: Resource temporarily unavailable (expected created.ok())'
terminating with uncaught exception of type NSException
CoreSimulator 757.3 - Device: iPhone 12 (75E41D4A-1A35-4B35-A073-9591F9932BDF) - Runtime: iOS 14.4 (18D46) - DeviceType: iPhone 12

It crash on the very beginning, right after the launch screen disappears. Running on the new M1 MacBook Pro, this wasn't shown on my previous MacBook Pro using the Intel chip. I need to clean the build folder, restart the simulator, sometimes even the system needs to reboot, to run the app on the simulator.

elai950 avatar Mar 12 '21 17:03 elai950

Exactly same issue than @elai950, with same iPhone 12 (simulator) iOS 14.4 version and also on M1 Pro. (maybe we should create a new issue 🤔

omerozk avatar Apr 17 '21 11:04 omerozk

@vizaiapp any luck with this crash on macOS?

tysonwil avatar Apr 22 '21 20:04 tysonwil

This issue happens for me when I run the app in SwiftUI Preview, and then try to launch it. Xcode keeps the app's process alive - even after the Preview is no longer visible in Xcode. Having those two processes open at the same time leads to this error.

How to fix:

  1. Open Activity Monitor app
  2. Find process of your app
  3. Kill it

It's annoying to deal with so if anyone from Firebase team can fix it, that'd be great :)

Moriquendi avatar May 13 '21 09:05 Moriquendi

You can using static library. Download firebase firestore at https://firebase.google.com/docs/ios/setup#frameworks

In Podfile add

target '{project_name}' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks! :linkage => :static
end

references: https://firebase.google.com/docs/ios/link-firebase-static-dynamic

lengocgiang avatar May 13 '21 09:05 lengocgiang

@lammertw I have the same issue that you described, you were able to solve it? Thanks

santiotin avatar Jun 29 '21 06:06 santiotin

I have the same issue...

mastercodekw avatar Jun 29 '21 07:06 mastercodekw

Same issue with a swift share-extension. MBP M1 chip. i have never seen this error in my macbook intel chip. to solve the problem.

1- Clean derived data folder: /Users/XXXX/Library/Developer/Xcode/DerivedData 2 - Restart simulator/xcode

bouraine avatar Jul 01 '21 21:07 bouraine

I have the same issue, too...

coffmark avatar Jul 03 '21 06:07 coffmark

Can this happen on real devices? if yes, how do I recover from it without asking users to delete/reinstall the app?

r-a-o avatar Jul 09 '21 09:07 r-a-o

Same on real iOS device...

Livinglist avatar Oct 07 '21 16:10 Livinglist

Same on a real iOS device too.

It's happening ~ 1% of the time to our users. Would love an update!

courtneypresto avatar Oct 09 '21 21:10 courtneypresto

@lammertw I have the same issue that you described, you were able to solve it? Thanks

@santiotin No we're still having the issue

lammertw avatar Oct 11 '21 14:10 lammertw

Also seeing this issue on an M1, but only in our UI testing scheme. No problem for anyone with an Intel chip.

iammike avatar Oct 13 '21 15:10 iammike

I've been running into this issue as well, I am on a M1 Max MacbookPro. I do noticed what @Moriquendi mentioned above. Which is when I run Activity Monitor, I can see the app still running in memory even if Xcode and the Simulator are closed.

jhoanarango avatar Nov 20 '21 16:11 jhoanarango

I am getting the same crash when try to show unity window from iOS both using firebase dependencies. Can anyone please explain the cause and provide solution?

vishnunairvp avatar Nov 23 '21 15:11 vishnunairvp

Having the same issue randomly on my M1 macOS project. Using Swift Package manager. Latest versions. Cleared derived data and reset the cache of the package manager. Still not working.

FIRESTORE INTERNAL ASSERTION FAILED: Failed to open DB: Internal: Failed to open LevelDB
database at /Users/<ME>/Library/Application Support/firestore/__FIRAPP_DEFAULT/<APP_ID>/main: LevelDB error: 
IO error: lock /Users/<ME>/Library/Application Support/firestore/__FIRAPP_DEFAULT/<APP_ID>/main/LOCK: 
Resource temporarily unavailable (expected created.ok())

What helped me was to disable persistence. I started having the issue, when I started implementing Unit Tests. I assume, that the Tests created some kind of 2nd instance. Running for now without persistence, and will then figure out how to add it for production, but disable it for Unit Tests.

let settings = FirestoreSettings()
settings.isPersistenceEnabled = false
        
let firestore = Firestore.firestore()
firestore.settings = settings

davidseek avatar Nov 24 '21 02:11 davidseek

I've got similar error with Firestore after unsintalling app on simulator and reinstalling it, and it happens all the time now. Before everything seems to work correctly :( I'm also using M1 Macbook it this have anything related to this issue. I've uninstall app again, make clean build, removed derived data, etc. and now it started to work again. But if it may crash it in production for significant number of users I am a little scared, and maybe I should turn off this local persistance?

michzio avatar Mar 08 '22 08:03 michzio

Hi, This is a known issue and a work around is using the memory persistence instead of disk persistence.

cherylEnkidu avatar Mar 08 '22 18:03 cherylEnkidu

It does happen to me as well. After uninstalling app on my simulator and adding more collection to the firestore manually, it solved. Still wondering what happen tho.

ekoirsyad avatar Mar 21 '22 05:03 ekoirsyad

I'm on Intel processor. Killing the app process (present even though the app was closed) in Activity Monitor solved the issue.

matthieudelaro avatar Mar 21 '22 10:03 matthieudelaro

I have faced this issue using React Native Firebase library. What resolved the issue in the end was restarting my computer.🙃

pohy avatar May 04 '22 13:05 pohy