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

Frozen Realm doesn't reference count correctly #7504

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alecbarber
Copy link

Creating new instances of an already-cached version doesn't increment the reference count, which causes closing frozen Realm instances to be non-deterministic. The following code works as expected:

val realm = Realm.getDefaultInstance()

// Freeze the Realm
val realm1 = realm.freeze()
// Write to the live Realm, and freeze again
realm.executeTransaction { ... }
val realm2 = realm.freeze()

// Close the first frozen instance
realm1.close()

// Make a query on the second frozen instance
realm2.where<...>().findAll().doSomething()

// Clean up
realm2.close()
realm.close()

However, this code throws an IllegalStateException if the write operation doesn't happen between the two freeze operations:

val realm = Realm.getDefaultInstance()

// Freeze the Realm
val realm1 = realm.freeze()
// Freeze a second time first, then write to the live Realm
val realm2 = realm.freeze()
realm.executeTransaction { ... }

// Close the first frozen instance.
realm1.close()

// Make a query on the second frozen instance
// Throws IllegalStateException, saying that realm2 is closed.
realm2.where<...>().findAll().doSomething()

// Clean up
realm2.close()
realm.close()

In a use-case where frozen RealmObjects/RealmResults are being temporarily created and there are concurrent write transactions (which is for example very easy to achieve with Kotlin Coroutines), cleaning up frozen Realms rapidly becomes non-deterministic.

This PR attempts to fix the issue by making the reference-counting behaviour of frozen Realms match that of live Realms – that is, every Realm instance created with Realm.freeze() is active (and using resources) until close() is called on that instance.

@bmunkholm
Copy link
Contributor

@cla-bot check

@cla-bot
Copy link

cla-bot bot commented Feb 22, 2022

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: alecbarber.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@cla-bot
Copy link

cla-bot bot commented Feb 22, 2022

The cla-bot has been summoned, and re-checked this pull request!

@alecbarber alecbarber force-pushed the patch/frozen-realm-reference-counting branch from ab0a5f0 to 84bbbcb Compare February 22, 2022 23:36
@cla-bot cla-bot bot added the cla: yes label Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants