realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

RealmFileException: Unable to open a realm at path #7301

Open vernazza opened 3 years ago

vernazza commented 3 years ago

Goal

Open a realm database

Actual Results

In a few devices, Realm cannot initialize the file and it throws an exception:

Fatal Exception: io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/user/0/my.package.name/files/default.realm': Realm file initial open failed Path:Exception backtrace:
<backtrace not supported on this platform>. (Realm file initial open failed Path: /data/user/0/my.package.name/files/default.realm
Exception backtrace:
<backtrace not supported on this platform>) (/data/user/0/my.package.name/files/default.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107
       at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java)
       at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:173)
       at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:249)
       at io.realm.BaseRealm.<init>(BaseRealm.java:138)
       at io.realm.BaseRealm.<init>(BaseRealm.java:105)
       at io.realm.Realm.<init>(Realm.java:159)
       at io.realm.Realm.createInstance(Realm.java:495)
       at io.realm.RealmCache.createInstance(RealmCache.java:481)
       at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:448)
       at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:412)
       at io.realm.Realm.getDefaultInstance(Realm.java:403)

Steps & Code to Reproduce

I cannot replicate the problem. I have the crash reports from open test users. This is how I initialize the DB in the app

       Realm.init(this)
       val realmConfiguration = RealmConfiguration.Builder()
                .encryptionKey(....)
                .assetFile("database/deploy.realm")
                .build()

        Realm.setDefaultConfiguration(realmConfiguration)

Then, later, the first time I do Realm.getDefaultInstance() it crashes for some users.

Version of Realm and tooling

Realm version(s): 10.3.1

Realm Sync feature enabled: No

Android Studio version: 4.1.2

Android Build Tools version: 4.1.2

Gradle version: 6.8.1

Which Android version and device(s): at the moment it happened on different Samsung models with Android 10 Affected users are about 0.3% of the open tests user base

clementetb commented 3 years ago

Thanks for reporting the issue, we are currently investigating it.

Are the affected users not able to use the app, or are these random crashes?

vernazza commented 3 years ago

They can't use the app, because those who are having this problem are having it every time the app tries to open the DB. I also tried to wrap the opening in a try/catch block, and try again, but the result is the same.

The Init block is in the Application OnCreate function.

rorbech commented 3 years ago

@vernazza Does the issue happen on fresh installs/reinstalls or are they ending up there after a while?

If on fresh installs would you be able to list the exact devices it is happening on? Or if after a while would you be able to get hold of a realm file exhibiting this behavior?

vernazza commented 3 years ago

Actually they are not fresh installs, but before that version, realm was not used, so it's the first time that those users use realm.

The problem seems to be on the creation of the DB file.

The devices that had the problems are Samsung Galaxy A51, Samsung Galaxy S9, Huaweu JNY-LX1. All 3 are Android 10

rorbech commented 3 years ago

Are you by any chance able to get a realm file from a device where this happens are able to share it or open it on a local device? And do you know whether it reoccurs if the app is uninstalled and installed again?

vernazza commented 3 years ago

Update. The devices where I'm getting the problem are: 56% android 10, 43% android 9 1% Android 7

Models:

So, it doesn't seem to be related to a specific vendor or to a specific OS

vernazza commented 3 years ago

Sorry, where it happens the DB is not created, so there is nothing to retrieve. Anyway, I could not replicate the problem and I have only the crash reports, so I don't know if uninstalling fixes the problem.

adhiamboperes commented 3 years ago

I have been getting the same exception since migrating to realm 10.3.0

Implementation

class App : Application() {   
     override fun onCreate() {
        super.onCreate()

      val realmConfiguration: RealmConfiguration = RealmConfiguration.Builder()
                  .name(App.instance.getString(R.string.app_name))
                  .schemaVersion(1)
                  .deleteRealmIfMigrationNeeded()
                  .build()

           Realm.init(this)
           Realm.setDefaultConfiguration(realmConfiguration)
    }
}

