Skip to content

Uncaught exception inside async #1189

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

Closed
september669 opened this issue May 14, 2019 · 1 comment
Closed

Uncaught exception inside async #1189

september669 opened this issue May 14, 2019 · 1 comment

Comments

@september669
Copy link

I think it may be related to #893

import kotlinx.coroutines.*
import java.net.UnknownHostException
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
import java.io.IOException

class RunContext: CoroutineScope{
    val scopeJob = Job()

    override val coroutineContext: CoroutineContext = Dispatchers.Default + scopeJob + CoroutineExceptionHandler { _, thr -> println("catch exception in handler"); thr.printStackTrace() }

    fun launchMain(
        block: suspend CoroutineScope.() -> Unit
    ): Job = launch {
        try {
            block()
        } catch (exc: Exception) {
            println("catch exception in launchMain")
            exc.printStackTrace()
        }
    }

    fun testExceptionAsync() = launchMain{
        println("testExceptionAsync()")
        asyncThrow().await()
    }

    fun asyncThrow(): Deferred<Int> = async {
        println("asyncThrow()")
        throw IOException()
    }

}

fun main(args: Array<String>) {
    runBlocking{
        println("runBlocking")
        RunContext().testExceptionAsync().join()
        println("runBlocking done")
    }
}

Print out:

runBlocking
testExceptionAsync()
asyncThrow()
catch exception in launchMain
java.io.IOException
	at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
catch exception in handler
java.io.IOException
	at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
runBlocking done


But if remove scopeJob from coroutineContext overriding:

override val coroutineContext: CoroutineContext = Dispatchers.Default + CoroutineExceptionHandler { _, thr -> println("catch exception in handler"); thr.printStackTrace() }

all works fine:

runBlocking
testExceptionAsync()
asyncThrow()
catch exception in launchMain
java.io.IOException
	at RunContext$asyncThrow$1.invokeSuspend(Simplest version.kt:31)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
runBlocking done

@september669
Copy link
Author

Works fine with SupervisorJob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant