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

Fix various dispatcher issues #1611

Merged
merged 8 commits into from
Jan 9, 2024
Merged

Fix various dispatcher issues #1611

merged 8 commits into from
Jan 9, 2024

Conversation

cmelchior
Copy link
Contributor

@cmelchior cmelchior commented Dec 20, 2023

Close #1608

  • This PR works around a race condition with Core notifications and dispatchers which I do not think we can solve otherwise.
  • Fixes a race condition with events in a test

See also Kotlin/kotlinx.coroutines#3993

@cmelchior cmelchior marked this pull request as ready for review December 21, 2023 11:09
e.printStackTrace()
}
} catch (ex: IllegalStateException) {
// A work-around for https://github.com/realm/realm-kotlin/issues/1608
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we have had some debate on which way to organize the dispatcher/scheduler relationship (#1559 and #1463 (review)), but this seems like a very unsafe way of guarding this. We already have infrastructure in place for being able to cancel the SingleThreadDispatcherScheduler, so seems way safer to expose the cancel to the upper layers, so that we follow the exact logic on when we expect this to happen (when we close the realm?). I guess that wouldn't work if people inject their own dispatcher and just close it, but we could document that they shouldn't close the dispatcher before closing the 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.

I modified the implementation, so we now have two locks instead, one to control the dispatcher and one for the scheduler.

@cmelchior cmelchior requested a review from rorbech January 8, 2024 20:24
@cmelchior
Copy link
Contributor Author

Ready for a second round @rorbech

@@ -120,6 +120,10 @@ public class LiveRealmContext(
}

override fun close() {
// Warning: It is important to release the scheduler before closing the
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this will work if we are guaranteed the user_data_free callback from core synchronously from realm_release(scheduler). It would probably be safer for future changes with a separate abstraction of the scheduler that would set dispatcherClosing before realm_release(scheduler) ... but this is way better that the previous implementation that was relying on the message from the coroutine-exception 🙈

@cmelchior cmelchior merged commit 8046324 into main Jan 9, 2024
2 checks passed
@cmelchior cmelchior deleted the cm/fix-dispatcher-issues branch January 9, 2024 12:36
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants