realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RealmFileException: Unable to open a realm at path

Open vernazza opened this issue 4 years ago • 37 comments

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

vernazza avatar Feb 07 '21 22:02 vernazza

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?

clementetb avatar Feb 12 '21 09:02 clementetb

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.

vernazza avatar Feb 12 '21 09:02 vernazza

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

rorbech avatar Feb 22 '21 12:02 rorbech

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

vernazza avatar Feb 23 '21 11:02 vernazza

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?

rorbech avatar Feb 23 '21 12:02 rorbech

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

Models:

  • Samsung Galaxy a51
  • Samsung Galaxy S9
  • Samsung Galaxy S8+
  • Samsung Galaxy S8
  • Samsung Galaxy A10
  • Huawey JNY-LX1
  • A-gold F1

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

vernazza avatar Feb 26 '21 11:02 vernazza

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.

vernazza avatar Feb 26 '21 15:02 vernazza

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

  • Gradle 6.5
  • Realm 10.3.0
  • Realm Sync feature enabled: No
  • Android Studio version: 4.1.2
  • Android Build Tools version: 4.1.2

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

adhiamboperes avatar Mar 09 '21 07:03 adhiamboperes

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?

edualonso avatar Mar 09 '21 09:03 edualonso

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

adhiamboperes avatar Mar 12 '21 06:03 adhiamboperes

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.

vernazza avatar Mar 17 '21 09:03 vernazza

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

adhiamboperes avatar Mar 17 '21 09:03 adhiamboperes

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)

evjava avatar May 12 '21 09:05 evjava

@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

mahmoudElfeel67 avatar Oct 09 '21 17:10 mahmoudElfeel67

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

bmunkholm avatar Nov 16 '21 10:11 bmunkholm

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

clementetb avatar Dec 03 '21 10:12 clementetb

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)

tfkci avatar Feb 23 '22 13:02 tfkci

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 avatar Mar 27 '22 21:03 note8g2018

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 ☺☺☺☺😀😀😀

note8g2018 avatar Mar 29 '22 21:03 note8g2018

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.

hukum-singh avatar Aug 12 '22 19:08 hukum-singh

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

wolfAle avatar Oct 10 '22 06:10 wolfAle

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.

susonthapa avatar Jan 30 '23 12:01 susonthapa

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.

rorbech avatar Jan 30 '23 13:01 rorbech

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

osafahmed51 avatar Mar 15 '23 11:03 osafahmed51

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.

VijayPatil14121988 avatar Mar 15 '23 16:03 VijayPatil14121988

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

osafahmed51 avatar Mar 18 '23 10:03 osafahmed51

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)

joaodevsantos avatar Jul 21 '23 09:07 joaodevsantos

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.

VijayPatil14121988 avatar Jul 21 '23 09:07 VijayPatil14121988

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)

joaodevsantos avatar Jul 21 '23 09:07 joaodevsantos

Do you have compaction enabled in your Realm?

clementetb avatar Jul 21 '23 10:07 clementetb