Skip to content

Commit ba87db3

Browse files
authored
[native-mt] Fix the errornous isDispatchNeeded check in Dispatchers.Main.immediate on Darwin platforms. (#2476)
Fixes #2283
1 parent cb19b9a commit ba87db3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private class DarwinMainDispatcher(
2828

2929
init { freeze() }
3030

31-
override fun isDispatchNeeded(context: CoroutineContext): Boolean = !invokeImmediately || isMainThread()
31+
override fun isDispatchNeeded(context: CoroutineContext): Boolean = !(invokeImmediately && isMainThread())
3232

3333
override fun dispatch(context: CoroutineContext, block: Runnable) {
3434
dispatch_async(dispatch_get_main_queue()) {

kotlinx-coroutines-core/nativeDarwin/test/MainDispatcherTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44

55
package kotlinx.coroutines
66

7+
import kotlin.coroutines.*
78
import kotlin.test.*
89

910
class MainDispatcherTest : TestBase() {
1011
private val testThread = currentThread()
1112

13+
@Test
14+
fun testDispatchNecessityCheckWithMainImmediateDispatcher() {
15+
runTest {
16+
val immediate = Dispatchers.Main.immediate
17+
val needsDispatch = testThread != mainThread
18+
assertEquals(needsDispatch, immediate.isDispatchNeeded(EmptyCoroutineContext))
19+
}
20+
}
21+
1222
@Test
1323
fun testWithContext() {
1424
if (testThread == mainThread) return // skip if already on the main thread

0 commit comments

Comments
 (0)