Skip to content

Uncaught exception is thrown two times from a coroutine scope in instrumentation/robolectric tests #3033

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
mkar-dev opened this issue Nov 19, 2021 · 1 comment
Labels

Comments

@mkar-dev
Copy link

mkar-dev commented Nov 19, 2021

I've stumbled onto weird behaviour regarding coroutineScope in instrumentation tests
I might be misunderstanding something about threads or something else, but it seems an undesired behaviour for me right now
It isn't occurring in a simple unit test (not android instrumentation nor robolectric), however

Current behaviour:

  • UncaughtExceptionHandler is called twice with same exception

Expected Behaviour:

  • UncaughtExceptionHandler is called only once per every exception

EDIT:

I just realised it doesn't even require the usage of Thread.uncaughtExceptionHandler
The exception is just thrown two times regardless of that handler presence

Code sample:

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    @Test
    fun uncaughtExceptionDoubling() = runBlocking{
        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            println("Handling stuff: $e")
        }

        CoroutineScope(Dispatchers.Unconfined).launch {
            throw IllegalStateException("Notice me")
        }.join()
    }
}

Output:

--------- beginning of main
11-16 05:27:12.952 10156  7902  7918 I System.out: Handling stuff: java.lang.IllegalStateException: Notice me
11-16 05:27:12.952 10156  7902  7918 I System.out: Handling stuff: java.lang.IllegalStateException: Notice me
11-16 05:27:12.952 10156  7902  7918 I TestRunner: finished: uncaughtExceptionDoubling(com.example.doubledexceptionhandling02.ExampleInstrumentedTest)
--------- beginning of kernel

Gradle dependencies (rest is standard generated by Android studio, I can provide rest if necessary)

  implementation 'androidx.core:core-ktx:1.7.0'
  implementation 'androidx.appcompat:appcompat:1.3.1'
  implementation 'com.google.android.material:material:1.4.0'
  implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0")
  testImplementation 'junit:junit:4.+'
  testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0'
  androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

Kotlin plugin version:

org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Dec 6, 2021

Should be fixed with #3056

@qwwdfsad qwwdfsad added the bug label Dec 7, 2021
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

2 participants