Skip to content

Do not propagate exceptions to CoroutineExceptionHandler in 'future' … #3580

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 16, 2023
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
10 changes: 6 additions & 4 deletions kotlinx-coroutines-core/jdk8/src/future/Future.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ private class CompletableFutureCoroutine<T>(
}

override fun onCancelled(cause: Throwable, handled: Boolean) {
if (!future.completeExceptionally(cause) && !handled) {
// prevents loss of exception that was not handled by parent & could not be set to CompletableFuture
handleCoroutineException(context, cause)
}
/*
* Here we can potentially lose the cause if the failure is racing with future's
* external cancellation. We are consistent with other future implementations
* (LF, FT, CF) and give up on such exception.
*/
future.completeExceptionally(cause)
}
}

Expand Down
6 changes: 1 addition & 5 deletions kotlinx-coroutines-core/jvm/test/jdk8/future/FutureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,7 @@ class FutureTest : TestBase() {
}

@Test
fun testUnhandledExceptionOnExternalCompletion() = runTest(
unhandled = listOf(
{ it -> it is TestException } // exception is unhandled because there is no parent
)
) {
fun testUnhandledExceptionOnExternalCompletionIsNotReported() = runTest {
expect(1)
// No parent here (NonCancellable), so nowhere to propagate exception
val result = future(NonCancellable + Dispatchers.Unconfined) {
Expand Down