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 crashes when app is in the background #5731

Closed
iprabin opened this issue Aug 10, 2022 · 8 comments
Closed

Realm crashes when app is in the background #5731

iprabin opened this issue Aug 10, 2022 · 8 comments
Assignees

Comments

@iprabin
Copy link

iprabin commented Aug 10, 2022

SDK and version

SDK : realm-js
Version: 10.19.5

Observations

  • How frequent do the crash occur?
    The crash is happening on background after 15 mins
  • Does it happen in production or during dev/test?
    Dev
  • Can the crash be reproduced by you?
    Yes
  • Can you provide instructions for how we can reproduce it?
    Start realm sync when app is launched. We generate some data and start inserting data into realm. We put the app in the background and lock the phone. After 15 mins, bring the app into foreground and it will crash.

Crash log / stacktrace

0 libsystem_kernel.dylib 0x00000001cc438334 __pthread_kill + 8 (:-1)
1 libsystem_pthread.dylib 0x00000001e9ebda9c pthread_kill + 272 (pthread.c:1582)
2 libsystem_c.dylib 0x00000001a74a9b84 abort + 124 (abort.c:118)
3 AppName 0x0000000100d61684 please_report_this_issue_in_github_realm_realm_core + 12
4 AppName 0x0000000100d6196c realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits, std::__1::allocator >&) + 260
5 AppName 0x0000000100d617cc realm::util::terminate(char const*, char const*, long, std::initializer_listrealm::util::Printable&&) + 328
6 AppName 0x0000000100c1c664 realm::util::network::Service::BasicStreamOpsrealm::util::network::ssl::Stream::BufferedReadOperBase::advance() + 336
7 AppName 0x0000000100c1c078 void realm::util::network::Service::BasicStreamOpsrealm::util::network::ssl::Stream::async_buffered_read<realm::util::UniqueFunction<void (std::__1::error_code, unsigned long)> >(realm::util::net... + 88
8 AppName 0x0000000100c179dc realm::util::websocket::(anonymous namespace)::EZSocketImpl::async_read(char*, unsigned long, realm::util::UniqueFunction<void (std::__1::error_code, unsigned long)>) + 72
9 AppName 0x0000000100c28650 (anonymous namespace)::WebSocket::frame_reader_loop() + 1140
10 AppName 0x0000000100c1c818 void realm::util::network::Service::AsyncOper::do_recycle_and_execute<realm::util::UniqueFunction<void (std::__1::error_code, unsigned long)>, std::__1::error_code&, unsigned long&>(bool, realm::ut... + 148
11 AppName 0x0000000100c1c394 realm::util::network::Service::BasicStreamOpsrealm::util::network::ssl::Stream::BufferedReadOper<realm::util::UniqueFunction<void (std::__1::error_code, unsigned long)> >::recycle_and_execute() + 240
12 AppName 0x0000000100c1f250 realm::util::network::Service::Impl::run() + 456
13 AppName 0x0000000100ba3b94 realm::sync::Client::run() + 36
14 AppName 0x0000000100aeed2c void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, realm::_impl::SyncClient::SyncClient(std::__1::... + 44
15 libsystem_pthread.dylib 0x00000001e9ebcbfc _pthread_start + 320 (pthread.c:887)
16 libsystem_pthread.dylib 0x00000001e9ec5758 thread_start + 8 (:-1)

Code Sample

  • Open realm sync with config
    try { realm = await Realm.open(config); } catch (e) { console.log(e); }

  • Writing to realm
    realm.write(() => { realm.create("data", { sensor: { timestamp: data.timestamp, values: data.values, frequency: data.freq, }, partitionId:partitionId, timestamp: new Date(), userId: user.id, _id: new ObjectId(), });

@kneth
Copy link
Contributor

kneth commented Aug 11, 2022

@iprabin

Thank you for reporting. I have a couple of questions:

  • Do you only observe the crash on iOS (and not Android)?
  • If you don't put the app in the background, do you observe the crash?
  • If the app has been in the background for less 15 minutes (say 1 minute), do you still observe the crash?

If you only observe it on iOS, I am very interested to know if enabling background tasks will solve the issue?

Unfortunately it is difficult to apply the trick from Realm Swift, but we are considering to add a configuration option to relax the protection level.

@kneth kneth self-assigned this Aug 11, 2022
@iprabin
Copy link
Author

iprabin commented Aug 11, 2022

@kneth

Thank you for your response.

  1. Yes, the crash is only happening on iOS but We are not testing as heavily on android
  2. No, The app does not crash if its on the foreground
  3. We have not tested this case yet, but I will test it and update it here

Couple of things I want to clarify as well:

  1. We are using a BLE device and we need the app to run on the background
  2. The app is running for long period of time on the background
  3. We have observed multiple crashes and it all points to a thread dealing with realm sync
  4. We have background fetch, background processing and uses BLE accessories enabled on the Background Modes

I will try the Realm Swift trick and let you know if that fixes the issue.

@ironage
Copy link
Contributor

ironage commented Aug 11, 2022

This is similar to the stack trace in #5435. I suspect there is an issue in the network code. Any further information about how to reproduce this would be very valuable. @iprabin since you are able to reproduce this in debug mode, can you check the console output for the assertion failure message and provide it here?

@kneth
Copy link
Contributor

kneth commented Aug 12, 2022

We are using a BLE device and we need the app to run on the background

When investigating realm/realm-js#4525 we tried to simulate the above (the reporter's app is similar to what you describe). Unfortunately we were never to provoke a crash. We didn't implement the Realm Swift trick as it highly speculative without a reproduction case. If the trick improves the situation for you, we can implement it in Realm JavaScript and document when to use it.

@kneth
Copy link
Contributor

kneth commented Aug 21, 2022

@iprabin Did you have a chance to try the Realm Swift trick? If so, did it improve the stability of the app?

@iprabin
Copy link
Author

iprabin commented Aug 22, 2022

@kneth, I did implement the realm swift trick and i am not able to reproduce the crash anymore. I will let you know if anything else changes in the next couple of weeks. For now, I will close the issue as well. Thank you for your help.

@iprabin iprabin closed this as completed Aug 22, 2022
@kneth
Copy link
Contributor

kneth commented Aug 23, 2022

@iprabin Great news. Can I get you to describe what you did? I mean, which files did you modify, etc. It will be valuable for the next app developer who runs into the same problem.

@iprabin
Copy link
Author

iprabin commented Aug 23, 2022

@kneth I am modifying the whole directory created by realm when we first open realm sync. We no longer have the crash even when the app is running in the background for over half of the day.

Here is the swift code:

    do {
      let dir = URL(fileURLWithPath: realmPath).deletingLastPathComponent().path
      try FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication],
                                             ofItemAtPath: dir)
    }
    catch{
      print("failed")
    }

Here is the react-native code:

    const realm = await Realm.open(config);
    //call the swift code with realm path
    fixFilePermission(realm.path);

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

3 participants