-
Notifications
You must be signed in to change notification settings - Fork 576
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
Fix leaking listeners #6552
Fix leaking listeners #6552
Conversation
Realm welcomes all contributions! The only requirement we have is that, like many other projects, we need to have a Contributor License Agreement (CLA) in place before we can accept any external code. Our own CLA is a modified version of the Apache Software Foundation’s CLA. Our records show that CLA has not been signed by @bimusiek. Please submit your CLA electronically using our Google form so we can accept your submissions. After signing the CLA you can recheck this PR with a |
@cla-bot check |
The cla-bot has been summoned, and re-checked this pull request! |
Hey @bimusiek thank you so much for the contribution. We'll try and get this reviewed shortly, but in the meantime, I was wondering if you have considered an approach similar to C#'s cancellation tokens - namely setting a flag that can be checked in the let registrationState: "registered" | "torn-down" | "pending" = "pending";
// ...
if (realm.isInTransaction) {
setImmediate(() => {
if (registrationState == "pending") {
collection.addListener(listenerCallback, keyPaths);
registrationState = "registered"
}
});
}
// ...
const tearDown = () => {
// ...
if (registrationState == "registered") {
collection.removeListener(listenerCallback);
}
registrationState = "torn-down";
}; |
@nirinchev This is good idea, I will implement it soon. We have release the app to production with this change and it helped a lot. No more memory leaks due to listener callbacks leaks. However, the impact of this change was lesser than we thought, as we had another hook that was re-rendering a lot. Which triggered this scenario of leaking listener callback. So at the end, this issue should not have huge impact. The re-rendering issue we had, was related to For now, we have patched react typings to disallow |
@bimusiek, we've added your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I have tested locally (since GHA doesn't spin up the server), and I see the same result as testing on main
.
(cherry picked from commit 2fe5240)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution @bimusiek! We will link to your profile in the CHANGELOG entry for this fix 👍
Co-authored-by: LJ <[email protected]>
What, How & Why?
If Realm is inTransaction and listener is created in the
setImmediate
, it won't be removed from listeners if tearDown is called quickly (which happens often if one listener triggers re-render of other listeners)This closes #1099 (comment)
☑️ ToDos
Compatibility
label is updated or copied from previous entryCOMPATIBILITY.md
package.json
s (if updating internal packages)Breaking
label has been applied or is not necessary