Skip to content

Properly enforce flow invariant when flow is used from "suspend fun m… #1426

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 2 commits into from
Aug 9, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ internal class SafeCollector<T>(
"FlowCollector is not thread-safe and concurrent emissions are prohibited. To mitigate this restriction please use 'channelFlow' builder instead of 'flow'"
)
}
count + 1

/*
* If collect job is null (-> EmptyCoroutineContext, probably run from `suspend fun main`), then invariant is maintained
* (common transitive parent is "null"), but count check will fail, so just do not count job context element when
* flow is collected from EmptyCoroutineContext
*/
if (collectJob == null) count else count + 1
}
if (result != collectContextSize) {
error(
Expand Down
65 changes: 65 additions & 0 deletions kotlinx-coroutines-core/common/test/flow/FlowInvariantsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kotlinx.coroutines.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.intrinsics.*
import kotlin.coroutines.*
import kotlin.reflect.*
import kotlin.test.*
Expand Down Expand Up @@ -214,4 +215,68 @@ class FlowInvariantsTest : TestBase() {
}
}
}

@Test
fun testEmptyCoroutineContext() = runTest {
emptyContextTest {
map {
expect(it)
it + 1
}
}
}

@Test
fun testEmptyCoroutineContextTransform() = runTest {
emptyContextTest {
transform {
expect(it)
emit(it + 1)
}
}
}

@Test
fun testEmptyCoroutineContextViolation() = runTest {
try {
emptyContextTest {
transform {
expect(it)
kotlinx.coroutines.withContext(Dispatchers.Unconfined) {
emit(it + 1)
}
}
}
expectUnreached()
} catch (e: IllegalStateException) {
assertTrue(e.message!!.contains("Flow invariant is violated"))
finish(2)
}
}

private suspend fun emptyContextTest(block: Flow<Int>.() -> Flow<Int>) {
suspend fun collector(): Int {
var result: Int = -1
channelFlow {
send(1)
}.block()
.collect {
expect(it)
result = it
}
return result
}

val result = runSuspendFun { collector() }
assertEquals(2, result)
finish(3)
}

private suspend fun runSuspendFun(block: suspend () -> Int): Int {
val baseline = Result.failure<Int>(IllegalStateException("Block was suspended"))
var result: Result<Int> = baseline
block.startCoroutineUnintercepted(Continuation(EmptyCoroutineContext) { result = it })
while (result == baseline) yield()
return result.getOrThrow()
}
}