realm / realm-core

Core database component for the Realm Mobile Database SDKs
https://realm.io
Apache License 2.0
1.02k stars 158 forks source link

Android 13 Backup Realm Database #6912

Open michaelsof47 opened 1 year ago

michaelsof47 commented 1 year ago

SDK and version

SDK : Android SDK 33 Version: Android 13 (Samsung A32)

Observations

Crash log / stacktrace

01 pc 00000000009eb5ac /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

02 pc 00000000009eb8a8 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

03 pc 00000000009eba34 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

04 pc 00000000009d9950 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

05 pc 00000000009d7160 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

06 pc 0000000000923ba4 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

07 pc 00000000006f114c /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

08 pc 00000000006486c4 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (Java_io_realm_internal_OsSharedRealm_nativeWriteCopy+692) (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)

11 pc 00000000004deb56 [anon:dalvik-classes.dex extracted in memory from /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/base.apk] (io.realm.internal.OsSharedRealm.writeCopy+78)

13 pc 00000000004b34fc [anon:dalvik-classes.dex extracted in memory from /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/base.apk] (io.realm.BaseRealm.writeCopyTo+16)

15 pc 00000000004d4574 [anon:dalvik-classes.dex extracted in memory from /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/base.apk] (io.realm.Realm.writeCopyTo+0)

Steps & Code to Reproduce

final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath().concat("/database.realm"));

if (file.exists())
    file.delete();

realm.writeCopyTo(file);
michaelsof47 commented 1 year ago

addition error information. i found an error in file.cpp... this error from my log

/tmp/realm-java/realm/realm-library/src/main/cpp/realm-core/src/realm/util/file.cpp:1102: [realm-core-13.9.0] Assertion failed: r == 0 && "File::unlock()" with (r, (*__errno())) = [-1, 38]

nicola-cab commented 1 year ago

Hello, thanks for reporting this. That assertion is triggered because the system call that is invoked for unlocking the file has failed. Do you happen to have multiple threads or processes that are trying to access the same realm file? Also, I am not familiar with the development environment for android. But most probably the file handle is held by some other process/thread and that's why you are getting that error. CC: @cmelchior

michaelsof47 commented 1 year ago

I actually watch an any class in our project. mostly many transaction do executeTransaction() without Thread condition and realm.close(). Only 1-2 function that i have encapsulation with Thread when i do executeTransactionAsync() or only 1 function that use realm.close(). what if any executeTransaction without realm.close() that have crash in one class especially that i have report this issue yesterday ? nb : sorry, i haven't good grammarly. but, i hope you have understand what i mean..

nicola-cab commented 1 year ago

I think your problem is a more general one. File::unlock() all it does is to call a system call to remove a file level lock (set at operating system level), which is used to ensure that multiple processes are reading correctly the file itself, via libRealm. In your case, this system call has failed. Meaning, it is very likely that an external process is accessing the realm file while your application is doing the same. Unfortunately, I can't really debug on a physical device, but if there is a way for me to reproduce the issue only on android studio in the emulator, I would be more than happy to try that.

cmelchior commented 1 year ago

This is a known limitation on newer versions of Android. Basically, you can no longer store Realms on external storage, unless you are backing up the file. You can see more here: https://github.com/realm/realm-core/issues/6028#issuecomment-1314119885

michaelsof47 commented 1 year ago

so, how we can alternative to export realm data into .realm or more universal access such like .json ?

michaelsof47 commented 1 year ago

I think your problem is a more general one. File::unlock() all it does is to call a system call to remove a file level lock (set at operating system level), which is used to ensure that multiple processes are reading correctly the file itself, via libRealm. In your case, this system call has failed. Meaning, it is very likely that an external process is accessing the realm file while your application is doing the same. Unfortunately, I can't really debug on a physical device, but if there is a way for me to reproduce the issue only on android studio in the emulator, I would be more than happy to try that.

i have to apologize. but, i can't do that because this project was close source

kiburtse commented 1 year ago

@michaelsof47 it's not realm specific, it's the android security restrictions, you need to use Scoped Storage API's

@nicola-cab @cmelchior it's been reported multiple times already #6664 #6028 #5581 #5107 #4810 #4783 #4777 #4754 Looks like pretty general pitfall at this point. Maybe we can handle it somehow better instead of crashing the app? Would it be possible to detect if flock is supported for the path and throw some exception with details?

nicola-cab commented 1 year ago

OK there you go, thanks @kiburtse. Maybe we can just throw an exception rather that having an assertion there?

cmelchior commented 1 year ago

Since this keeps coming up. Having a specific exception message when Core is built for Android would probably be quite nice 👍

nicola-cab commented 1 year ago

OK makes sense, let me add it.

michaelsof47 commented 1 year ago

thank you very much for have produce our problem in realm version. i waiting for it

kiburtse commented 1 year ago

So, ok, as discussed under linked #6926 we've probably misdiagnosed the issue before, and it may fix your problem. Most likely, locking was not needed for your usecase, but still let's keep this ticket to actually check what are constraints on external storage on real device with android 13 and api targeting Android >= 11. I've checked so far writeCopyTo with external sdcard with exFAT (it also seems we have some issues with such setup #6739), and it works fine from what i can tell, although on desktop OSes. Will check the real device with proper manifest.

michaelsof47 commented 1 year ago

so, how to limit storage that we stored data in realm and how to make writeCopyTo work well in android 13 above or below ?