Calling Function

    // update cart with latest values
    private fun updateLocalCart(updatedCart: Cart) {
        Realm.getDefaultInstance().use { realm ->
            realm.executeTransaction { r->
                val c = r.where<Cart>().findFirst()
                c?.orderItems?.apply {
                    clear()
                    addAll(updatedCart.orderItems)
                }

                c?.totalAmount = updatedCart.totalAmount

                val totalItems = c?.orderItems?.size ?: 0

                cartValue.postValue(Pair(totalItems, c?.totalAmount ?: 0.0))
            }
        }
    }

Full trace

Fatal Exception: io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/user/0/com.company.app/files/AppName': Realm file initial open failed Path:Exception backtrace:
<backtrace not supported on this platform>. (Realm file initial open failed Path: /data/user/0/com.company.app/files/AppName
Exception backtrace:
<backtrace not supported on this platform>) (/data/user/0/com.company.app/files/AppName) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107
       at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java)
       at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:8)
       at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:2)
       at io.realm.BaseRealm.<init>(BaseRealm.java:12)
       at io.realm.Realm.<init>(Realm.java:4)
       at io.realm.Realm.createInstance(Realm.java:14)
       at io.realm.RealmCache.createInstance(RealmCache.java:14)
       at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:14)
       at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:14)
       at io.realm.Realm.getDefaultInstance(Realm.java:2)
       at com.company.app.ui.fragment.cart.CartViewModel.updateLocalCart(CartViewModel.java:1)
       at com.company.app.ui.fragment.cart.CartViewModel$getCart$1.invokeSuspend(CartViewModel.java:10)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:2)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:5)

Affected devices Lenovo, Huawei, Oppo, Infinix Mobility

Affected OSes

Android 5.1.1(29%), 9(14%) and 7(57%)

Tooling

This is happenning in production and I'm yet to reproduce it on debug.

+++ Our users have reported that the app works if they uninstall and reinstall

edualonso commented 3 years ago

Hello @adhiamboperes. Just out of curiosity: you mention you started seeing this crash after migrating to 10.3.1. Which version did you migrate from? And to be 100% sure, you were and are not using encryption in the database, right?

adhiamboperes commented 3 years ago

I am seeing this in v10.3.0. i updated from realm v10.2.0 @edualonso

vernazza commented 3 years ago

It seems that the problem is related to an incomplete DB while copying from the assets. I didn't have a chance to get a corrupted file, but I added some logs to check the file size and I see that it's smaller than the deployed one (I'm not deleting data from the DB, so it can't be smaller).

I solved my problem like this:

   fun getRealmDefaultInstance(): Realm {
        return try {
            Realm.getDefaultInstance()
        } catch (e: io.realm.exceptions.RealmFileException) {            
            //The file is probably malformed. Delete it and try again
            Realm.getDefaultConfiguration()?.let{Realm.deleteRealm(it)}
            Realm.getDefaultInstance()
        }
    }

and calling this instead of Realm.GetDefaultInstance()

BTW, I also tried reverting to Realm 10.0.0 and I kept getting the problem.

adhiamboperes commented 3 years ago

Thanks @vernazza , I'll try this fix and update if it helps

evjava commented 3 years ago

Reproduced today. Crash happened in production and probably on the fresh install.

Realm version: 10.3.1 Realm Sync feature enabled: No Android Studio version: 4.2 buildToolsVersion: 30.0.2 Gradle version: 6.8

Device: OUKITEL WP7, Android 9

init process:

Realm.init(this)
...
val configuration = RealmConfiguration.Builder()
  .schemaVersion(..)
  .migration(..)
  .name(Realm.DEFAULT_REALM_NAME)
  .build()
val realm = Realm.getInstance(configuration)
mahmoudElfeel67 commented 3 years ago

@clementetb i am also facing this issue

