diff --git a/src/main/java/com/google/devtools/build/lib/query2/AbstractBlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/AbstractBlazeQueryEnvironment.java index 55cb759a8dc790..2ab2758ee38abe 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/AbstractBlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/AbstractBlazeQueryEnvironment.java @@ -48,7 +48,8 @@ * {@link QueryEnvironment} that can evaluate queries to produce a result, and implements as much of * QueryEnvironment as possible while remaining mostly agnostic as to the objects being stored. */ -public abstract class AbstractBlazeQueryEnvironment extends AbstractQueryEnvironment { +public abstract class AbstractBlazeQueryEnvironment extends AbstractQueryEnvironment + implements AutoCloseable { protected ErrorSensingEventHandler eventHandler; protected final boolean keepGoing; protected final boolean strictScope; @@ -78,6 +79,9 @@ protected AbstractBlazeQueryEnvironment( this.extraFunctions = ImmutableList.copyOf(extraFunctions); } + @Override + public abstract void close(); + private static DependencyFilter constructDependencyFilter( Set settings) { DependencyFilter specifiedFilter = diff --git a/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java index 601ae5c0e66b4c..593957330f00e3 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/BlazeQueryEnvironment.java @@ -111,6 +111,11 @@ public class BlazeQueryEnvironment extends AbstractBlazeQueryEnvironment this.labelVisitor = new LabelVisitor(targetProvider, dependencyFilter); } + @Override + public void close() { + // BlazeQueryEnvironment has no resources that need to be cleaned up. + } + @Override public DigraphQueryEvalResult evaluateQuery( QueryExpression expr, diff --git a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java index db78d71e034f8a..fe54c4d033d3f9 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java @@ -232,6 +232,14 @@ protected SkyQueryEnvironment( PrepareDepsOfPatternsFunction.getSkyKeys(universeKey, eventHandler)); } + @Override + public void close() { + if (executor != null) { + executor.shutdownNow(); + executor = null; + } + } + private void beforeEvaluateQuery() throws InterruptedException { if (graph == null || !graphFactory.isUpToDate(universeKey)) { // If this environment is uninitialized or the graph factory needs to evaluate, do so. We