Fix race between completion/fail in BackgroundHiveSplitLoader
@@ -77,6 +77,7 @@ import static com.facebook.presto.hive.metastore.MetastoreUtil.getHiveSchema;
import static com.facebook.presto.hive.util.ConfigurationUtils.toJobConf;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.slice.Slices.utf8Slice;
import static java.lang.String.format;
@@ -174,12 +175,18 @@ public class BackgroundHiveSplitLoader
if (stopped) {
return TaskStatus.finished();
}
- try {
CompletableFuture<?> future;
taskExecutionLock.readLock().lock();
try {
future = loadSplits();
}
+ catch (Exception e) {
+ // Fail the split source before releasing the execution lock
+ // Otherwise, a race could occur where the split source is completed before we fail it.
+ hiveSplitSource.fail(e);
+ checkState(stopped);
+ return TaskStatus.finished();
+ }
finally {
taskExecutionLock.readLock().unlock();
}
@@ -188,10 +195,6 @@ public class BackgroundHiveSplitLoader
return TaskStatus.continueOn(future);
}
}
- catch (Exception e) {
- hiveSplitSource.fail(e);
- }
- }
}
}
