From a73aa12be65454ac8cfb5a8f3e056c420402f997 Mon Sep 17 00:00:00 2001 From: Chi Wang Date: Tue, 8 Mar 2022 07:27:24 -0800 Subject: [PATCH] Remote: Fix crashes with InterruptedException when using http cache. Also cancels tasks submitted during `afterCommand` if interrupted. Fixes #14787. Closes #14992. PiperOrigin-RevId: 433205726 --- .../build/lib/remote/http/HttpCacheClient.java | 18 +++++++++++++++++- .../build/lib/runtime/BlockWaitingModule.java | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/http/HttpCacheClient.java b/src/main/java/com/google/devtools/build/lib/remote/http/HttpCacheClient.java index c5646258ce6f26..6c6d4cc6f897fc 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/http/HttpCacheClient.java +++ b/src/main/java/com/google/devtools/build/lib/remote/http/HttpCacheClient.java @@ -729,7 +729,23 @@ public void close() { } isClosed = true; - channelPool.close(); + + // Clear interrupted status to prevent failure to close, indicated with #14787 + boolean wasInterrupted = Thread.interrupted(); + try { + channelPool.close(); + } catch (RuntimeException e) { + if (e.getCause() instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } else { + throw e; + } + } finally { + if (wasInterrupted) { + Thread.currentThread().interrupt(); + } + } + eventLoop.shutdownGracefully(); } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlockWaitingModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BlockWaitingModule.java index 6d5b74a8f51e88..408cf73886c2cf 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlockWaitingModule.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlockWaitingModule.java @@ -51,6 +51,7 @@ public void afterCommand() throws AbruptExitException { try { executorService.awaitTermination(Long.MAX_VALUE, SECONDS); } catch (InterruptedException e) { + executorService.shutdownNow(); Thread.currentThread().interrupt(); }