Skip to content

Do not report already handled exception in select builder #1436

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
Aug 14, 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
15 changes: 11 additions & 4 deletions kotlinx-coroutines-core/common/src/selects/Select.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,17 @@ internal class SelectBuilderImpl<in R>(
internal fun handleBuilderException(e: Throwable) {
if (trySelect(null)) {
resumeWithException(e)
} else {
// Cannot handle this exception -- builder was already resumed with a different exception,
// so treat it as "unhandled exception"
handleCoroutineException(context, e)
} else if (e !is CancellationException) {
/*
* Cannot handle this exception -- builder was already resumed with a different exception,
* so treat it as "unhandled exception". But only if it is not the completion reason
* and it's not the cancellation. Otherwise, in the face of structured concurrency
* the same exception will be reported to theglobal exception handler.
*/
val result = getResult()
if (result !is CompletedExceptionally || unwrap(result.cause) !== unwrap(e)) {
handleCoroutineException(context, e)
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions kotlinx-coroutines-core/jvm/test/flow/CombineStressTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.flow

import kotlinx.coroutines.*
import org.junit.*

class CombineStressTest : TestBase() {

@Test
public fun testCancellation() = runTest {
withContext(Dispatchers.Default + CoroutineExceptionHandler { _, _ -> expectUnreached() }) {
flow {
expect(1)
repeat(1_000 * stressTestMultiplier) {
emit(it)
}
}.flatMapLatest {
combine(flowOf(it), flowOf(it)) { arr -> arr[0] }
}.collect()
finish(2)
reset()
}
}

@Test
public fun testFailure() = runTest {
val innerIterations = 100 * stressTestMultiplierSqrt
val outerIterations = 10 * stressTestMultiplierSqrt
withContext(Dispatchers.Default + CoroutineExceptionHandler { _, _ -> expectUnreached() }) {
repeat(outerIterations) {
try {
flow {
expect(1)
repeat(innerIterations) {
emit(it)
}
}.flatMapLatest {
combine(flowOf(it), flowOf(it)) { arr -> arr[0] }
}.onEach {
if (it >= innerIterations / 2) throw TestException()
}.collect()
} catch (e: TestException) {
expect(2)
}
finish(3)
reset()
}
}
}
}