question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

withClue() fails with EmptyStackException if a coroutine switches threads

See original GitHub issue

Version: kotest-assertions-core:4.6.2

As described in the coroutine docs on thread-local data, a coroutine may switch threads at suspension points.

withClue() relies on thread-local data via ThreadLocalErrorCollector, without taking coroutine thread-switching into account. It fails if its block contains a coroutine resuming on a different thread.

Example

import io.kotest.assertions.withClue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test

class TestCase {
    @Test
    fun `coroutine changing threads`() = runBlocking(Dispatchers.Unconfined) {
        withClue("Hello") {
            Thread.currentThread().run { println("withClue() block begins on $name, id $id") }
            delay(10)  // First suspension makes the Unconfined dispatcher resume on a different thread
            Thread.currentThread().run { println("withClue() block ends   on $name, id $id") }
        }
    }
}
Build script
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.5.30"
    application
}

group = "me.oliver"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1")
    testImplementation(kotlin("test"))
    testImplementation("io.kotest:kotest-assertions-core:4.6.2")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "11"
}

application {
    mainClass.set("MainKt")
}
Result
> Task :test FAILED
withClue() block begins on Test worker @coroutine#1, id 12
withClue() block ends   on kotlinx.coroutines.DefaultExecutor @coroutine#1, id 15

java.util.EmptyStackException
	at java.base/java.util.Stack.peek(Stack.java:102)
	at java.base/java.util.Stack.pop(Stack.java:84)
	at io.kotest.assertions.ThreadLocalErrorCollector.popClue(ErrorCollector.kt:32)
	at TestCase$coroutine changing threads$1.invokeSuspend(TestCase.kt:23)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:32 (32 by maintainers)

github_iconTop GitHub Comments

2reactions
jschneidereitcommented, Sep 12, 2021

Sorry, I had some things come up. I can provide code review though.

1reaction
sksamuelcommented, Aug 31, 2021

Yes we’ve mullled over this jim and also doing the same for soft assertions mode

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow clues to be added to timeouts · Issue #2230 - GitHub
sksamuel mentioned this issue on Aug 31, 2021. withClue() fails with EmptyStackException if a coroutine switches threads #2447.
Read more >
How to Fix the Empty Stack Exception in Java - Rollbar
The EmptyStackException is a runtime exception in Java that is thrown by methods in the Stack class to indicate that the stack is...
Read more >
Kotlin Coroutine Unit Testing Error: Exception in thread "main ...
UPDATE: I've been doing some additional tests, and it looks like if I remove the suspend call within my presenter if (!userRepo.isAuthenticated ...
Read more >
What's New in Stackless-Python 3.7.9
This is the change log of Stackless-Python. It lists all user visible changes to Stackless-Python. For other changes see What's New in Python....
Read more >
Coroutines As Threads - Dmitry's Blog
This post is part of the blogpost series explaining coroutines, how they implemented in various programming languages and how they can make ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found