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

Kotlin grpc context validateGeneration error #11411

Closed
KarinaGanieva-sl opened this issue May 20, 2024 · 3 comments · Fixed by #11500
Closed

Kotlin grpc context validateGeneration error #11411

KarinaGanieva-sl opened this issue May 20, 2024 · 3 comments · Fixed by #11500
Labels
bug Something isn't working needs repro needs triage New issue that requires triage

Comments

@KarinaGanieva-sl
Copy link

Describe the bug

My vertx kotlin application runs runBlocking code and makes a great number of grpc calls to the server. I'm using javaagent (version 2.4.0) for auto-instrumentation on my client.
Making grpc calls with kotlin grpc stub and collecting flow leads to this error:
May 20, 2024 8:21:17 PM io.grpc.Context validateGeneration SEVERE: Context ancestry chain length is abnormally long. This suggests an error in application code. Length exceeded: 1000 java.lang.Exception at io.grpc.Context.validateGeneration(Context.java:1110) at io.grpc.Context.<init>(Context.java:191) at io.grpc.Context.fork(Context.java:404) at io.opentelemetry.javaagent.shaded.instrumentation.grpc.v1_6.internal.ContextStorageBridge.current(ContextStorageBridge.java:138) at io.grpc.Context.current(Context.java:172) at opentelemetry.GrpcClientKotlinStub$Companion$main$1.invokeSuspend(GrpcClientKotlinStub.kt:53) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:28) at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:99) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104) at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:608) at io.vertx.reactivex.ContextScheduler$ContextWorker$TimedAction.run(ContextScheduler.java:190) at io.vertx.reactivex.ContextScheduler$ContextWorker$TimedAction.lambda$execute$1(ContextScheduler.java:175) at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$1(ContextImpl.java:191) at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:279) at io.vertx.core.impl.ContextImpl.lambda$internalExecuteBlocking$2(ContextImpl.java:210) at io.opentelemetry.javaagent.bootstrap.executors.ContextPropagatingRunnable.run(ContextPropagatingRunnable.java:37)

During debugging I found out that grpc context is being forked all the time despite the new independent grpc call. Adding io.opentelemetry.context.Context.current().asContextElement() fixes the issue but having kotlinx-coroutines module inside javaagent should be enough

Steps to reproduce

`class GrpcClientKotlinStub {

companion object {
    val vertx = Vertx.vertx()
    val openTelemetryContext = io.opentelemetry.context.Context.current().asContextElement()

    @JvmStatic
    fun main(): Unit = runBlocking(vertx.blockingDispatcher(false)) {

        val channel = ManagedChannelBuilder
            .forAddress("localhost", 8080)
            .usePlaintext()
            .build()
        val count = AtomicInteger()

        (1..600).map { n ->
            val stub = BofficerApiGrpcKt.BofficerApiCoroutineStub(channel)
            println(n)
            stub.hello(HelloRequest.getDefaultInstance())
                .collect {
                    count.incrementAndGet()
                }
            val context2 = Context.current()
            val generationField2 = Context::class.java.getDeclaredField("generation")
            generationField2.isAccessible = true
            println("generation after: ${generationField2.get(context2) as Int}")
        }
        println("after request, got ${count.get()} items")
        channel.shutdown()
    }
}

}

fun main() {
GrpcClientKotlinStub.main()
}`

Expected behavior

No problem with making grpc call using kotlin coroutines and opentelemetry at the same time

Actual behavior

Exception in io.grpc.Context validateGeneration due to a great number of forks

Javaagent or library instrumentation version

2.4.0

Environment

JDK: 17.0.6
OS: Ubuntu 20.04.3 LTS

Additional context

No response

@KarinaGanieva-sl KarinaGanieva-sl added bug Something isn't working needs triage New issue that requires triage labels May 20, 2024
@KarinaGanieva-sl
Copy link
Author

The bug is reproduced only when using Vertx dispatcher

@laurit
Copy link
Contributor

laurit commented May 21, 2024

Could you provide a minimal application that reproduces the issue.

@laurit laurit added needs author feedback Waiting for additional feedback from the author needs repro labels May 21, 2024
@KarinaGanieva-sl
Copy link
Author

@github-actions github-actions bot removed the needs author feedback Waiting for additional feedback from the author label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs repro needs triage New issue that requires triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants