From c294c23760464b87ec193fc9b995e06f3d5e907f Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 4 Dec 2023 06:43:28 -0800 Subject: [PATCH] Make various classes catch `Exception` instead of `RuntimeException` even when only `RuntimeException` is theoretically possible. In fact, because checked exceptions are a Java-source-language feature (rather than a JVM feature), it's possible to throw them when you're not "supposed to," either through tricky Java code or more naturally through other languages, such as Kotlin. We have seen this happening a few times in tests. This causes exceptions to slip past the `catch` blocks that are meant to handle them. This could cause operations to hang, such as when one listener of a `ListenableFuture` throws an exception, preventing subsequent listeners (such as a listener that powers `Futures.transform`) from running. I didn't cover _every_ `catch (RuntimeException ...)` block in our codebase, but I tried to get the ones that looked most likely to be affected. RELNOTES=Changed various classes to catch `Exception` instead of `RuntimeException` even when only `RuntimeException` is theoretically possible. PiperOrigin-RevId: 587701612 --- .../util/concurrent/AbstractFutureTest.java | 20 +++++++++++++++++++ .../util/concurrent/AbstractFuture.java | 20 ++++++++++++++----- .../concurrent/AbstractScheduledService.java | 4 +++- .../common/util/concurrent/ExecutionList.java | 3 ++- .../common/util/concurrent/Futures.java | 6 ++++-- .../util/concurrent/ImmediateFuture.java | 3 ++- .../util/concurrent/ListenerCallQueue.java | 6 ++++-- .../common/util/concurrent/Monitor.java | 3 ++- .../common/util/concurrent/MoreExecutors.java | 3 ++- .../util/concurrent/SequentialExecutor.java | 6 ++++-- .../util/concurrent/AbstractFutureTest.java | 20 +++++++++++++++++++ .../util/concurrent/AbstractFuture.java | 20 ++++++++++++++----- .../concurrent/AbstractScheduledService.java | 4 +++- .../common/util/concurrent/ExecutionList.java | 3 ++- .../common/util/concurrent/Futures.java | 6 ++++-- .../util/concurrent/ImmediateFuture.java | 3 ++- .../util/concurrent/ListenerCallQueue.java | 6 ++++-- .../common/util/concurrent/Monitor.java | 3 ++- .../common/util/concurrent/MoreExecutors.java | 3 ++- .../util/concurrent/SequentialExecutor.java | 6 ++++-- 20 files changed, 116 insertions(+), 32 deletions(-) diff --git a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java index 9385acf5a9f8..6bf4c99d1b32 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -20,6 +20,7 @@ import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtIncompatible; @@ -1056,6 +1057,25 @@ public void run() { t.join(); } + public void testCatchesUndeclaredThrowableFromListener() { + AbstractFuture f = new AbstractFuture() {}; + f.set("foo"); + f.addListener(() -> sneakyThrow(new SomeCheckedException()), directExecutor()); + } + + private static final class SomeCheckedException extends Exception {} + + /** Throws an undeclared checked exception. */ + private static void sneakyThrow(Throwable t) { + class SneakyThrower { + @SuppressWarnings("unchecked") // intentionally unsafe for test + void throwIt(Throwable t) throws T { + throw (T) t; + } + } + new SneakyThrower().throwIt(t); + } + public void testTrustedGetFailure_Completed() { SettableFuture future = SettableFuture.create(); future.set("261"); diff --git a/android/guava/src/com/google/common/util/concurrent/AbstractFuture.java b/android/guava/src/com/google/common/util/concurrent/AbstractFuture.java index b060e74bca73..e2232c272331 100644 --- a/android/guava/src/com/google/common/util/concurrent/AbstractFuture.java +++ b/android/guava/src/com/google/common/util/concurrent/AbstractFuture.java @@ -867,7 +867,9 @@ protected boolean setFuture(ListenableFuture future) { // since all we are doing is unpacking a completed future which should be fast. try { future.addListener(valueToSet, DirectExecutor.INSTANCE); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // addListener has thrown an exception! SetFuture.run can't throw any exceptions so this // must have been caused by addListener itself. The most likely explanation is a // misconfigured mock. Try to switch to Failure. @@ -1193,6 +1195,7 @@ protected String pendingToString() { return null; } + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void addPendingString(StringBuilder builder) { // Capture current builder length so it can be truncated if this future ends up completing while // the toString is being calculated @@ -1209,7 +1212,9 @@ private void addPendingString(StringBuilder builder) { String pendingDescription; try { pendingDescription = Strings.emptyToNull(pendingToString()); - } catch (RuntimeException | StackOverflowError e) { + } catch (Exception | StackOverflowError e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // Don't call getMessage or toString() on the exception, in case the exception thrown by the // subclass is implemented with bugs similar to the subclass. pendingDescription = "Exception thrown from implementation: " + e.getClass(); @@ -1228,6 +1233,7 @@ private void addPendingString(StringBuilder builder) { } } + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void addDoneString(StringBuilder builder) { try { V value = getUninterruptibly(this); @@ -1238,7 +1244,7 @@ private void addDoneString(StringBuilder builder) { builder.append("FAILURE, cause=[").append(e.getCause()).append("]"); } catch (CancellationException e) { builder.append("CANCELLED"); // shouldn't be reachable - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception builder.append("UNKNOWN, cause=[").append(e.getClass()).append(" thrown from get()]"); } } @@ -1262,6 +1268,7 @@ private void appendResultObject(StringBuilder builder, @CheckForNull Object o) { } /** Helper for printing user supplied objects into our toString method. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { // This is some basic recursion detection for when people create cycles via set/setFuture or // when deep chains of futures exist resulting in a StackOverflowException. We could detect @@ -1273,7 +1280,9 @@ private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { } else { builder.append(o); } - } catch (RuntimeException | StackOverflowError e) { + } catch (Exception | StackOverflowError e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // Don't call getMessage or toString() on the exception, in case the exception thrown by the // user object is implemented with bugs similar to the user object. builder.append("Exception thrown from implementation: ").append(e.getClass()); @@ -1284,10 +1293,11 @@ private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { * Submits the given runnable to the given {@link Executor} catching and logging all {@linkplain * RuntimeException runtime exceptions} thrown by the executor. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private static void executeListener(Runnable runnable, Executor executor) { try { executor.execute(runnable); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going -- bad runnable and/or executor. Don't punish the other runnables if // we're given a bad one. We only catch RuntimeException because we want Errors to propagate // up. diff --git a/android/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java b/android/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java index 0b7d34e6ba4d..826374bad133 100644 --- a/android/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java +++ b/android/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java @@ -564,7 +564,9 @@ public Cancellable reschedule() { lock.lock(); try { toReturn = initializeOrUpdateCancellationDelegate(schedule); - } catch (RuntimeException | Error e) { + } catch (Throwable e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // If an exception is thrown by the subclass then we need to make sure that the service // notices and transitions to the FAILED state. We do it by calling notifyFailed directly // because the service does not monitor the state of the future so if the exception is not diff --git a/android/guava/src/com/google/common/util/concurrent/ExecutionList.java b/android/guava/src/com/google/common/util/concurrent/ExecutionList.java index 645817c4af36..5f9dfc28ac21 100644 --- a/android/guava/src/com/google/common/util/concurrent/ExecutionList.java +++ b/android/guava/src/com/google/common/util/concurrent/ExecutionList.java @@ -140,10 +140,11 @@ public void execute() { * Submits the given runnable to the given {@link Executor} catching and logging all {@linkplain * RuntimeException runtime exceptions} thrown by the executor. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private static void executeListener(Runnable runnable, Executor executor) { try { executor.execute(runnable); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going -- bad runnable and/or executor. Don't punish the other runnables if // we're given a bad one. We only catch RuntimeException because we want Errors to propagate // up. diff --git a/android/guava/src/com/google/common/util/concurrent/Futures.java b/android/guava/src/com/google/common/util/concurrent/Futures.java index e36ff391809c..5715f0a9f0e8 100644 --- a/android/guava/src/com/google/common/util/concurrent/Futures.java +++ b/android/guava/src/com/google/common/util/concurrent/Futures.java @@ -510,7 +510,8 @@ public O get(long timeout, TimeUnit unit) private O applyTransformation(I input) throws ExecutionException { try { return function.apply(input); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. throw new ExecutionException(t); } } @@ -1091,7 +1092,8 @@ public void run() { } catch (ExecutionException e) { callback.onFailure(e.getCause()); return; - } catch (RuntimeException | Error e) { + } catch (Throwable e) { + // Any Exception is either a RuntimeException or sneaky checked exception. callback.onFailure(e); return; } diff --git a/android/guava/src/com/google/common/util/concurrent/ImmediateFuture.java b/android/guava/src/com/google/common/util/concurrent/ImmediateFuture.java index f09816c4e3cb..0a6e2303b877 100644 --- a/android/guava/src/com/google/common/util/concurrent/ImmediateFuture.java +++ b/android/guava/src/com/google/common/util/concurrent/ImmediateFuture.java @@ -42,12 +42,13 @@ class ImmediateFuture implements ListenableFuture } @Override + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception public void addListener(Runnable listener, Executor executor) { checkNotNull(listener, "Runnable was null."); checkNotNull(executor, "Executor was null."); try { executor.execute(listener); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // ListenableFuture's contract is that it will not throw unchecked exceptions, so log the bad // runnable and/or executor and swallow it. log.log( diff --git a/android/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java b/android/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java index 4ef7ed36c056..897c95d5e187 100644 --- a/android/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java +++ b/android/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java @@ -159,6 +159,7 @@ synchronized void add(ListenerCallQueue.Event event, Object label) { * Dispatches all listeners {@linkplain #enqueue enqueued} prior to this call, serially and in * order. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception void dispatch() { boolean scheduleEventRunner = false; synchronized (this) { @@ -170,7 +171,7 @@ void dispatch() { if (scheduleEventRunner) { try { executor.execute(this); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // reset state in case of an error so that later dispatch calls will actually do something synchronized (this) { isThreadScheduled = false; @@ -186,6 +187,7 @@ void dispatch() { } @Override + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception public void run() { boolean stillRunning = true; try { @@ -206,7 +208,7 @@ public void run() { // Always run while _not_ holding the lock, to avoid deadlocks. try { nextToRun.call(listener); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going. logger.log( Level.SEVERE, diff --git a/android/guava/src/com/google/common/util/concurrent/Monitor.java b/android/guava/src/com/google/common/util/concurrent/Monitor.java index 5542b0bd5522..8ba45d49f315 100644 --- a/android/guava/src/com/google/common/util/concurrent/Monitor.java +++ b/android/guava/src/com/google/common/util/concurrent/Monitor.java @@ -1013,7 +1013,8 @@ private void signalNextWaiter() { private boolean isSatisfied(Guard guard) { try { return guard.isSatisfied(); - } catch (RuntimeException | Error throwable) { + } catch (Throwable throwable) { + // Any Exception is either a RuntimeException or sneaky checked exception. signalAllWaiters(); throw throwable; } diff --git a/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java b/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java index dba374014806..4c9bd2bd23e9 100644 --- a/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java +++ b/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java @@ -678,7 +678,8 @@ public NeverSuccessfulListenableFutureTask(Runnable delegate) { public void run() { try { delegate.run(); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. setException(t); throw t; } diff --git a/android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java b/android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java index c842d7e07d56..224bd1f120cb 100644 --- a/android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java +++ b/android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java @@ -136,7 +136,8 @@ public String toString() { try { executor.execute(worker); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. synchronized (queue) { boolean removed = (workerRunningState == IDLE || workerRunningState == QUEUING) @@ -202,6 +203,7 @@ public void run() { * will still be present. If the composed Executor is an ExecutorService, it can respond to * shutdown() by returning tasks queued on that Thread after {@link #worker} drains the queue. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void workOnQueue() { boolean interruptedDuringTask = false; boolean hasSetRunning = false; @@ -235,7 +237,7 @@ private void workOnQueue() { interruptedDuringTask |= Thread.interrupted(); try { task.run(); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception log.log(Level.SEVERE, "Exception while executing runnable " + task, e); } finally { task = null; diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java b/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java index 9385acf5a9f8..6bf4c99d1b32 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java @@ -20,6 +20,7 @@ import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtIncompatible; @@ -1056,6 +1057,25 @@ public void run() { t.join(); } + public void testCatchesUndeclaredThrowableFromListener() { + AbstractFuture f = new AbstractFuture() {}; + f.set("foo"); + f.addListener(() -> sneakyThrow(new SomeCheckedException()), directExecutor()); + } + + private static final class SomeCheckedException extends Exception {} + + /** Throws an undeclared checked exception. */ + private static void sneakyThrow(Throwable t) { + class SneakyThrower { + @SuppressWarnings("unchecked") // intentionally unsafe for test + void throwIt(Throwable t) throws T { + throw (T) t; + } + } + new SneakyThrower().throwIt(t); + } + public void testTrustedGetFailure_Completed() { SettableFuture future = SettableFuture.create(); future.set("261"); diff --git a/guava/src/com/google/common/util/concurrent/AbstractFuture.java b/guava/src/com/google/common/util/concurrent/AbstractFuture.java index 444b16cd8ea0..a044b39ae885 100644 --- a/guava/src/com/google/common/util/concurrent/AbstractFuture.java +++ b/guava/src/com/google/common/util/concurrent/AbstractFuture.java @@ -867,7 +867,9 @@ protected boolean setFuture(ListenableFuture future) { // since all we are doing is unpacking a completed future which should be fast. try { future.addListener(valueToSet, DirectExecutor.INSTANCE); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // addListener has thrown an exception! SetFuture.run can't throw any exceptions so this // must have been caused by addListener itself. The most likely explanation is a // misconfigured mock. Try to switch to Failure. @@ -1193,6 +1195,7 @@ protected String pendingToString() { return null; } + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void addPendingString(StringBuilder builder) { // Capture current builder length so it can be truncated if this future ends up completing while // the toString is being calculated @@ -1209,7 +1212,9 @@ private void addPendingString(StringBuilder builder) { String pendingDescription; try { pendingDescription = Strings.emptyToNull(pendingToString()); - } catch (RuntimeException | StackOverflowError e) { + } catch (Exception | StackOverflowError e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // Don't call getMessage or toString() on the exception, in case the exception thrown by the // subclass is implemented with bugs similar to the subclass. pendingDescription = "Exception thrown from implementation: " + e.getClass(); @@ -1228,6 +1233,7 @@ private void addPendingString(StringBuilder builder) { } } + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void addDoneString(StringBuilder builder) { try { V value = getUninterruptibly(this); @@ -1238,7 +1244,7 @@ private void addDoneString(StringBuilder builder) { builder.append("FAILURE, cause=[").append(e.getCause()).append("]"); } catch (CancellationException e) { builder.append("CANCELLED"); // shouldn't be reachable - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception builder.append("UNKNOWN, cause=[").append(e.getClass()).append(" thrown from get()]"); } } @@ -1262,6 +1268,7 @@ private void appendResultObject(StringBuilder builder, @CheckForNull Object o) { } /** Helper for printing user supplied objects into our toString method. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { // This is some basic recursion detection for when people create cycles via set/setFuture or // when deep chains of futures exist resulting in a StackOverflowException. We could detect @@ -1273,7 +1280,9 @@ private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { } else { builder.append(o); } - } catch (RuntimeException | StackOverflowError e) { + } catch (Exception | StackOverflowError e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // Don't call getMessage or toString() on the exception, in case the exception thrown by the // user object is implemented with bugs similar to the user object. builder.append("Exception thrown from implementation: ").append(e.getClass()); @@ -1284,10 +1293,11 @@ private void appendUserObject(StringBuilder builder, @CheckForNull Object o) { * Submits the given runnable to the given {@link Executor} catching and logging all {@linkplain * RuntimeException runtime exceptions} thrown by the executor. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private static void executeListener(Runnable runnable, Executor executor) { try { executor.execute(runnable); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going -- bad runnable and/or executor. Don't punish the other runnables if // we're given a bad one. We only catch RuntimeException because we want Errors to propagate // up. diff --git a/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java b/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java index 164a6dbb435d..d6865f0a8c4a 100644 --- a/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java +++ b/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java @@ -606,7 +606,9 @@ public Cancellable reschedule() { lock.lock(); try { toReturn = initializeOrUpdateCancellationDelegate(schedule); - } catch (RuntimeException | Error e) { + } catch (Throwable e) { + // Any Exception is either a RuntimeException or sneaky checked exception. + // // If an exception is thrown by the subclass then we need to make sure that the service // notices and transitions to the FAILED state. We do it by calling notifyFailed directly // because the service does not monitor the state of the future so if the exception is not diff --git a/guava/src/com/google/common/util/concurrent/ExecutionList.java b/guava/src/com/google/common/util/concurrent/ExecutionList.java index 645817c4af36..5f9dfc28ac21 100644 --- a/guava/src/com/google/common/util/concurrent/ExecutionList.java +++ b/guava/src/com/google/common/util/concurrent/ExecutionList.java @@ -140,10 +140,11 @@ public void execute() { * Submits the given runnable to the given {@link Executor} catching and logging all {@linkplain * RuntimeException runtime exceptions} thrown by the executor. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private static void executeListener(Runnable runnable, Executor executor) { try { executor.execute(runnable); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going -- bad runnable and/or executor. Don't punish the other runnables if // we're given a bad one. We only catch RuntimeException because we want Errors to propagate // up. diff --git a/guava/src/com/google/common/util/concurrent/Futures.java b/guava/src/com/google/common/util/concurrent/Futures.java index c073d1d20b09..6e8d20193eb5 100644 --- a/guava/src/com/google/common/util/concurrent/Futures.java +++ b/guava/src/com/google/common/util/concurrent/Futures.java @@ -545,7 +545,8 @@ public O get(long timeout, TimeUnit unit) private O applyTransformation(I input) throws ExecutionException { try { return function.apply(input); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. throw new ExecutionException(t); } } @@ -1126,7 +1127,8 @@ public void run() { } catch (ExecutionException e) { callback.onFailure(e.getCause()); return; - } catch (RuntimeException | Error e) { + } catch (Throwable e) { + // Any Exception is either a RuntimeException or sneaky checked exception. callback.onFailure(e); return; } diff --git a/guava/src/com/google/common/util/concurrent/ImmediateFuture.java b/guava/src/com/google/common/util/concurrent/ImmediateFuture.java index f09816c4e3cb..0a6e2303b877 100644 --- a/guava/src/com/google/common/util/concurrent/ImmediateFuture.java +++ b/guava/src/com/google/common/util/concurrent/ImmediateFuture.java @@ -42,12 +42,13 @@ class ImmediateFuture implements ListenableFuture } @Override + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception public void addListener(Runnable listener, Executor executor) { checkNotNull(listener, "Runnable was null."); checkNotNull(executor, "Executor was null."); try { executor.execute(listener); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // ListenableFuture's contract is that it will not throw unchecked exceptions, so log the bad // runnable and/or executor and swallow it. log.log( diff --git a/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java b/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java index 4ef7ed36c056..897c95d5e187 100644 --- a/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java +++ b/guava/src/com/google/common/util/concurrent/ListenerCallQueue.java @@ -159,6 +159,7 @@ synchronized void add(ListenerCallQueue.Event event, Object label) { * Dispatches all listeners {@linkplain #enqueue enqueued} prior to this call, serially and in * order. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception void dispatch() { boolean scheduleEventRunner = false; synchronized (this) { @@ -170,7 +171,7 @@ void dispatch() { if (scheduleEventRunner) { try { executor.execute(this); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // reset state in case of an error so that later dispatch calls will actually do something synchronized (this) { isThreadScheduled = false; @@ -186,6 +187,7 @@ void dispatch() { } @Override + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception public void run() { boolean stillRunning = true; try { @@ -206,7 +208,7 @@ public void run() { // Always run while _not_ holding the lock, to avoid deadlocks. try { nextToRun.call(listener); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception // Log it and keep going. logger.log( Level.SEVERE, diff --git a/guava/src/com/google/common/util/concurrent/Monitor.java b/guava/src/com/google/common/util/concurrent/Monitor.java index 2ed31eda4ecc..1abbc6405319 100644 --- a/guava/src/com/google/common/util/concurrent/Monitor.java +++ b/guava/src/com/google/common/util/concurrent/Monitor.java @@ -1123,7 +1123,8 @@ private void signalNextWaiter() { private boolean isSatisfied(Guard guard) { try { return guard.isSatisfied(); - } catch (RuntimeException | Error throwable) { + } catch (Throwable throwable) { + // Any Exception is either a RuntimeException or sneaky checked exception. signalAllWaiters(); throw throwable; } diff --git a/guava/src/com/google/common/util/concurrent/MoreExecutors.java b/guava/src/com/google/common/util/concurrent/MoreExecutors.java index ea3536957038..824c2001538e 100644 --- a/guava/src/com/google/common/util/concurrent/MoreExecutors.java +++ b/guava/src/com/google/common/util/concurrent/MoreExecutors.java @@ -739,7 +739,8 @@ public NeverSuccessfulListenableFutureTask(Runnable delegate) { public void run() { try { delegate.run(); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. setException(t); throw t; } diff --git a/guava/src/com/google/common/util/concurrent/SequentialExecutor.java b/guava/src/com/google/common/util/concurrent/SequentialExecutor.java index c842d7e07d56..224bd1f120cb 100644 --- a/guava/src/com/google/common/util/concurrent/SequentialExecutor.java +++ b/guava/src/com/google/common/util/concurrent/SequentialExecutor.java @@ -136,7 +136,8 @@ public String toString() { try { executor.execute(worker); - } catch (RuntimeException | Error t) { + } catch (Throwable t) { + // Any Exception is either a RuntimeException or sneaky checked exception. synchronized (queue) { boolean removed = (workerRunningState == IDLE || workerRunningState == QUEUING) @@ -202,6 +203,7 @@ public void run() { * will still be present. If the composed Executor is an ExecutorService, it can respond to * shutdown() by returning tasks queued on that Thread after {@link #worker} drains the queue. */ + @SuppressWarnings("CatchingUnchecked") // sneaky checked exception private void workOnQueue() { boolean interruptedDuringTask = false; boolean hasSetRunning = false; @@ -235,7 +237,7 @@ private void workOnQueue() { interruptedDuringTask |= Thread.interrupted(); try { task.run(); - } catch (RuntimeException e) { + } catch (Exception e) { // sneaky checked exception log.log(Level.SEVERE, "Exception while executing runnable " + task, e); } finally { task = null;