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

RJS-2784: Clear internal state to avoid crashes when React Native app is reloaded #6612

Merged
merged 4 commits into from
Apr 17, 2024

Conversation

kneth
Copy link
Contributor

@kneth kneth commented Apr 12, 2024

What, How & Why?

This closes #6579

During development it is common to reload a React Native app. As it is a rough process, it is important to clear the previous state - including listeners. Realm Core provides some low-level method to clear many internal data structures, and by invoking these when the SDK is initialized (loaded in the app), any state from the previous app instance is removed.

Testing is difficult, and I have only tried it manually in a app running locally on my computer.

☑️ ToDos

  • 📝 Changelog entry
  • 📝 Compatibility label is updated or copied from previous entry
  • 📝 Update COMPATIBILITY.md
  • 🚦 Tests
  • 📦 Updated internal package version in consuming package.jsons (if updating internal packages)
  • 📱 Check the React Native/other sample apps work if necessary
  • 💥 Breaking label has been applied or is not necessary

//Set default logger and log level.
// Clear the internal state to prevent crashes when reloading on React Native
binding.RealmCoordinator.clearAllCaches();
binding.App.clearCachedApps();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach 👍 I suppose there's a time between the runtime being torn down until this code runs, where there could potentially fire an event which would crash the app 🤔 But ... it's probably a lot better than nothing.

I wonder if it would make sense to simply call the Realm.shutdown instead? That would also cancel outstanding async open tasks, etc.

Also, since this is relevant only for React Native, it might make sense to move it into the platform specific wrapper:

import { Realm } from "../../Realm";
export = Realm;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach

No, just a hack 😄

simply call the Realm.shutdown instead

I had an issue with Realm.shutdown since RealmProgressPromise is undefined.

move it into the platform specific wrapper

I agree.

Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this as well with the RN test app (on iOS and Android) and it seems to work. I'm not all that familiar with the ins and outs of this change so I'll leave it up to Kraen to approve 👍

CHANGELOG.md Outdated Show resolved Hide resolved
@kneth
Copy link
Contributor Author

kneth commented Apr 17, 2024

Coverage tests crashed with "free(): invalid pointer", and we should investigate it

@kneth kneth merged commit 724540c into main Apr 17, 2024
30 of 31 checks passed
@kneth kneth deleted the kneth/fix/reload branch April 17, 2024 08:40
papafe added a commit that referenced this pull request Apr 24, 2024
* main:
  Installing dependencies again after checkout
  [12.7.1] Bump version (#6632)
  RJS-2806: Upgrade to Realm Core v14.5.2 (#6629)
  RJS-2804: remove incorrect privacy manifest for iOS (#6627)
  Adding permissions fix "Resource not accessible by integration"
  RJS-2143: Tests to clarify number conversion (#6623)
  Fixing unstable test (#6625)
  Adding a script to update submodules
  Update to the new documentation URL (#6597)
  Prepare for vNext
  [12.7.0] Bump version (#6622)
  RJS-2757: Add Apple Privacy Manifest (#6605)
  RJS-2784: Clear internal state to avoid crashes when React Native app is reloaded (#6612)
  Use CMake 3.29.2 (#6619)
  RJS-2801: Upgrade to Realm Core v14.5.1 (#6611)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

React Native Android application crashing when using syncSession.addConnectionNotification
3 participants