Skip to content
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

Stacktrace recovery doesn't work for suspend main #1328

Closed
opensource21 opened this issue Jul 9, 2019 · 6 comments
Closed

Stacktrace recovery doesn't work for suspend main #1328

opensource21 opened this issue Jul 9, 2019 · 6 comments
Assignees
Labels
Milestone

Comments

@opensource21
Copy link

opensource21 commented Jul 9, 2019

I took the example https://raw.githubusercontent.com/Kotlin/kotlinx.coroutines/master/kotlinx-coroutines-debug/test/RecoveryExample.kt and add at line 16 a println("${Thread.currentThread().name}"), so I get

package example

import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async

object PublicApiImplementation : CoroutineScope by CoroutineScope(CoroutineName("Example")) {

    private fun doWork(): Int {
        error("Internal invariant failed")
    }

    private fun asynchronousWork(): Int {
        println("${Thread.currentThread().name}")
        return doWork() + 1
    }

    public suspend fun awaitAsynchronousWorkInMainThread() {
        val task = async(Dispatchers.Default) {
            asynchronousWork()
        }

        task.await()
    }
}

suspend fun main() {
    // Try to switch debug mode on and off to see the difference
    PublicApiImplementation.awaitAsynchronousWorkInMainThread()
}

I run it with

  1. -ea
  2. -Dkotlinx.coroutines.debug -Dkotlinx.coroutines.stacktrace.recovery=true
  3. -Dkotlinx.coroutines.debug

I never get an recovered stacktrace. How ever I got
DefaultDispatcher-worker-1 @Example#1
so -Dkotlinx.coroutines.debug seems to work.

What does I'm missing?
Here my version
kotlin 1.3.41
coroutines.version 1.2.2

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Jul 9, 2019

Thanks for the report!
It appears like a missing recovery in one of the await branches, will investigate further

@opensource21
Copy link
Author

@qwwdfsad do you know a version where it works. Would be nice to see this feature in action.

@opensource21
Copy link
Author

I looked in org/jetbrains/kotlinx/kotlinx-coroutines-core/1.2.2/kotlinx-coroutines-core-1.2.2-sources.jar!/commonMain/JobSupport.kt:1131. It seems that the state Empty{Active} isn't the expected one.

@qwwdfsad qwwdfsad self-assigned this Jul 15, 2019
@qwwdfsad
Copy link
Collaborator

@opensource21 it works in the latest version except for your particular case (with suspending await).
I will fix your problem in 1.3.0

@qwwdfsad
Copy link
Collaborator

As a workaround, you can use fun main() = runBlocking { ... } instead of suspend fun main()

@elizarov elizarov added this to the 1.3 milestone Jul 16, 2019
qwwdfsad added a commit that referenced this issue Jul 16, 2019
…s in 'suspend fun main' cases to further improve user experience

Fixes #1328
@qwwdfsad qwwdfsad changed the title Stacktrace recovery doesn't work Stacktrace recovery doesn't work for suspend main Jul 16, 2019
@opensource21
Copy link
Author

If you clone https://gitlab.com/ppiag/zulipbot, rename application.properties.sample to application.properties and replace zulip.url=https://####.zulipchat.com with zulip.url=https://www.zulipchat.com and start BotRunner , I guess you have a different situation with ktor and it doesn't work. Unfortunately it's not so easy to debug :-(

Any idea when 1.3 will be released or a beta for a test?

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

No branches or pull requests

3 participants