Skip to content

Promote CoroutineStart.UNDISPATCHED to non experimental API #2505

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

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions kotlinx-coroutines-core/common/src/CoroutineStart.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines

Expand Down Expand Up @@ -58,8 +58,8 @@ public enum class CoroutineStart {
ATOMIC,

/**
* Immediately executes the coroutine until its first suspension point _in the current thread_ as if the
* coroutine was started using [Dispatchers.Unconfined]. However, when the coroutine is resumed from suspension
* Immediately executes the coroutine until its first suspension point _in the current thread_ similarly to
* the coroutine being started using [Dispatchers.Unconfined]. However, when the coroutine is resumed from suspension
* it is dispatched according to the [CoroutineDispatcher] in its context.
*
* This is similar to [ATOMIC] in the sense that coroutine starts executing even if it was already cancelled,
Expand All @@ -68,9 +68,11 @@ public enum class CoroutineStart {
* Cancellability of coroutine at suspension points depends on the particular implementation details of
* suspending functions as in [DEFAULT].
*
* **Note: This is an experimental api.** Execution semantics of coroutines may change in the future when this mode is used.
* ### Unconfined event loop
*
* Unlike [Dispatchers.Unconfined] and [MainCoroutineDispatcher.immediate], nested undispatched coroutines do not form
* an event loop that otherwise prevents potential stack overflow in case of unlimited nesting.
*/
@ExperimentalCoroutinesApi // Since 1.0.0, no ETA on stability
UNDISPATCHED;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines
Expand Down Expand Up @@ -31,6 +31,38 @@ class AtomicCancellationCommonTest : TestBase() {
expect(3)
}

@Test
fun testUndispatchedLaunch() = runTest {
expect(1)
assertFailsWith<CancellationException> {
withContext(Job()) {
cancel()
launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
yield()
expectUnreached()
}
}
}
finish(3)
}

@Test
fun testUndispatchedLaunchWithUnconfinedContext() = runTest {
expect(1)
assertFailsWith<CancellationException> {
withContext(Dispatchers.Unconfined + Job()) {
cancel()
launch(start = CoroutineStart.UNDISPATCHED) {
expect(2)
yield()
expectUnreached()
}
}
}
finish(3)
}

@Test
fun testDeferredAwaitCancellable() = runTest {
expect(1)
Expand Down Expand Up @@ -122,4 +154,4 @@ class AtomicCancellationCommonTest : TestBase() {
yield() // now yield
finish(4)
}
}
}