Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
13.07k stars 1.85k forks source link

Exception while trying to handle coroutine exception #2123

Closed Aazu07 closed 2 years ago

Aazu07 commented 4 years ago

I am getting an exception from Coroutine exception handler. It say ClassCastException as java.lang.InternalError cannot be cast to java.lang.Exception

I am unable to understand what might cause this.

Fatal Exception: java.lang.RuntimeException: Exception while trying to handle coroutine exception
       at kotlinx.coroutines.CoroutineExceptionHandlerKt.handlerException(CoroutineExceptionHandlerKt.java:38)
       at kotlinx.coroutines.CoroutineExceptionHandlerKt.handleCoroutineException(CoroutineExceptionHandlerKt.java:29)
       at kotlinx.coroutines.StandaloneCoroutine.handleJobException(StandaloneCoroutine.java:184)
       at kotlinx.coroutines.JobSupport.tryFinalizeFinishingState(JobSupport.java:226)
       at kotlinx.coroutines.JobSupport.tryMakeCompletingSlowPath(JobSupport.java:849)
       at kotlinx.coroutines.JobSupport.tryMakeCompleting(JobSupport.java:811)
       at kotlinx.coroutines.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.java:787)
       at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.java:111)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:46)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:241)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:594)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.java:60)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:740)
Caused by java.lang.ClassCastException: java.lang.InternalError cannot be cast to java.lang.Exception
       at com.package.in.viewmodels.CommonSearchActivityViewModel$$special$$inlined$CoroutineExceptionHandler$1.handleException(CommonSearchActivityViewModel.java:85)
       at kotlinx.coroutines.CoroutineExceptionHandlerKt.handleCoroutineException(CoroutineExceptionHandlerKt.java:25)
       at kotlinx.coroutines.StandaloneCoroutine.handleJobException(StandaloneCoroutine.java:184)
       at kotlinx.coroutines.JobSupport.tryFinalizeFinishingState(JobSupport.java:226)
       at kotlinx.coroutines.JobSupport.tryMakeCompletingSlowPath(JobSupport.java:849)
       at kotlinx.coroutines.JobSupport.tryMakeCompleting(JobSupport.java:811)
       at kotlinx.coroutines.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.java:787)
       at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.java:111)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:46)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:241)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:594)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.java:60)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:740)`

The code for Coroutine handler is as follows

private val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception ->
        isLoading.postValue(false)
        GlobalErrorUtils.handleGenericError(
                applicationContext = applicationContext,
                exception = exception as Exception,
                logNonFatalException = true,
                displayToastInDebug = true
        )
        GlobalLoggerUtils.showLog("COUROUTINES", "SocketTimeOutException")
        exception.printStackTrace()
        job = Job()
    }
qwwdfsad commented 4 years ago

Thanks for the report! Could you please clarify what exact line in your snippet is CommonSearchActivityViewModel.java:85?

Caused by java.lang.ClassCastException: java.lang.InternalError cannot be cast to java.lang.Exception

Usually, such errors indicate a bug in the Kotlin compiler, but it still would be nice to see where java.lang.InternalError comes from. As a workaround, if it is really a compiler bug, you could try the following snippet:

public val ceh2: CoroutineExceptionHandler = object : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler {
    override fun handleException(context: CoroutineContext, exception: Throwable) {
        isLoading.postValue(false)
        GlobalErrorUtils.handleGenericError(
                applicationContext = applicationContext,
                exception = exception as Exception,
                logNonFatalException = true,
                displayToastInDebug = true
        )
        GlobalLoggerUtils.showLog("COUROUTINES", "SocketTimeOutException")
        exception.printStackTrace()
        job = Job()
    }
}

Is it possible to extract self-contained reproducer (snippet I can copy-paste and run in isolation) from your code?

Aazu07 commented 4 years ago

The issue is that its not reproducible at our end, we got this stack trace from Crashlytics. And CommonSearchActivityViewModel.kt class ends before line no 85 so don't know exact line where its happening.

qwwdfsad commented 4 years ago

And CommonSearchActivityViewModel.kt class ends before line no 85 so don't know exact line where its happening.

Could you please copy the whole stracktrace to the clipboard and use Analyze -> Stack Trace or Thread dump action in IDEA, then just click on that frame and see where it navigates? Line-numbers are messed up because of inline functions, but IDEA/Android Studio should properly map them to the source

Aazu07 commented 4 years ago

Its giving the same line i.e line after end of file. The whole file is here but there are many internal references so you might not be able to create a file and use it.

Aazu07 commented 4 years ago

I have installed kotlin to java decompiler and according to that the line number 85 points to a method in a suspend function. Its some computation on string. I guess that must be causing an Error which is then thrown to CoroutineExceptionHandler.