Realm-java: Exception : Invalid format of Realm file.

Created on 24 Jun 2016  路  20Comments  路  Source: realm/realm-java

I'm using realm for databse processingin my android project. And I got some user's report about the crash:

java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file.
at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions()(SourceFile:-2)
at io.realm.internal.SharedGroup.<init>()(SourceFile:64)
at io.realm.internal.SharedGroupManager.<init>()(SourceFile:49)
at io.realm.BaseRealm.<init>()(SourceFile:77)
at io.realm.Realm.<init>()(SourceFile:138)
at io.realm.Realm.createAndValidate()(SourceFile:269)
at io.realm.Realm.createInstance()(SourceFile:249)
at io.realm.RealmCache.createRealmOrGetFromCache()(SourceFile:114)
at io.realm.Realm.getInstance()(SourceFile:213)

I never face the crash while developing but seems that my user got it many times. Below is my implementation which crash was detected:


private Realm getRealmFromConfig(Context context) {
    RealmConfiguration config = getRealmConfigByDBName(context, databaseName);
    Realm.compactRealm(config);
    return Realm.getInstance(config);
}

public RealmConfiguration getRealmConfigByDBName(Context context, String dbName) {
    RealmConfiguration config = new RealmConfiguration.Builder(context)
            .name(dbName)
            .schemaVersion(1)
            .setModules(new Module())
            .deleteRealmIfMigrationNeeded()
            .build();
    return config;
}

Details

The crash log points out that crash occur in line return Realm.getInstance(config);

I have a look at Realm.java file but still can not get any more information which related to message Invalid format of Realm file. So any one can help to give me a solution or workaround which can resolve the problem or some theory about why crash occurs.

Thanks for helping

O-Community Pipeline-On-Hold Reproduction-Required T-Bug-Crash

Most helpful comment

In last 30 days this crash hit 146 times for 8 different users! Happened in all android versions 4,5 and 6!

In onCreate() of App.java I init realm like this

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(App.this)
                .schemaVersion(10)
                .migration(talkootMigration)
                .name("db")
                .build();
 Realm.setDefaultConfiguration(realmConfiguration);
 Realm.compactRealm(realmConfiguration);

In my activity's onCreate() method I create realm instance as follows,

mRealm = Realm.getDefaultInstance();
mRealm.setAutoRefresh(true);

Crash starts from this line 'mRealm = Realm.getDefaultInstance()' from onCreate() of activity.
I don't have any more information from the crash log or from users.

I couldn't reproduce this bug on my own. But my few of my users are experiencing it. But those who experiences this. experience this multiple times.

Any help would be appreciated.
And sorry I can't share you the entire source code.

All 20 comments

Hi @dinhthaidaica

What version of Realm java are you using?
And what versions of Realm java were you using?

In version 0.91, we fixed a bug that does not remove some files when deleting a realm database file (that includes deletion by deleteRealmIfMigrationNeeded()). See #2839

Hi @zaki50
Thank you for your reply.
I'm currently using version 0.89.1 of Realm java.
In a try to fix that issue, I did remove Realm.compactRealm(config); to hope that issue has been resolve.
Any way I'll upgrade version of realm in my app and let you know if it help or not :)

@dinhthaidaica I'll close the issue for now. Don't hesitate to reopen or create a new if your experience the problem after upgrading to a newer version of Realm.

I have similar problem with this, I using a IntentService to sync data with api. I am using a global Realm config object as member of the Application object.

Some of my users do experienced crash which identical to @dinhthaidaica 's issue.

@kuno Does your service run in a separate process? Realm does not yet support that.

Hi @kneth , As your suggestion I have upgrade realm version to 1.1.0 and seem that my users still experienced this crash many times :(.
One thing I want to notice that our development and testing team can not reproduce this issue with both of debug mode and release mode, but users experienced it a lot of times. Please re-open this issue and if you have any suggestion as a workaround, please let me know. I'm very ready if you want to get any information in my implementation.
Btw, I'd love to have you and your team's suggest about how to implement realm in perfect way (example in the case of @kuno ), because in official website it's hard for developer to know how do a right way.

I'm experiencing the same issue in Realm 1.0.0
Crashlog.

Fatal Exception: java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file. at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(SharedGroup.java) at io.realm.internal.SharedGroup.(SharedGroup.java) at io.realm.internal.SharedGroupManager.(SharedGroupManager.java) at io.realm.BaseRealm.(BaseRealm.java) at io.realm.Realm.(Realm.java) at io.realm.Realm.createAndValidate(Realm.java:248) at io.realm.Realm.createInstance(Realm.java:228) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:126) at io.realm.Realm.getDefaultInstance(Realm.java:174) at com.talkoot.android.pubnub.PubNubObserverDelegateImpl$$Lambda$2.run(Unknown Source:1072) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841)

Devices:

  1. Samsung GT-N7100, OS 4.4.2, Realm version 1.0.0

@dinhthaidaica @imidhuntv

Is it possible that you catch some exceptions thrown by Realm and not handle them properly?

If you want, things would be faster if you can share your source code with us privately to [email protected] . We can try to take look to see would could cause the problem.

@dinhthaidaica @imidhuntv Do you have the possibility to share your code with us?

In last 30 days this crash hit 146 times for 8 different users! Happened in all android versions 4,5 and 6!

In onCreate() of App.java I init realm like this

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(App.this)
                .schemaVersion(10)
                .migration(talkootMigration)
                .name("db")
                .build();
 Realm.setDefaultConfiguration(realmConfiguration);
 Realm.compactRealm(realmConfiguration);

In my activity's onCreate() method I create realm instance as follows,

mRealm = Realm.getDefaultInstance();
mRealm.setAutoRefresh(true);

Crash starts from this line 'mRealm = Realm.getDefaultInstance()' from onCreate() of activity.
I don't have any more information from the crash log or from users.

I couldn't reproduce this bug on my own. But my few of my users are experiencing it. But those who experiences this. experience this multiple times.

Any help would be appreciated.
And sorry I can't share you the entire source code.

@imidhuntv Is PubNubObserverDelegateImpl running in a different process? Could it start before Realm.compactRealm() finished? Can you share some code about starting the thread of PubNubObserverDelegateImpl?

@beeender The above one was a different log. See this one too.

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.talkoot.android/com.talkoot.android.activities.SplashActivity}: java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Caused by java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file.
at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(SharedGroup.java)
at io.realm.internal.SharedGroup.(SharedGroup.java)
at io.realm.internal.SharedGroupManager.(SharedGroupManager.java)
at io.realm.BaseRealm.(BaseRealm.java)
at io.realm.Realm.(Realm.java)
at io.realm.Realm.createAndValidate(Realm.java:248)
at io.realm.Realm.createInstance(Realm.java:228)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:126)
at io.realm.Realm.getDefaultInstance(Realm.java:174)
at com.talkoot.android.activities.BaseActivity.onCreate(BaseActivity.java:83)
at com.talkoot.android.activities.SplashActivity.onCreate(SplashActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

PubNubObserverDelegateImpl is not in a different process and it can not start before Realm.compactRealm().

I'm experiencing the same issue in Realm 1.1.0

The context was a user updated the app (only app, no realm version or schema). When he open the app a IntentService was call throw a "java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file."

Obs.: When the same user delete the app when reinstall all features run with success. No more exception been throw.

n Exception java.lang.IllegalArgumentException: Illegal Argument: Invalid format of Realm file. io.realm.internal.SharedGroup.createNativeWithImplicitTransactions (SharedGroup.java) io.realm.internal.SharedGroup.openSharedGroupOrFail (SharedGroup.java:95) io.realm.internal.SharedGroup.<init> (SharedGroup.java:74) io.realm.internal.SharedGroupManager.<init> (SharedGroupManager.java:49) io.realm.BaseRealm.<init> (BaseRealm.java:81) io.realm.Realm.<init> (Realm.java:140) io.realm.Realm.createAndValidate (Realm.java:240) io.realm.Realm.createInstance (Realm.java:220) io.realm.RealmCache.createRealmOrGetFromCache (RealmCache.java:126) io.realm.Realm.getDefaultInstance (Realm.java:166)

@lucasgcampos Thanks for sharing the observation.

@lucasgcampos Do you know if the app has an open Realm instance in another thread when the IntentService is opening a Realm? I am trying to get a picture of the timing to see it we have a race condition.

We have a GCM Receiver which extends From FirebaseMessagingService like below.

`public class GcmReceiver extends FirebaseMessagingService {

private static String TAG = "GcmReceiver";

@Inject
PubNubObserverDelegate mDelegate;

@Override
public void onMessageReceived(RemoteMessage message) {
    String from = message.getFrom();
    Map data = message.getData();
    if (!from.equals(BuildConfig.GCM_SENDER_ID))
    {
        return;
    }

    App.getComponent().inject(this);
    JSONObject json = new JSONObject();
    Set<String> keys = data.keySet();
    for (String key : keys) {
        try {
            // json.put(key, bundle.get(key)); see edit below
            json.put(key, data.get(key));
        } catch(JSONException e) {
            //Handle exception here
            e.printStackTrace();
        }
    }
    String channel = null;
    if (data.containsKey("channelId"))
        channel = data.get("channelId").toString();
    if (channel != null)
        mDelegate.showChatNotification(channel, json);
    else
        Log.v(TAG, "Ignore non chat message");
}

}`

In mDelegate.showChatNotification(channel, json) method we get realm instance and write the message to realm.
We haven't specified any process for GCM receiver. Could this be the problem in anyways?
Is GCM receiving the message in another process and is our crash happening while we open the app while GCM is writing to realm?
Any help?

This thread increases my suspicion

Is your Realm encrypted?

Also, why have I just asked this a second ago elsewhere?

I folded a number of other issues into this one, so we just have this error tracked in one place.

Just to iterate the current state of "Invalid format of the Realm file" errors:

  • Trying to open an valid encrypted file with a wrong key will cause this.
  • Trying to open a corrupted file with an valid encryption key will cause this. We cannot detect if a file is corrupted or the wrong key is used - #3641.
  • The file was copied manually and the input/output stream wasn't closed correctly can cause this. Use e.g. RealmConfiguration.Builder.initialData() or RealmConfiguration.Builder.assetFile() instead.
  • Another non-Realm process/thread is deleting/writing to the file can cause this..
  • Play Store Backup is most likely not responsible for this error - #3694 .
  • Opening a Realm file from multiple processes using Realm Java will _not_ cause this. Realm Core is process safe, but Realm Java is not, (yet - #1300 ).
  • We cannot rule out a bug in Realm Core yet. We are working on adding more error checking for this. See https://github.com/realm/realm-core/pull/2259.

Closing as outdated and these kinds of errors also seem to have disappeared from later versions of Realm (for whatever reason).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gpulido picture gpulido  路  3Comments

nolanamy picture nolanamy  路  3Comments

cmelchior picture cmelchior  路  3Comments

Merlin1993 picture Merlin1993  路  3Comments

aschrijver picture aschrijver  路  3Comments