realm-swift: Started experiencing SIGKILL crashes very frequently after iOS 15 launch
How frequently does the bug occur?
Sometimes
Description
We’re having a ton of crash reports appearing after iOS 15 release that have not been present before. All of them are SIGKILL crashes and stack traces have nothing nut realm activity. Is this something that is a known issue?
Stacktrace & log output
Multiple different stack traces
Can you reproduce the bug?
Yes, sometimes
Reproduction Steps
The iOS will kill the app eventually
Version
10.7.6
What SDK flavour are you using?
Local Database only
Are you using encryption?
Yes, using encryption
Platform OS and version(s)
iOS 15
Build environment
Xcode version: … Dependency manager and version: …
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 31 (7 by maintainers)
I’ve understood and solved the issue for our app:
TL;DR: requesting a
RLMRealmvia any of therealmWithConfigurationconstructors (exlet realm = try RLMRealm.init(configuration: configuration) must always be wrapped in a background task, since if the app is suspended while that constructor is being executed, the app will crash with aSIGKILLon an idle main thread. Any usage of thatRLMRealmmust also be wrapped.Explanation: The issue is that (as of iOS 15) if you are holding a file lock in a shared app container when the app is suspended, the OS kills the app. This is regardless of other running processes. It’s a protection mechanism to prevent deadlocks in other processes that might access the file. (This information comes from here (https://developer.apple.com/forums/thread/655225?answerId=632433022#632433022) and here (https://githubhot.com/repo/realm/realm-swift/issues/7649))
I was able to repro the crash behaviour with a simple prototype app, requesting a
RLMRealmin a shared container, by inserting a sleep between the application going to background, requesting theRLMRealm, and being suspended. With the right sleep timing, which forces theRLMRealmto be created as the app is being suspended, the crash is 100% reproducible.We solved this issue with the following
RLMRealmwrapper:It can then be used as follows: the background task lasts until the
RLMRealminstance is deallocated:Important note: if you don’t wrap up your Realm activity before the background task expiration handler is called (~30 seconds after backgrounding), you’ll crash, for the same reason as before. This gives you time to wrap up what you are doing, but doesn’t fix that you can’t hold a file lock when the app is suspended.