Unable to open a realm at path '/data/user/0/com.bloomer.alaWad3k/files/conetent_db/Db83.realm': Realm file initial open failed: Top ref outside file (size = 1081344). top_ref[0]: FFFFFFFFFFFFFFFF, top_ref[1]: 4088E8, mnemonic: 54 2D 44 42, fmt[0]: 22, fmt[1]: 22, flags: 1 Path:Exception backtrace: <backtrace not supported on this platform> Path: /data/user/0/com.bloomer.alaWad3k/files/conetent_db/Db83.realm Exception backtrace: <backtrace not supported on this platform>. (Realm file initial open failed: Top ref outside file (size = 1081344). top_ref[0]: FFFFFFFFFFFFFFFF, top_ref[1]: 4088E8, mnemonic: 54 2D 44 42, fmt[0]: 22, fmt[1]: 22, flags: 1 Path: /data/user/0/com.bloomer.alaWad3k/files/conetent_db/Db83.realm Exception backtrace: <backtrace not supported on this platform> Path: /data/user/0/com.bloomer.alaWad3k/files/conetent_db/Db83.realm Exception backtrace: <backtrace not supported on this platform>) (/data/user/0/com.bloomer.alaWad3k/files/conetent_db/Db83.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107

bmunkholm commented 2 years ago

@mahmoudElfeel67 This is not the same issue. Please create another github issue. Thanks!

clementetb commented 2 years ago

@adhiamboperes

I am reading your snippet and I noticed that on your Application constructor you call Realm.init() after building the configuration, that should not be even possible, as it would throw an IllegalStateException("Call Realm.init(Context) before creating a RealmConfiguration").

Is that App initialization code triggered at all?

tfkci commented 2 years ago

Having the same issue. The realm file turns into a malformed file for some reason randomly. Couldnt Find the real cause.

Realm Version: 10.10.1

Can the realm malformation happen if the user left the process while the realm transaction occurs?

Unable to start activity ComponentInfo{com.ertech.daynote/com.ertech.daynote.Activities.MainActivity}: io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/user/0/com.ertech.daynote/files/default.realm': Invalid top array size (ref: 120512, size: 0) Path:Exception backtrace: <backtrace not supported on this platform>. (Invalid top array size (ref: 120512, size: 0) Path: /data/user/0/com.ertech.daynote/files/default.realm Exception backtrace: <backtrace not supported on this platform>) (/data/user/0/com.ertech.daynote/files/default.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 106 Kind: ACCESS_ERROR.

io.realm.internal.OsSharedRealm.nativeGetSharedRealm (OsSharedRealm.java)
io.realm.internal.OsSharedRealm.<init> (OsSharedRealm.java:175)
io.realm.internal.OsSharedRealm.getInstance (OsSharedRealm.java:251)
io.realm.BaseRealm.<init> (BaseRealm.java:141)
io.realm.BaseRealm.<init> (BaseRealm.java:108)
io.realm.Realm.<init> (Realm.java:159)
io.realm.Realm.createInstance (Realm.java:495)
io.realm.RealmCache.createInstance (RealmCache.java:494)
io.realm.RealmCache.doCreateRealmOrGetFromCache (RealmCache.java:461)
io.realm.RealmCache.createRealmOrGetFromCache (RealmCache.java:422)
io.realm.Realm.getInstance (Realm.java:424)
note8g2018 commented 2 years ago

Realm 10.10.1 Android Gradle Plugin Version 7.3.0-alpha07 Gradle Version 7.4.1 same problem crash because Realm.init(this)

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.coolme.me.square18, PID: 11723
    java.lang.ExceptionInInitializerError: RealmTransformer doesn't seem to be applied. Please update the project configuration to use the Realm Gradle plugin. See https://docs.mongodb.com/realm/sdk/android/install/#customize-dependecies-defined-by-the-realm-gradle-plugin
        at io.realm.RealmConfiguration.<clinit>(RealmConfiguration.java:80)
        at io.realm.RealmConfiguration.access$000(RealmConfiguration.java:68)
        at io.realm.RealmConfiguration$Builder.initializeBuilder(RealmConfiguration.java:552)
        at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:538)
        at io.realm.Realm.initializeRealm(Realm.java:320)
        at io.realm.Realm.init(Realm.java:261)
        at com.coolme.me.square18.MyApplication.onCreate(MyApplication.kt:15)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1223)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6734)
        at android.app.ActivityThread.access$1500(ActivityThread.java:256)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2090)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I/Process: Sending signal. PID: 11723 SIG: 9
