Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 13 Backup Realm Database #6912

Open
michaelsof47 opened this issue Aug 22, 2023 · 14 comments
Open

Android 13 Backup Realm Database #6912

michaelsof47 opened this issue Aug 22, 2023 · 14 comments

Comments

@michaelsof47
Copy link

michaelsof47 commented Aug 22, 2023

SDK and version

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

Observations

  • How frequent do the crash occur? -> more then 1 times
  • test : development test by android studio
  • Can the crash be reproduced by you? -> no
    step by step :
  1. do activate storage access from settings manufacture device
  2. do click button that trigger access download realm database

Crash log / stacktrace

#1 pc 00000000009eb5ac /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#2 pc 00000000009eb8a8 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#3 pc 00000000009eba34 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#4 pc 00000000009d9950 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#5 pc 00000000009d7160 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#6 pc 0000000000923ba4 /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#7 pc 00000000006f114c /data/app/~~UExZR8o2wgDERmbmmDgl-Q==/dev.koffiesoft.com.koffiepos-V3aBg_J-gTdFrF2Fx-st-w==/lib/arm64/librealm-jni.so (BuildId: c6264775c67e2f6754b1267f9e52dbd092cfe418)
#8 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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

nicola-cab commented Aug 23, 2023

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
Copy link
Contributor

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: #6028 (comment)

@michaelsof47
Copy link
Author

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

@michaelsof47
Copy link
Author

michaelsof47 commented Aug 24, 2023

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
Copy link
Contributor

kiburtse commented Aug 24, 2023

@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
Copy link
Member

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

@cmelchior
Copy link
Contributor

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

@nicola-cab
Copy link
Member

OK makes sense, let me add it.

@michaelsof47
Copy link
Author

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

@kiburtse
Copy link
Contributor

kiburtse commented Sep 4, 2023

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants