-
Notifications
You must be signed in to change notification settings - Fork 903
Shutting down a grpcsender without interrupting the thread #7557
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
Conversation
@@ -214,7 +214,7 @@ private static String grpcMessage(Response response) { | |||
public CompletableResultCode shutdown() { | |||
client.dispatcher().cancelAll(); | |||
if (managedExecutor) { | |||
client.dispatcher().executorService().shutdownNow(); | |||
client.dispatcher().executorService().shutdown(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know if the executor is using daemon threads, or do we need to worry about this possibly impacting application shutdown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So managedExecutor
is only true if no ExecutorService
is NOT passed into this class, which means we are purging the ExecutorService
only when we are using OktHttp
default ExecutorService
for the Dispatcher
.
Shouldn't it be the other way around, i.e. we only explicitly shut down the executor if we pass it in ourselves, and leave the clean up to OkHttp
if it's providing its own executor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
managedExecutor
is only true if noExecutorService
is passed into this class, which means we are purging theExecutorService
only when we are usingOktHttp
defaultExecutorService
for theDispatcher
.
I think we never use okhttp's default service, as we set one here when users don't provide any.
do you know if the executor is using daemon threads, or do we need to worry about this possibly impacting application shutdown?
We are using daemon threads, but it's not guaranteed that it will always be the case. I didn't think of the app not closing due to a non-daemon thread scenario, because Android will still close an app regardless of existing non-daemon threads running, but it's a great point. Also, @laurit mentioned below that we're using shutdownNow
in many places, so this doesn't seem to be a proper fix overall. I'll work on an alternative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL, sorry, I flipped the logic yesterday. What I meant to say is that managedExecutor
is only true if you DON'T pass in an executor, so this codepath should never be hit if we always passed one in.
THAT was the logic that I thought seems a bit off.
That being said, handing the uncaught exception explicitly seems like a good idea. Android won't crash the app until the uncaught exception is thrown on the main thread, but silently crashing a background thread isn't great either if you can deal with it explicitly.
I guess the issue here is that the default uncaught exception handler on android kills the application while in java it prints the exception to stderr. I doubt that this issue is specific only to the grpc+okhttp, there are other places in the sdk that call |
This is a better alternative, so I've created this PR to go with this approach instead. Thanks, @laurit. |
Fixes: open-telemetry/opentelemetry-android#1134
Summary
This is to avoid the following crash from happening when a grpc exporter is shut down while okhttp is waiting to establish a connection with the server.
Description
The crash happens when okhttp gets an unhandled interrupted exception here which propagates to the host app.
The proposed solution is to avoid interrupting the thread, so that the execution will return here after a timeout, and then finish here as the call is cancelled prior to attempting to shut down the executor.
Alternative solution
Asking okhttp to handle possible
java.lang.InterruptedException
s here.