-
Notifications
You must be signed in to change notification settings - Fork 903
Suppressing interrupted exceptions from managed okhttp threads #7565
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
base: main
Are you sure you want to change the base?
Suppressing interrupted exceptions from managed okhttp threads #7565
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7565 +/- ##
============================================
- Coverage 90.02% 90.01% -0.01%
- Complexity 7080 7082 +2
============================================
Files 803 803
Lines 21417 21429 +12
Branches 2086 2087 +1
============================================
+ Hits 19280 19289 +9
- Misses 1475 1477 +2
- Partials 662 663 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
An alternative to this, if you want to restrict it to the Android use case, is to use an |
clearInvocations(threadMock, defaultHandler); | ||
IllegalStateException e = new IllegalStateException(); | ||
uncaughtExceptionHandler.uncaughtException(threadMock, e); | ||
verify(defaultHandler).uncaughtException(threadMock, e); |
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.
I think that doing all assertions/verifications in an async block when the main test body doesn't also verify the execution is a code smell.
I think there could be something at the end of the Runnable
(AtomicBoolean, CountdownLatch, etc) that you can then verify in the main thread. That way the reader can easily determine that the runnable completed which means the assertions all ran and passed.
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.
Got it. I've added some changes to address this, cheers!
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.
This looks good to me. I offered one small improvement idea in the test.
I see what you mean, that'll work too. Though whenever possible, I prefer to set the uncaught exception handler to the specific thread that we handle, to make the solution more targeted to this problem, rather than setting it to the main thread or as the default one, to avoid potential conflicts with other uncaught exception handlers that might be set for other reasons, as we can't control in which order they'll be set and whether all of them will be "good citizens" and wrap/delegate to the previous one. |
Fixes: open-telemetry/opentelemetry-android#1134
Supersedes #7557
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.