9292import org .elasticsearch .test .IndexSettingsModule ;
9393import org .elasticsearch .threadpool .TestThreadPool ;
9494import org .elasticsearch .threadpool .ThreadPool ;
95+ import org .elasticsearch .threadpool .ThreadPoolStats ;
9596import org .elasticsearch .xpack .searchablesnapshots .SearchableSnapshots ;
9697import org .elasticsearch .xpack .searchablesnapshots .cache .CacheService ;
9798import org .hamcrest .Matcher ;
119120import java .util .List ;
120121import java .util .Map ;
121122import java .util .Set ;
123+ import java .util .concurrent .TimeUnit ;
122124import java .util .stream .Collectors ;
123125
124126import static java .util .Arrays .asList ;
@@ -627,7 +629,7 @@ protected void assertSnapshotOrGenericThread() {
627629 Releasables .close (releasables );
628630 }
629631 } finally {
630- terminate (threadPool );
632+ terminateSafely (threadPool );
631633 }
632634 }
633635
@@ -744,7 +746,7 @@ public void testClearCache() throws Exception {
744746 }
745747 }
746748 } finally {
747- terminate (threadPool );
749+ terminateSafely (threadPool );
748750 }
749751 }
750752 }
@@ -940,6 +942,17 @@ private SearchableSnapshotRecoveryState createRecoveryState() {
940942 return recoveryState ;
941943 }
942944
945+ // Wait for all operations on the threadpool to complete to make sure we don't leak any reference count releasing and then shut it down
946+ private static void terminateSafely (ThreadPool threadPool ) throws Exception {
947+ assertBusy (() -> {
948+ for (ThreadPoolStats .Stats stat : threadPool .stats ()) {
949+ assertEquals (stat .getActive (), 0 );
950+ assertEquals (stat .getQueue (), 0 );
951+ }
952+ });
953+ assertTrue (ThreadPool .terminate (threadPool , 10 , TimeUnit .SECONDS ));
954+ }
955+
943956 private static class FaultyReadsFileSystem extends FilterFileSystemProvider {
944957 FaultyReadsFileSystem (FileSystem inner ) {
945958 super ("faulty_fs://" , inner );
@@ -956,5 +969,4 @@ public int read(ByteBuffer dst) throws IOException {
956969 };
957970 }
958971 }
959-
960972}
0 commit comments