-
Notifications
You must be signed in to change notification settings - Fork 22
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
Integrate CoroutineContext and Scope with generated server base #7
Conversation
257fc0c
to
aabf749
Compare
fce756e
to
cd06fb5
Compare
This looks like a promising solution, and seems like more or less the right way to handle exceptions with coroutines. There are however two concerns I have with the current solution:
The code generated for Java accomplishes both of these by allowing an exception to propagate out of the What are your thoughts? |
I've tried the new setup with a server that throws simple a class GreeterImpl : GreeterGrpcKt.GreeterImplBase(
// tried with and without this
coroutineContext = newFixedThreadPoolContext(4, "server-pool")
) {
override fun greet(request: GreetRequest) = async<GreetReply> {
throw RuntimeException("bork")
}
} I've seen both:
Note that initially I did not see these. But that was because I had both the server and the client created in the same main method. This caused the main method and the JVM to exit as soon as the client received the failure, which lead to that the server thread stack trace was not printed. This was not an issue when the server was long lived, and I could see the exceptions printed consistently. On the second note, I'm not so sure if handling these kinds of exceptions in a public void greet(GreetRequest request, StreamObserver<GreetReply> responseObserver) {
// simulate asynchronous execution
executor.execute(() -> {
throw new RuntimeException("bork");
});
} In this situation, the exception is never seen by the I don't see any good way to get the exceptions to propagate to the If the first point is solved, how important is the second point for you? I think it's a matter of whether the gRPC threads or the coroutine context is executing the user code and thus where this kind of exception handling should reside. |
I've confirmed that you're right on the first point. I must have been encountering the same issue as you where the server was exiting before the exception finished propagating. The interceptor behaviour is really not important for me. I haven't seen it in any official documentation, only in a Stack Overflow answer and a GitHub issue discussion. There are two arguments I can think of in favour of trying propagating the exception:
|
@wfhartford I'll create a separate issue for trying to get exceptions to bubble up to the interceptor. At the moment, I'm not sure how that can be achieved without artificially blocking the server thread. Let me know if you have any ideas. |
Sounds good @rouzwawi, I think you're correct that the main server thread would have to be blocked, and it would be nice to avoid that. |
This should also remedy #3 by allowing a
CoroutineExceptionHandler
to be added to theCoroutineContext
, like: