diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java index 88b01c66898..f5d23c4f3f8 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java @@ -389,10 +389,27 @@ public class RemoteClusterConnectionTests extends ESTestCase { throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicReference exceptionAtomicReference = new AtomicReference<>(); - ActionListener listener = ActionListener.wrap(x -> latch.countDown(), x -> { - exceptionAtomicReference.set(x); - latch.countDown(); - }); + ActionListener listener = ActionListener.wrap( + x -> latch.countDown(), + x -> { + /* + * This can occur on a thread submitted to the thread pool while we are closing the + * remote cluster connection at the end of the test. + */ + if (x instanceof CancellableThreads.ExecutionCancelledException) { + try { + // we should already be shutting down + assertEquals(0L, latch.getCount()); + } finally { + // ensure we count down the latch on failure as well to not prevent failing tests from ending + latch.countDown(); + } + return; + } + exceptionAtomicReference.set(x); + latch.countDown(); + } + ); connection.updateSeedNodes(proxyAddress, seedNodes, listener); latch.await(); if (exceptionAtomicReference.get() != null) {