-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Crash when adding object to realm #7703
Comments
Hi @sipersso Do you have any code which may help to reproduce the issue? |
It only happens very infrequently in production and I have not been able to reproduce it locally I am afraid :/ |
I noticed that the actual crash was missing from firebase so I added that: Not sure if it helps: Crashed: com.apple.main-thread |
Hi @sipersso
|
1: I don't know the size of the realm file, but this is maybe something that I could add tracking for in a future update. 2: I don't. But I can see that there are repetitive crashes. The affected have crashes within minutes of each other. 3: I don't have any global realm instances if that is what you are talking about. I wrap all calls to realm in background threads in autoreleasepools. I did see another realm thread in crashlytics if that helps in Realm notification listener. |
@sipersso could you show us how you wrap the calls the background threads in autorelease pools? |
The actual call that failed is done on the main thread and does not contain any autoreleasepools. It is not a simple proof of concept app, but an example is this:
I assume I could in some cases avoid the try! and replace it with error handling instead of crashing. Do you think it is the try! Realm() that causes the crashes? Is there anything in particular you are looking for here? |
Realms are not supported on concurrent queues, which is what let queue = DispatchQueue(label: "my-queue")
queue.async {
let realm = try! Realm(configuration: .defaultConfiguration, queue: queue)
} Would you be able to remove the usage of Realms with concurrent queues and see if that solves the issue? |
Yes, I can definitely remove the use of global(). I'll try this out. Thanks! |
By the way @leemaguire maybe this is something that should be made a little more clear in the documentation? I just read https://docs.mongodb.com/realm/sdk/swift/advanced-guides/threading/ and there is no mention there that you should avoid concurrent queues and DispatchQueue.global() and when searching on GitHub I see that you have written the same response to many open issues. Maybe something to add to the docs? |
@leemaguire FIY... I have changed all background operations to use serial queues as per your recommendation. But the issue still persists. The write operation in this case is performed on the main thread. Could that have anything to do with it? I am in the process of switching to writing on background threads, but not done with that yet. I am not using sync, only local realms for now. |
@sipersso once you read / write from the realm on the queue that it is bound to things should be fine. Do you have any additional crash logs and any new code snippets you could share with us? |
I think all of the issues here are caused by corrupted realms. The corruption seems to have happened for 10.22.0, but I don't see many new instances of these after upgrading. However, if there is corrupt data in the realm, upgrading won't help. You have to delete and reinstall the app (and thus risk loosing data, since I haven't been able to migrate to MongoDb Realm yet). Realm doesn't offer any API for detecting corruption in local files so all I can do is to wait for the user to reach out after getting multiple repeated crashes :/ I have an open support case: 00900582 and have shared more info there. (Including a realm file with corrupted data) |
Just chiming in to say that this keeps constantly happening for us also to a very large number of users with tens of thousands of crashes per day. It would be nice if this could be investigated with a high priority if at all possible! The situation is bad enough to have us start thinking about a migration strategy away from using Realm entirely in our product. We are also writing everything in main queue and the storage engine has been unstable before, but after the v10.20.2 update(s) it seems to have taken a significant turn for the worse. Currently we are using v10.24.1 and it's still experiencing these issues. |
@anlaital are you using sync? or just locally? To be fair, I think the crashes for me has been limited to t a few people on the 10.22.0 update. But I haven't seen many crashes for other users after that. The app does have quite a lot of volume with 100k+ active users. If you got repeated crashes even before the 10.22.0 update, maybe there is something wrong with your Realm usage? Tens of thousands of crashes per day sounds a lot though. Sorry you have to go through this. @anlaital do you see the same stack trace as I do? |
We are not using sync at all. Realm storage engine has always been somewhat volatile, but with the most recent updates it's just gone to a whole new level of pain. I don't think there is anything we are doing wrong, it's just that our data volumes are large as is our user base and our models are fairly complex and see heavy usage throughout the application. Here's a stack trace where simply deleting an object from the Realm causes constant crashes:
Here's one where simply adding an object to Realm causes it to blow up constantly:
|
HI @anlaital which version of Realm are you using at the moment? |
@dianaafanador3 we are using Realm v10.24.1 on iOS, which is the platform experiencing these crashes. Android has no crashes whatsoever and they are using an older Java Realm version v10.5.1. |
We added some fixes for some data corruption crashes on large files on |
Updating won't help correct the realms that already have corruption though. There is also no API for detecting corruption. If there is corrupt data, realm will just crash because of an assertion and there is nothing you can do about it except for deleting the realm. In my case I was able to get a hold of one user that had the problem and extract a realm file. Using Realm.writeCopy didn't work since it would also crash. But using FileManager I was able to give him a special build that didn't use any realm apis, extract the file and remove the corrupted entries manually using Realm Studio. The user won't be able to use the corrupted realm, so delete and reinstall (with possible data loss) seems to be the only option here. |
Is there any way to detect corruption? I am struggling with the error logs coming up without being able to reproduce it. |
@shimastripe I don't think you won't be able to reproduce this. The transaction causing corrupted data could have happened anywhere. I am seeing fewer issues now than I did in the 10.22.0 update, but I still have customers reaching out every week with realms that are corrupted :/ I don't know if that is because they were using the 10.22.0 update and then waited to reach out or if this is still an issue. I was in contact with support and since Realm is using hard asserts when bad data is discovered I can not provide the user with an error dialog informing them that there is a problem with their data. The ticket was basically closed as a won't fix as the app probably won't function anyway with corrupt data in there and the assertion would be justified. But it does lead to a bad user experience. All I can do is wait for the negative reviews and angry customers to reach out :( |
Still getting crashes here. It is rare but it happens. And for those that have the issue, it happens repetitively. Here is a new stack trace
|
Hello, I'm getting these crashes pretty often
Sometimes it crashes and indicates at line db.add($0) - not 'catch' block but can it be related to situation when object with same id exists already? |
How frequently does the bug occur?
Sometimes
Description
Realm seems to crash for some users when adding new objects to realm
I have seen similar issues since 10.22.0, but this is also happening since the 10.24.0 upgrade.
Stacktrace & log output
Can you reproduce the bug?
Not yet
Reproduction Steps
No response
Version
10.24.0
What SDK flavour are you using?
Local Database only
Are you using encryption?
No, not using encryption
Platform OS and version(s)
iOS 15
Build environment
Xcode version: 13.2.1
Dependency manager and version: SPM
The text was updated successfully, but these errors were encountered: