micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.09k stars 1.07k forks source link

Coroutine endpoints throws NullPointerException "Cannot invoke InvocationInstrumenter.newInstrumentation because "p0" is null" #8555

Open dniel opened 1 year ago

dniel commented 1 year ago

Expected Behavior

When calling an endpoint that has suspend and coroutineScope or withContext and starting an async or even just a delay like this https://github.com/nsbno/trafficinfo-baseline-micronaut/blob/migrate_to_coroutines/src/main/kotlin/no/vy/trafficinfo/baseline/micronaut/controllers/ChangeController.kt#L112-L124C5 the coroutine should complete and return result from await()

Actual Behaviour

Exception is thrown.

Exception in thread "DefaultDispatcher-worker-1" kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[Dispatchers.Default, Continuation at no.vy.trafficinfo.baseline.micronaut.controllers.ChangeController$changeEventCreate$2$a$1.invokeSuspend(ChangeController.kt)@2d3b7e14]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
    at kotlinx.coroutines.DispatchedTask.handleFatalException(DispatchedTask.kt:144)
[io.micronaut.http.bind.binders.ServerRequestScopeHandler@5b52d79c, Context1{user.id=anonymous}, io.micronaut.tracing.instrument.kotlin.CoroutineTracingDispatcher@7fd70a16, CoroutineId(12), "coroutine#12":DeferredCoroutine{Active}@1c046bb2, Dispatchers.Default]
Caused by: java.lang.NullPointerException: Cannot invoke "io.micronaut.scheduling.instrument.InvocationInstrumenter.newInstrumentation()" because "p0" is null

If instead using runBlocking instead of coroutinesScope like this https://github.com/nsbno/trafficinfo-baseline-micronaut/blob/migrate_to_coroutines/src/main/kotlin/no/vy/trafficinfo/baseline/micronaut/controllers/ChangeController.kt#L94-L105 it works as expected and result is returned from endpoint.

Steps To Reproduce

  1. Example https://github.com/nsbno/trafficinfo-baseline-micronaut/blob/migrate_to_coroutines
  2. Unit tests https://github.com/nsbno/trafficinfo-baseline-micronaut/blob/migrate_to_coroutines/src/test/kotlin/no/vy/trafficinfo/baseline/micronaut/controllers/ChangeControllerSpec.kt

Environment Information

Example Application

https://github.com/nsbno/trafficinfo-baseline-micronaut/tree/migrate_to_coroutines

Version

3.8.0

dniel commented 1 year ago

Tried to upgrade as much as possible

Same problem both when starting the application in IDEA locally on my machine and packaged as a docker container using Temurin JRE 19 as base image.

dniel commented 1 year ago

Tried to downgrade coroutines to 1.6.0 and then 1.5.0 without any change.

dniel commented 1 year ago

Tried to downgrade micronaut to 3.7.0, 3.6.0, 3.5.0, 3.4.0 and all of them fails with same error.

dniel commented 1 year ago

Removed dependency on micronaut-tracing where the class CoroutineTracingDispatcher that crash lives. Now the code doesn't fail but most probably without automatic coroutines context propagation.

dniel commented 1 year ago

Seems like the list invocationInstrumenters in the constructor to CoroutineTracingDispatcher has size 1, but the element in the list is null