Skip to content

Commit aebfdf1

Browse files
authored
Fix issue with Gradle daemons hanging indefinitely on shutdown (#44867) (#44878)
1 parent 0360516 commit aebfdf1

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClusterCleanupOnShutdown.java

+21-23
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ public class TestClusterCleanupOnShutdown implements Runnable {
1919

2020
private Set<ElasticsearchCluster> clustersToWatch = new HashSet<>();
2121

22-
public void watch(Collection<ElasticsearchCluster> cluster) {
23-
synchronized (clustersToWatch) {
24-
clustersToWatch.addAll(clustersToWatch);
25-
}
22+
public synchronized void watch(Collection<ElasticsearchCluster> clusters) {
23+
clustersToWatch.addAll(clusters);
2624
}
2725

28-
public void unWatch(Collection<ElasticsearchCluster> cluster) {
29-
synchronized (clustersToWatch) {
30-
clustersToWatch.removeAll(clustersToWatch);
31-
}
26+
public synchronized void unWatch(Collection<ElasticsearchCluster> clusters) {
27+
clustersToWatch.removeAll(clusters);
3228
}
3329

3430
@Override
@@ -38,21 +34,23 @@ public void run() {
3834
Thread.sleep(Long.MAX_VALUE);
3935
}
4036
} catch (InterruptedException interrupted) {
41-
synchronized (clustersToWatch) {
42-
if (clustersToWatch.isEmpty()) {
43-
return;
44-
}
45-
logger.info("Cleanup thread was interrupted, shutting down all clusters");
46-
Iterator<ElasticsearchCluster> iterator = clustersToWatch.iterator();
47-
while (iterator.hasNext()) {
48-
ElasticsearchCluster cluster = iterator.next();
49-
iterator.remove();
50-
try {
51-
cluster.stop(false);
52-
} catch (Exception e) {
53-
logger.warn("Could not shut down {}", cluster, e);
54-
}
55-
}
37+
shutdownClusters();
38+
}
39+
}
40+
41+
public synchronized void shutdownClusters() {
42+
if (clustersToWatch.isEmpty()) {
43+
return;
44+
}
45+
logger.info("Cleanup thread was interrupted, shutting down all clusters");
46+
Iterator<ElasticsearchCluster> iterator = clustersToWatch.iterator();
47+
while (iterator.hasNext()) {
48+
ElasticsearchCluster cluster = iterator.next();
49+
iterator.remove();
50+
try {
51+
cluster.stop(false);
52+
} catch (Exception e) {
53+
logger.warn("Could not shut down {}", cluster, e);
5654
}
5755
}
5856
}

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersCleanupExtension.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public TestClustersCleanupExtension() {
3333
executorService.submit(cleanupThread);
3434
}
3535

36-
3736
public static void createExtension(Project project) {
3837
if (project.getRootProject().getExtensions().findByType(TestClustersCleanupExtension.class) != null) {
3938
return;
@@ -43,7 +42,7 @@ public static void createExtension(Project project) {
4342
"__testclusters_rate_limit",
4443
TestClustersCleanupExtension.class
4544
);
46-
Thread shutdownHook = new Thread(ext.cleanupThread::run);
45+
Thread shutdownHook = new Thread(ext.cleanupThread::shutdownClusters);
4746
Runtime.getRuntime().addShutdownHook(shutdownHook);
4847
project.getGradle().buildFinished(buildResult -> {
4948
ext.executorService.shutdownNow();

0 commit comments

Comments
 (0)