note8g2018 commented 2 years ago

I found the solution: Use Kotlin SDK instead of Java SDK https://www.mongodb.com/docs/realm/sdk/kotlin/install/android/ here is the document even work with hilt ☺☺☺☺😀😀😀

hukum-singh commented 2 years ago

Hint - 99% crashes are heppening in the background only, when workmanger is getting triggered. also it is happening only devices which are android-9 or above.

wolfAle commented 2 years ago

Hi,

I was debugging my app and I just bumped in what seems to be the same issue.

io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/user/0/it.intesys.dolomitisuperski/files/default.realm': Realm file initial open failed: Top ref outside file (size = 7376896). top_ref[0]: 30315000000E0F8, top_ref[1]: 46020200, mnemonic: 54 2D 44 42, fmt[0]: 22, fmt[1]: 22, flags: 1

The application was working just fine, did a few changes not Realm reladed to the code and suddenly it started throwing this error. I do have a copy of the broken .realm file, if that can be helpful in anyway. I didn't manage to open that file in any way, not even with Realm Studio/Mongo DB Realm Studio. I'm not sure I'll be able to replicate the issue tho, I need to uninstall the app and proceed further with the development now.

It's an Android application in Kotlin and: classpath "io.realm:realm-gradle-plugin:10.8.0"

Let me know if I can help in any way with the debug.

Kind regards, Alessandro

susonthapa commented 1 year ago

I am running into this issue on Android(The app is ReactNative), iOS works fine. We get the following exception. This is happening mostly on Samsung devices.

Unable to open a realm at path '/data/user/0/com.example.app/files/app.realm': Realm file initial open failed: Top ref outside file (size = 36388864). top_ref[0]: FFFFFFFFFFFFFFFF, top_ref[1]: 3368860, mnemonic: 54 2D 44 42, fmt[0]: 9, fmt[1]: 9, flags: 1

When I split the Realm file(using the split command) and only use the first part I get a similar crash as above. I think this points to the Realm file not being copied properly. This is happening to quite a lot of users. On Android, the assets are compressed by default so I think that might be interfering with the copying process.

rorbech commented 1 year ago

Hi @susonthapa. This repository is for realm-java. If you use realm-js with ReactNative then please report your issues at https://github.com/realm/realm-js.

osafahmed51 commented 1 year ago

Caused by io.realm.exceptions.RealmFileException Unable to open a realm at path '/data/user/0/app.quizpatenteonline.quizpatenteb2018pertutti/files/realm_encrypt.realm': Realm file decryption failed Path:Exception backtrace: . (Realm file decryption failed Path: /data/user/0/app.quizpatenteonline.quizpatenteb2018pertutti/files/realm_encrypt.realm Exception backtrace: ) (/data/user/0/app.quizpatenteonline.quizpatenteb2018pertutti/files/realm_encrypt.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107

if anyone got the solution , please help me to get myself out of it. Thanks

VijayPatil14121988 commented 1 year ago

In my case, I identified the root cause of the issue as the version of the Android application being used by our users, which was 1.0.1. Upon attempting to update to the newer version 1.0.2, I encountered an issue with realm encryption file access. While reviewing the changes I had made, I realized that I had modified the application name from 'x' to 'y'. This change impacted the encryption key generated from KeyPairGeneratorSpec, which was based on the application name. To resolve the issue, I re-added the previous application name for key pair generation, in addition to other feature changes made in the application. This successfully resolved the issue with realm encryption file access.

osafahmed51 commented 1 year ago

actually i am also getting error on update , when user update the app from playstore it get crash . while uninstalling the previous and installing the new one went well, app name is same for both the versions

joaodevsantos commented 1 year ago

I'm facing the same issue by now:

Caused by io.realm.exceptions.RealmFileException: Failed to open Realm file at path '/data/user/0/<APP_ID>/files/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=4096)') (/data/user/0/<APP_ID>/files/default.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107 at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java) at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:175) at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:260) at io.realm.BaseRealm.<init>(BaseRealm.java:142) at io.realm.BaseRealm.<init>(BaseRealm.java:109) at io.realm.Realm.<init>(Realm.java:161) at io.realm.Realm.createInstance(Realm.java:535) at io.realm.RealmCache.createInstance(RealmCache.java:508) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:461) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:422) at io.realm.Realm.getDefaultInstance(Realm.java:443)

