Error being undetected by the SubscriberExceptionHandler
API(s)
`com.google.common.eventbus.Subscriber#dispatchEvent`
How do you want it to be improved?
final void dispatchEvent(final Object event) {
executor.execute(
new Runnable() {
@Override
public void run() {
try {
invokeSubscriberMethod(event);
} catch (InvocationTargetException e) {
bus.handleSubscriberException(e.getCause(), context(event));
} catch(Throwable e) {
bus.handleSubscriberException(e, context(event));
}
}
});
}
Why do we need it to be improved?
The Subscriber#dispatchEvent did not catch Throwable, resulting in the Error being undetected by the SubscriberExceptionHandler
Example
-
Current Behavior
Desired Behavior
Error can be detected by the SubscriberExceptionHandler
Concrete Use Cases
Checklist
-
[x] I agree to follow the code of conduct.
-
[x] I have read and understood the contribution guidelines.
-
[x] I have read and understood Guava's philosophy, and I strongly believe that this proposal aligns with it.
First, a disclaimer: we recommend against using this API.
Can you say a bit about what kind of Error you are seeing here? It's not one that is thrown by the @Subscribe method, since that would be wrapped in an InvocationTargetException and handled by the existing code. So presumably something like OutOfMemoryError or LinkageError. It's not obvious to me that catching and logging such an Error is the right thing to do.
Thanks @eamonnmcmanus for the quick feedback. When encountering ERRORs such as StackOverflowError, losing the problem stack trace can make troubleshooting extremely difficult.
Updating dispatchEvent to catch Throwable instead of only InvocationTargetException makes the system more robust, production-safe, and easier to debug. This change aligns with fail-safe principles in asynchronous and event-driven architectures.
final void dispatchEvent(final Object event) {
executor.execute(
new Runnable() {
@Override
public void run() {
try {
invokeSubscriberMethod(event);
} catch (InvocationTargetException e) {
bus.handleSubscriberException(e.getCause(), context(event));
} catch (Throwable e) { // added to catch Errors and other Throwables
bus.handleSubscriberException(e, context(event));
}
}
});
}