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
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3603,18 +3603,41 @@ actual object RealmInterop {
}

override fun notify(work_queue: CPointer<realm_work_queue_t>?) {
scope.launch {
try {
printlntid("on dispatcher")
lock.withLock {
if (!cancelled) {
realm_wrapper.realm_scheduler_perform_work(work_queue)
try {
scope.launch {
try {
printlntid("on dispatcher")
lock.withLock {
if (!cancelled) {
realm_wrapper.realm_scheduler_perform_work(work_queue)
}
}
} catch (e: Exception) {
// Should never happen, but is included for development to get some indicators
// on errors instead of silent crashes.
e.printStackTrace()
}
} catch (e: Exception) {
// Should never happen, but is included for development to get some indicators
// on errors instead of silent crashes.
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.

// As the Core listeners are separated from Coroutines, there is a chance
// that we have closed the Kotlin dispatcher while Core is in the process
// of sending notifications. If this happens we might end up in this
// `notify` method with the dispatcher being closed.
//
// As the ClosableDispatcher does not expose a `isClosed` state, it means
// there is no way for us to detect if it is safe to launch a task using
// it. So as a last resort we are catching the error here and ignore it.
//
// This should be safe as we are only closing the dispatcher when closing the
// Realm anyway.
//
// Note, JVM and Native behave differently on this. See this issue for more
// details: https://github.com/Kotlin/kotlinx.coroutines/issues/3993
if (ex.message?.contains("was closed, attempted to schedule") == false) {
throw ex
} else {
ex.printStackTrace()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ class RealmNotificationsTests : FlowableTests {
realm.write { /* Do nothing */ }
val nextVersion = realm.version()

c1.receiveOrFail(message = "Failed to receive update event on Channel 1").let { realmChange ->
rorbech marked this conversation as resolved.
Show resolved Hide resolved
assertIs<UpdatedRealm<Realm>>(realmChange)
assertEquals(nextVersion, realmChange.realm.version())
}

val observer2 = async {
realm.asFlow().collect {
c2.send(it)
}
}

c1.receiveOrFail(message = "Failed to receive update event on Channel 1").let { realmChange ->
assertIs<UpdatedRealm<Realm>>(realmChange)
assertEquals(nextVersion, realmChange.realm.version())
}
c2.receiveOrFail(message = "Failed to receive initial event on Channel 2").let { realmChange ->
assertIs<InitialRealm<Realm>>(realmChange)
assertEquals(nextVersion, realmChange.realm.version())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ import kotlinx.coroutines.CloseableCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlin.random.Random
import kotlin.random.nextUInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
import kotlin.time.Duration.Companion.seconds

fun totalThreadCount() = Thread.getAllStackTraces().size
Expand All @@ -43,6 +47,18 @@ fun totalThreadCount() = Thread.getAllStackTraces().size
*/
class RealmTests {

// Test for https://github.com/Kotlin/kotlinx.coroutines/issues/3993
@Test
fun closingDispatchersThrowIllegalState() {
cmelchior marked this conversation as resolved.
Show resolved Hide resolved
val dispatcher = singleThreadDispatcher("test-${Random.nextUInt()}")
dispatcher.close()
runBlocking {
launch(dispatcher) {
fail("Dispatcher was running")
}
}
}

@Test
fun cleanupDispatcherThreadsOnClose() = runBlocking {
val tmpDir = PlatformUtils.createTempDir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ import io.realm.kotlin.internal.platform.singleThreadDispatcher
import io.realm.kotlin.test.platform.NsQueueDispatcher
import io.realm.kotlin.test.platform.PlatformUtils
import io.realm.kotlin.test.util.Utils.printlntid
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import platform.CoreFoundation.CFRunLoopRun
import platform.Foundation.NSNumber
import platform.darwin.DISPATCH_QUEUE_PRIORITY_BACKGROUND
import platform.darwin.dispatch_get_global_queue
import kotlin.random.Random
import kotlin.random.nextUInt
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.fail

/**
* Various coroutine tests to track if basic dispatching, etc. works.
Expand Down Expand Up @@ -89,4 +96,22 @@ class CoroutineTests {
assertEquals(tid, PlatformUtils.threadId())
}
}

// Test for https://github.com/Kotlin/kotlinx.coroutines/issues/3993
@Test
fun closingDispatchersThrowIllegalState() {
val dispatcher = singleThreadDispatcher("test-${Random.nextUInt()}")
dispatcher.close()
try {
runBlocking {
launch(dispatcher) {
fail("Dispatcher was running")
}
}
fail("No error was thrown")
} catch (ex: IllegalStateException) {
assertFalse(ex is CancellationException, "Was: ${ex::class}")
assertTrue(ex.message!!.contains("was closed, attempted to schedule"), ex.message)
}
}
}