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

Install Dispatchers.Default on native when creating new context for coroutine builders when there is no ContinuationInterceptor there #4075

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions kotlinx-coroutines-core/common/test/CoroutineScopeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ class CoroutineScopeTest : TestBase() {
finish(7)
}

@Test
fun testNewCoroutineContextDispatcher() {
fun newContextDispatcher(c1: CoroutineContext, c2: CoroutineContext) =
ContextScope(c1).newCoroutineContext(c2)[ContinuationInterceptor]

assertSame(Dispatchers.Default, newContextDispatcher(EmptyCoroutineContext, EmptyCoroutineContext))
assertSame(Dispatchers.Default, newContextDispatcher(EmptyCoroutineContext, Dispatchers.Default))
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Default, EmptyCoroutineContext))
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Default, Dispatchers.Default))
assertSame(Dispatchers.Default, newContextDispatcher(Dispatchers.Unconfined, Dispatchers.Default))
assertSame(Dispatchers.Unconfined, newContextDispatcher(Dispatchers.Default, Dispatchers.Unconfined))
assertSame(Dispatchers.Unconfined, newContextDispatcher(Dispatchers.Unconfined, Dispatchers.Unconfined))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we also need to check the user-visible bug directly, like what you have in your reproducer:

    runTest {
        val job = Job()
        val scope = CoroutineScope(job + coroutineContext[CoroutineExceptionHandler]!!)
        scope.launch(Dispatchers.Default) {
            assertSame(Dispatchers.Default, coroutineContext[ContinuationInterceptor])
        }
        scope.launch {
            assertSame(Dispatchers.Default, coroutineContext[ContinuationInterceptor])
        }
        job.complete()
        job.join()
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, added additional test with launch


@Test
fun testScopePlusContext() {
assertSame(EmptyCoroutineContext, scopePlusContext(EmptyCoroutineContext, EmptyCoroutineContext))
Expand Down
4 changes: 2 additions & 2 deletions kotlinx-coroutines-core/native/src/CoroutineContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ internal actual val DefaultDelay: Delay = DefaultExecutor

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
val combined = coroutineContext + context
return if (combined !== DefaultDelay && combined[ContinuationInterceptor] == null)
combined + (DefaultDelay as CoroutineContext.Element) else combined
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
combined + Dispatchers.Default else combined
}

public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {
Expand Down