Ensure SettableFutures in remote module are all set.

Failed doing so, will cause Bazel hanging forever. This could be one of causes for #11782.

Closes #12426.

PiperOrigin-RevId: 341554621
diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteCache.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteCache.java
index 659d9a1..511c5bd 100644
--- a/src/main/java/com/google/devtools/build/lib/remote/RemoteCache.java
+++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteCache.java
@@ -35,6 +35,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
+import com.google.common.flogger.GoogleLogger;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -92,6 +93,7 @@
 /** A cache for storing artifacts (input and output) as well as the output of running an action. */
 @ThreadSafety.ThreadSafe
 public class RemoteCache implements AutoCloseable {
+  private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
 
   /** See {@link SpawnExecutionContext#lockOutputFiles()}. */
   @FunctionalInterface
@@ -265,7 +267,12 @@
         new FutureCallback<Void>() {
           @Override
           public void onSuccess(Void aVoid) {
-            outerF.set(bOut.toByteArray());
+            try {
+              outerF.set(bOut.toByteArray());
+            } catch (RuntimeException e) {
+              logger.atWarning().withCause(e).log("Unexpected exception");
+              outerF.setException(e);
+            }
           }
 
           @Override
@@ -471,6 +478,9 @@
               outerF.set(null);
             } catch (IOException e) {
               outerF.setException(e);
+            } catch (RuntimeException e) {
+              logger.atWarning().withCause(e).log("Unexpected exception");
+              outerF.setException(e);
             }
           }
 
@@ -482,6 +492,9 @@
               if (t != e) {
                 t.addSuppressed(e);
               }
+            } catch (RuntimeException e) {
+              logger.atWarning().withCause(e).log("Unexpected exception");
+              t.addSuppressed(e);
             } finally {
               outerF.setException(t);
             }