VijayPatil14121988 commented 1 year ago

I'm facing the same issue by now:

Caused by io.realm.exceptions.RealmFileException: Failed to open Realm file at path '/data/user/0/<APP_ID>/files/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=4096)') (/data/user/0/<APP_ID>/files/default.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107 at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java) at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:175) at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:260) at io.realm.BaseRealm.<init>(BaseRealm.java:142) at io.realm.BaseRealm.<init>(BaseRealm.java:109) at io.realm.Realm.<init>(Realm.java:161) at io.realm.Realm.createInstance(Realm.java:535) at io.realm.RealmCache.createInstance(RealmCache.java:508) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:461) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:422) at io.realm.Realm.getDefaultInstance(Realm.java:443)

Can you check by any chance if you updated your app name? In my case, the app name was part of my encryption key and when I changed the app name it was failing.

joaodevsantos commented 1 year ago

I'm facing the same issue by now: Caused by io.realm.exceptions.RealmFileException: Failed to open Realm file at path '/data/user/0/<APP_ID>/files/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=4096)') (/data/user/0/<APP_ID>/files/default.realm) in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 107 at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(OsSharedRealm.java) at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:175) at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:260) at io.realm.BaseRealm.<init>(BaseRealm.java:142) at io.realm.BaseRealm.<init>(BaseRealm.java:109) at io.realm.Realm.<init>(Realm.java:161) at io.realm.Realm.createInstance(Realm.java:535) at io.realm.RealmCache.createInstance(RealmCache.java:508) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:461) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:422) at io.realm.Realm.getDefaultInstance(Realm.java:443)

Can you check by any chance if you updated your app name? In my case, the app name was part of my encryption key and when I changed the app name it was failing.

I have already looked into that but no because we use the app identifier and the apps are live for a while (+/- 4 years)

clementetb commented 1 year ago

Do you have compaction enabled in your Realm?

joaodevsantos commented 1 year ago

Do you have compaction enabled in your Realm?

No

joaodevsantos commented 1 year ago

I got my answer to this issue on the issue below:

https://github.com/realm/realm-java/issues/7574

andkrawiec commented 1 year ago

I'm also facing the same problem. This issue seems to occur intermittently, affecting only certain devices while most work fine. After analyzing the pattern, the error appears more likely to occur when the system marks the application as "unused" and initiates the power-saving or sleep mode. This leads me to hypothesize that the error might be connected to the system's attempt to put the application to sleep, affecting the Realm database operations.

arundey commented 8 months ago

Facing same issue and many crashes logged in crashlytics. If anyone got the solution, please help here.

Screenshot 2024-03-11 at 12 49 08 PM
Velord commented 7 months ago

+1, crash happens most of the time in the background 91%. Also it is in 4 sec after user start session. Also most crashes on Xiaomi devices 92%.

shiv2664 commented 2 weeks ago

Some users facing same issue after I updated the realm version from 6.0.2 to 10.11.1 app crashing when updated from play store and works fine when freshly installed default.realm': Invalid top array size same as @tfkci