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.

Advice exception handling not working with grpc-kotlin coroutines

See original GitHub issue

The context

The plan is to implement the server stub with coroutine support from grpc-kotlin and exception handling via an exception handler advice.

The bug

While the registered advice picks up exceptions when implementing the normal blocking server stub (*ImplBase), when using coroutines support (*CoroutineImplBase) the exceptions are not handled and a Status.UNKNOWN is returned regardless.

Stacktrace and logs

Is there a stacktrace or a hint in the logs? (very important) Screenshots work as well, but don’t screenshot your logs.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Implement server stubs for the CoroutineImplBase
  2. Implement and register GrpcAdvice
  3. Force some exception to be thrown
  4. Verify that advice methods are not reached

The application’s environment

Which versions do you use?

  • Spring (boot): 2.4.0
  • grpc-kotlin: 1.0.0
  • grpc-spring-boot-starter: 2.11.0

Additional context

There seems to be a difference between the regular implementation where onHalfClose is called (and thus exception is handled) and the coroutine implementation.

To go around this, I quickly created a hacky interceptor like so:

import io.grpc.ForwardingServerCall
import io.grpc.Metadata
import io.grpc.ServerCall
import io.grpc.ServerCallHandler
import io.grpc.ServerInterceptor
import io.grpc.Status
import io.grpc.StatusException
import net.devh.boot.grpc.server.advice.GrpcAdviceExceptionHandler

@GrpcGlobalServerInterceptor
class ExceptionHandlingInterceptor(private val handler: GrpcAdviceExceptionHandler) : ServerInterceptor {

    private class ExceptionTranslatingServerCall<ReqT, RespT>(
        delegate: ServerCall<ReqT, RespT>,
        private val handler: GrpcAdviceExceptionHandler
    ) : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(delegate) {

        override fun close(status: Status, trailers: Metadata) {
            if (status.isOk) {
                return super.close(status, trailers)
            }

            val problemStatus = handler.handleThrownException(status.cause) as StatusException
            super.close(problemStatus.status, trailers)
        }
    }

    override fun <ReqT : Any?, RespT : Any?> interceptCall(call: ServerCall<ReqT, RespT>?, headers: Metadata?, next: ServerCallHandler<ReqT, RespT>?): ServerCall.Listener<ReqT> {
        return next!!.startCall(ExceptionTranslatingServerCall(call!!, handler), headers)
    }
}

I haven’t delved deep into the underlying issue, so it can be a problem of the grpc-kotlin-stub:1.0.0 I’m using

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
ST-DDTcommented, Nov 17, 2021

Definitely this year. Most likely in November. I can try to push the release at bit.

2reactions
skoretcommented, Apr 6, 2021

It really seems like a bug in grpc-kotlin, related issues: grpc/grpc-kotlin#141 and grpc/grpc-kotlin#184

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advice exception handling not working with grpc-kotlin ...
The plan is to implement the server stub with coroutine support from grpc-kotlin and exception handling via an exception handler advice. The bug....
Read more >
Coroutine exceptions handling | Kotlin
This section covers exception handling and cancellation on exceptions. We already know that a cancelled coroutine throws ...
Read more >
Seamless back-pressure handling in gRPC-Kotlin - Medium
To handle back-pressure in gRPC-Java, one has to setOnReadyHandler and check isReady . This is very error-prone. In Kotlin coroutines, there ...
Read more >
RetryPolicy does not work with coroutines - java - Stack Overflow
gRPC depends on StreamObserver to send response to client after call responseObserver.onCompleted() or responseObserver.onError , please make ...
Read more >
Bootstrapping a Kotlin gRPC service with Spring Boot
It has always been possible to build gRPC services in kotlin through ... Note that while our services are using coroutines, we will...
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