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

Realm Swift - iOS 14 issue - there’s no crash, but saved info in database is not available #6843

Closed
AuraNica opened this issue Oct 6, 2020 · 22 comments

Comments

@AuraNica
Copy link

AuraNica commented Oct 6, 2020

Goals

My app should be able to access already saved data from database.

Expected Results

As above

Actual Results

After some time spent in the app, users with iOS 14 and above can't get or save data from/to database. There's no error or crash - it seems like the info from database is not there anymore (but it is).

Steps for others to Reproduce

I was able to reproduce this only on the store version of my app - just opened it, navigated to a few screens, saved some data, retrieved some data.
After a while, after the app was send to background and activated again (for several times), when I try to read from database the same info that was ok before - realm says it's not there anymore. But after I run the same app from xcode (so on top of the one downloaded from store) - the info was there, I could read it, the database was ok to read and write again.

The issue goes away for awhile if the users reinstall the app, but after using it for a few minutes it happens again.

Code Sample

The database uses encryption as documented here: https://docs.realm.io/sync/using-synced-realms/encrypting-realms

Reading this issue, I already updated realm to the version 5.4.7, but the issue is still happening. And I don't think is the same problem because I don't have shared app group container implemented.

Version of Realm and Tooling

Realm framework version: 5.4.7

Xcode version: 12.0.1

iOS version: >= 14.0

Dependency manager + version: COCOAPODS 1.9.3

