Permission denied in /src/main/cpp/io_realm_internal_SharedRealm.cpp line 101 on Huawei P8 and P8 Lite
Edit by @cmelchior See https://github.com/realm/realm-java/issues/3972#issuecomment-313675948 for the most plausible reason for this.
Goal
What do you want to achieve?
Open Realm file on Huawei P8 and Huawei P8 Lite
Expected Results
Realm should work on all devices
Actual Results
Apparently there are 2 very specific Huawei devices (Android 6.0) where Realm files cannot be opened.
There is also possibility that this only occurs in multi-process access, but it is happening in production.
io.realm.exceptions.RealmError: Unrecoverable error. Permission denied in /Users/Nabil/Dev/realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 101
at io.realm.internal.SharedRealm.nativeGetSharedRealm(SharedRealm.java)
at io.realm.internal.SharedRealm.<init>(SharedRealm.java:172)
at io.realm.internal.SharedRealm.<init>(SharedRealm.java:172)
at io.realm.internal.SharedRealm.getInstance(SharedRealm.java:219)
at io.realm.internal.SharedRealm.getInstance(SharedRealm.java:209)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:319)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getInstance(Realm.java:353)
at x.data.repository.LongRealmRepositoryImpl.<init>(LongRealmRepositoryImpl.java:25)
at x.data.repository.impl.tv_programs.program_info.ProgramNotificationRepositoryImpl.<init>(ProgramNotificationRepositoryImpl.java:15)
at x.application.RestoreAfterReboot.onReceive(RestoreAfterReboot.java:38)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2760)
at android.app.ActivityThread.access$1800(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1453)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
and
io.realm.exceptions.RealmError: Unrecoverable error. Permission denied in /Users/Nabil/Dev/realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 101
at io.realm.internal.SharedRealm.nativeGetSharedRealm(SharedRealm.java)
at io.realm.internal.SharedRealm.<init>(SharedRealm.java:172)
at io.realm.internal.SharedRealm.<init>(SharedRealm.java:172)
at io.realm.internal.SharedRealm.getInstance(SharedRealm.java:219)
at io.realm.internal.SharedRealm.getInstance(SharedRealm.java:209)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:319)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getInstance(Realm.java:353)
at x.application.CustomApplication.initializeRealm(CustomApplication.java:397)
at x.application.CustomApplication.initialize(CustomApplication.java:229)
at x.application.Initializer.initialize(Initializer.java:12)
at x.presentation.view.activity.base.BaseActivity.onCreate(BaseActivity.java:261)
at x.presentation.view.activity.DashboardActivity.onCreate(DashboardActivity.java:156)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Steps & Code to Reproduce
I do not have these devices. They are in the wild. But technically it just opens a Realm in Application.onCreate(), or in BroadcastReceiver.onReceive().
Code Sample
It really is just opening a Realm configuration. Properties:
- No encryption
deleteIfMigrationNeeded()
The tricky thing here is that
1.) these devices already had permission troubles with 1.2.0 (?!) 2.) it only happens on two specific Huawei devices, but it seems to be a popular device, because it affects quite a few users... :| 3.) the access of Realm file is multi-process (broadcast receiver + app itself)
Version of Realm and tooling
Realm version(s): 1.2.0 and 4.1.1
Realm sync feature enabled: no
Android Studio version: irrelevant
Which Android version and device:
Android: 6.0
Android Build: GRA-L09C150B390
Manufacturer: HUAWEI
Model: HUAWEI GRA-L09
and
Android: 6.0
Android Build: ALE-L21C150B588
Manufacturer: HUAWEI
Model: ALE-L21
(i think this is the same as https://github.com/realm/realm-java/issues/3972 )
I also see that there were similar problems with Huawei after factory reset, but it affects 267 users which means it's highly unlikely that they're all doing factory reset without wiping data. 😄
Now this is a question worth investigating for the app: https://github.com/realm/realm-java/issues/3972#issuecomment-311869899
We'll try to change the directory
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.directory(getExternalCacheDir())
.build();
On those Huawei devices and see if it helps...
Thanks for digging through our own issues @Zhuinden 😄 , let us know if it makes a difference.
For others, there are also more context here: https://github.com/realm/realm-java/issues/3972#issuecomment-313675948
My guess is that there are other situations than factory reset that might cause the uid to change, but exactly what is hard to tell.
The tricky thing is that it ONLY happens in Huawei P8 and Huawei P8 Lite.
Based on the logs however, I think it is most prominent when you actually try to... Reboot the phone, and access Realm in a broadcast receiver that receives BOOT_COMPLETED.
I'm not sure when the next update roll out will happen though, so checking will be tricky. I kinda wish I had this device physically
Hey @Zhuinden
Is there any update on this issue?
I am facing the same crash on line 101 of io_realm_internal_SharedRealm.cpp.
Following are the details about the device

@rahulrvp I no longer work there but my ex-workmate is trying to workaround this issue today, I don't know if he's succeeded yet.
My guess is that we should open Realm into getExternalCacheDir() on the two HUAWEI devices, his guess is that he should just clear user data for the app if the error comes up.
I also think that this is related to multi-process Realm access on Huawei devices, MAYBE related to the lock file not being released right, OR the system not allowing the creation of pipe files in /data/data/package (which would be a real bummer)
I wish I had the device myself 😄 but as I don't, I only have guesses
@Zhuinden thanks for the quick reply.
This one is not a Huawei device. And even I don't have this device with me. This is a crash report came from one of the users who uses this app. 😄
Anyways, I will do the workaround you've suggested and will see if that helps.
Thanks!
I'd only use the getExternalCacheDir() on the specific device type where the error occurs, though.
I'd prefer to store my Realm in /data/data if possible 😄
This really is problematic though because the Huawei P8 is quite popular in this area.
I think that multi-process access might be related, but I'm not entirely sure. I don't have the test phone. :|
I had no other choice but switch back to SQLite from Realm to solve this. Using getExternalCacheDir() did not solve the issue and I was not able to get the device for debugging. :|
@Zhuinden thank you so much for spending your time trying to help me out. I really appreciate that! :)
What sucks is that I never got the phone and also never figured it out.
I really do think it has something to do with the multiprocess access that happens on booting the phone and the broadcast receiver runs.. But I have no idea what really happens.
It's strange to me that Realm gets messages from the OS that aren't very descriptive. Permission denied to do what, exactly?
In the end, this affected 1% of users and that's kind of a lot. We added the workaround "delete the data of the app if this happens" but I'm super uneasy about having to do that.
....I shoulda just bought the damn phone.
It could be an SELinux permission policy violation which results in a Permission denied, if it's the case usually you'll see logcat outputting the violation before denying the access
Example:
type=1400 audit(0.0:315): avc: denied { create } for name="access_control.new_commit.cv" scontext=u:r:system_app:s0 tcontext=u:object_r:system_app_data_file:s0 tclass=fifo_file permissive=0
https://github.com/realm/realm-java/issues/4630
I really need to buy this device at some point :smile:
But as much as I hate to say it, I think it is Realm-Core responsibility to have a workaround for when they cannot create pipe file. I think this permission issue could have been there since 0.89.0 with the introduction of named pipes, but might emerge primarily in multi-process access on some devices, or possible concurrent access on initialization.
I wonder if making getInstance() method call synchronized would have solved my issue. My ex-coworker went with the "delete everything" approach but I'm honestly not too fond of it. But that is still better than the app not starting.
One day, we should clearly buy a Huawei P8 Lite, create a multi-process sample, then figure out how beeender used to debug this sort of thing.
https://github.com/realm/realm-java/issues/3972#issuecomment-376259546 <-- this doesn't sound particularly healthy, either. I wonder if it is the same cause.

Are you sure the error is for Huawei P8 Lite? I see more devices in my list
For me it was on Huawei P8 and Huawei P8 Lite. Didn't happen on other devices.
I do not work there anymore though (for other reasons ofc) so my ex-co-worker attempted to add the "app clear data" workaround, I no longer have access to see if it helped either.
@Zhuinden @cmelchior
It took a few releases to isolate the the Permission denied exception. But yes, I can confirm that this happens primarily on Huawei devices. Currently in this case, I don't have any fallback - the DB is simply not available for these users (for storage).

See https://github.com/realm/realm-java/issues/3972 for more of this issue occurring.
any update ??
any updates??
I guess, this is the same as https://github.com/realm/realm-java/issues/6378...