Our crash reporting tool found this kind of crashes, but I have no idea if is related to this issue or not (on the phone that I could reproduce 2 times there's no crash report), I never saw the app crashing (neither did the other users that faced this problem), but maybe it crashed when it was in the background.

Crashed: com.google.fira.worker
0 libobjc.A.dylib 0x19a4dcdf0 objc_release + 16
1 libobjc.A.dylib 0x19a4de584 AutoreleasePoolPage::releaseUntil(objc_object**) + 200
2 libobjc.A.dylib 0x19a4de460 objc_autoreleasePoolPop + 208
3 libdispatch.dylib 0x1868d2260 _dispatch_last_resort_autorelease_pool_pop + 40
4 libdispatch.dylib 0x18687bae4 _dispatch_lane_invoke$VARIANT$mp + 520
5 libdispatch.dylib 0x186885518 _dispatch_workloop_worker_thread + 712
6 libsystem_pthread.dylib 0x1cc57c5a4 _pthread_wqthread + 272
7 libsystem_pthread.dylib 0x1cc57f874 start_wqthread + 8

com.apple.main-thread
0 libsystem_kernel.dylib 0x1b15ec8c4 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x1b15ebcc8 mach_msg + 72
2 CoreFoundation 0x186c1874c __CFRunLoopServiceMachPort + 376
3 CoreFoundation 0x186c12bd0 __CFRunLoopRun + 1176
4 CoreFoundation 0x186c12200 CFRunLoopRunSpecific + 572
5 GraphicsServices 0x19cd0d598 GSEventRunModal + 160
6 UIKitCore 0x1894d8004 -[UIApplication _run] + 1052
7 UIKitCore 0x1894dd5d8 UIApplicationMain + 164
8 libdyld.dylib 0x1868f1598 start + 4

Thread
0 libsystem_kernel.dylib 0x1b161058c __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x1cc57c5f0 _pthread_wqthread + 348
2 libsystem_pthread.dylib 0x1cc57f874 start_wqthread + 8

DTX_COMM_THREAD_8.201.1.1002
0 libsystem_kernel.dylib 0x1b160fd00 __semwait_signal + 8
1 libsystem_c.dylib 0x18f8e0714 nanosleep + 212
2 Foundation 0x187e724f4 ﹍[NSThread sleepForTimeInterval:]﹍ 152
3 Dynatrace 0x100fe3608 hidden#3786

  • 687 (_hidden#3855:687)
    4 Foundation 0x187f78c48 NSThread__start + 848
    5 libsystem_pthread.dylib 0x1cc57ab70 _pthread_start + 288
    6 libsystem_pthread.dylib 0x1cc57f880 thread_start + 8

KSCrash Exception Handler (Secondary)
0 libsystem_kernel.dylib 0x1b15ec8c4 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x1b15ebcc8 mach_msg + 72
2 libsystem_kernel.dylib 0x1b160962c thread_suspend + 80
3 Dynatrace 0x100f5e8a8 hidden#583

  • 272 (_hidden#597:272)
    4 libsystem_pthread.dylib 0x1cc57ab70 _pthread_start + 288
    5 libsystem_pthread.dylib 0x1cc57f880 thread_start + 8

KSCrash Exception Handler (Primary)
0 libsystem_kernel.dylib 0x1b15ec8c4 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x1b15ebcc8 mach_msg + 72
2 Dynatrace 0x100f5e8d4 hidden#583

  • 288 (_hidden#597:288)
    3 libsystem_pthread.dylib 0x1cc57ab70 _pthread_start + 288
    4 libsystem_pthread.dylib 0x1cc57f880 thread_start + 8

com.apple.uikit.eventfetch-thread
0 libsystem_kernel.dylib 0x1b15ec8c4 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x1b15ebcc8 mach_msg + 72
2 CoreFoundation 0x186c1874c __CFRunLoopServiceMachPort + 376
3 CoreFoundation 0x186c12bd0 __CFRunLoopRun + 1176
4 CoreFoundation 0x186c12200 CFRunLoopRunSpecific + 572
5 Foundation 0x187e1c278 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 228
6 Foundation 0x187e1c158 -[NSRunLoop(NSRunLoop) runUntilDate:] + 88
7 UIKitCore 0x1895839fc -[UIEventFetcher threadMain] + 504
8 Foundation 0x187f78c48 NSThread__start

  • 848
    9 libsystem_pthread.dylib 0x1cc57ab70 _pthread_start + 288
    10 libsystem_pthread.dylib 0x1cc57f880 thread_start + 8

Realm notification listener
0 libsystem_kernel.dylib 0x1b16114b4 kevent + 8
1 Realm 0x1028c2fe0 realm::_impl::ExternalCommitHelper::listen() + 160
2 Realm 0x1028c3ae0 realm::_impl::ExternalCommitHelper::notify_others() + 2696
3 libsystem_pthread.dylib 0x1cc57ab70 _pthread_start + 288
4 libsystem_pthread.dylib 0x1cc57f880 thread_start + 8

@voragomod
Copy link

have same issue

@ppamorim
Copy link

@AuraNica Are you able to reproduce this on 5.4.8?

@ppamorim
Copy link

@AuraNica Are you running the application with the debugger active? I notice that my write transactions are really slow when the iPhone is connected.

@AuraNica
Copy link
Author

AuraNica commented Oct 14, 2020

Are you able to reproduce this on 5.4.8?

I've waited to release yesterday a version of our app with realm 5.4.8 and I was able to see today that some users are still facing the issue.

Are you running the application with the debugger active?

I could never see the issue with the debugger attached, only on the store version.

Later edit:
After checking analytics from our app, I could see that the issue starts to happen after the app was put to background and then terminated (by the user or by the system, I don't know for sure),
And when the app starts again, realm fails to init - this happens on any version of iOS 14+, but not all users with iOS 14+ seem to have this issue.

@DShamaev
Copy link

DShamaev commented Oct 19, 2020

have the same setup and symptoms and any action that's using Realm when app is in background leads to this issue.

Realm framework version: 5.4.1 - 5.4.8
Xcode version: 11.7 and 12.0.1
iOS version: >= 14.0
Dependency manager + version: COCOAPODS 1.10.0 rc1

@AuraNica
Copy link
Author

I was able to see the error at realm init:
"Encrypted interprocess sharing is currently unsupported.DB has been opened by pid: 4848. Current pid is 5806. "

But we don't have app groups or share extension in our app, is there anything else that could cause this issue?

Do you have any idea what could have happened?

@AuraNica
Copy link
Author

Is there anyone from Realm team that could answer my question above? I'm trying to understand what is wrong in our app and how to fix or change that.

We've updated our app to Realm 10.0.0, but the issue is still here affecting most of our users with iOS 14+ when they leave the app in the background for a long time (sometimes a few hours, sometimes a day).
And the only choice they have (because the app is unusable) is to uninstall and reinstall the app again - and this is only a temporary fix, the issue reappears after awhile.

After we've updated to Realm 10.0.0 it started to reproduce on any version of the app, not just the one from store. So I can catch the error in my debugger - do you need any more details? What can I do to help you speed this process?

Thank you.

@tgoyne
Copy link
Member

tgoyne commented Oct 27, 2020

This is pointing at a problem with our workaround for iOS 14 no longer allowing us to use flock(). It sounds like in some case we think that the old process is still running when it isn't, which for unencrypted realms is mostly fine (it just stops us from doing some cleanup) but for encrypted realms isn't.

A bad workaround for this is to delete the default.realm.management/lock.fifo file when your app starts up. Note that it's important to do this before you first open the Realm, and not at a time when there's any chance of the Realm being open.

@AuraNica
Copy link
Author

AuraNica commented Oct 27, 2020

I'll give it a try, but I'm not sure what lock.fifo is, are you referring to this files?

let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
let realmURLs = [
realmURL.appendingPathExtension("lock"),
realmURL.appendingPathExtension("management")]

@DShamaev
Copy link

DShamaev commented Oct 28, 2020

@tgoyne are there any troubleshooting steps to identify place that breaks locks more precisely and probably help with a better understanding what's going on? Because in my case any action that moves application to background(switch app, minimize it, open notification center, etc) make realm database unusable on real device with iOS 14 and i couldn't understand why in my case it appears without time delays like in @AuraNica 's app.

Update: maybe it is because my application is not doing any background work and application immediately is moved into suspended state.
But then I wonder why realm is still locking the file if there are no activity related to save to/read from Realm?

@tgoyne
Copy link
Member

tgoyne commented Nov 2, 2020

I spent some time digging into this today in the hopes of reproducing it without any luck. Some of the things I tried:

  1. Put app in background then relaunch via Xcode.
  2. Force-quit app while it's suspended and relaunch via device.
  3. Have the OS kill the app due to memory pressure while it's suspended and then relaunch via device.
  4. Have a task started on transition to background exceed the time limit.
  5. Have a background fetch exceed the time limit.
  6. All of the above, but with the realm in a write transaction at the time when it's killed

Unfortunately all of these seemed to work correctly.

@AuraNica
Copy link
Author

We released a new version with the fix from above, the errors with "interprocess sharing" were gone, but I saw new ones like this: "Realm file decryption failed".
The encryption key is definitely there and is the correct one, so what is causing this issue?

The only way to fix it was to remove the entire database file and try to init realm again...but this just doesn’t seem right.

@AuraNica
Copy link
Author

We ended up removing the encryption of the database, now we only encrypt the fields that we need in keychain.

Not the best choice, but at some point in the future we will develop a watch app, so it was necessary for that, too.

@iabuilder
Copy link

@AuraNica did you resolve this bug, my app have the same issue. Does the latest version of Realm (10.5.1) resolve this bug. Thank you.

@drademacher
Copy link

drademacher commented Jan 29, 2021

We seem to have this same problem with version 10.5.0

@amritkr1947
Copy link

Hi All, We are also facing the same issue.. Do you guys have a fix yet for your apps.

@mohanrajnkl
Copy link

Hi All, I using realm in Xcode 12.4 version for iOS14, i have enabled the UIFileSharingEnabled , so the auxiliary Realm files store in Files in iPhone & iPad device , when i open iPhone & iPad files its loading its not showing auxiliary Realm files . i can't open file any files. please give any solution for me.

@mohanrajnkl
Copy link

even though realm i using the code below
autoreleasepool {
// all Realm usage here
}
do {
_ = try Realm.deleteFiles(for: Realm.Configuration.defaultConfiguration)
} catch {
// handle error
}
its never deleting auxiliary Realm files in from device. can you please help me.

@mohanrajnkl
Copy link

mohanrajnkl commented Mar 3, 2021

I using this below code, i can not delete Realm's default auxiliary files like default.ream, default.lock files, can you please help me.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask,
true)

let applicationSupportDirectoryPath = paths.first

let realmPath = applicationSupportDirectoryPath?.appending("default.realm")
Realm.Configuration.defaultConfiguration.objectTypes = objectTypes

    var realmConfig = Realm.Configuration.defaultConfiguration
    realmConfig.fileURL = URL(fileURLWithPath: realmPath!)
    realm = try! Realm(configuration: realmConfig)
        do{

            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm"))
            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm.lock"))
            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm.management"))
            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm"))
            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm.lock"))
            try FileManager.default.removeItem(atPath: oldPaths[0].appending("default.realm.management"))

        }
        catch{
            
        }
        autoreleasepool {
    }
    do {
        _ = try Realm.deleteFiles(for: Realm.Configuration.defaultConfiguration)
    } catch {
        // handle error
    }

@JituDeore
Copy link

Anybody fixed this issue?

@mohanrajnkl
Copy link

finally i got the solution
Please update xcode 12.5

Please use pod 'RealmSwift', '~> 10.5.0'

This 10.5 version only support unrealm concept

If enable UIFileSharing Enabled the files opening even the realm auxiliary files downloading

@jsflax
Copy link
Contributor

jsflax commented Aug 3, 2021

Closing this as a solution seems to have been found.

@jsflax jsflax closed this as completed Aug 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests