diff --git a/core/trino-main/src/main/java/io/trino/operator/Driver.java b/core/trino-main/src/main/java/io/trino/operator/Driver.java index e1f968d6c4ec..9841fc5d7c73 100644 --- a/core/trino-main/src/main/java/io/trino/operator/Driver.java +++ b/core/trino-main/src/main/java/io/trino/operator/Driver.java @@ -48,6 +48,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Throwables.throwIfUnchecked; +import static com.google.common.base.Verify.verify; import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static io.airlift.concurrent.MoreFutures.getFutureValue; import static io.trino.operator.Operator.NOT_BLOCKED; @@ -698,22 +699,21 @@ private Optional tryWithLock(long timeout, TimeUnit unit, boolean interru return Optional.empty(); } - Optional result; + T result = null; + Throwable failure = null; + try { - result = Optional.of(task.get()); + result = task.get(); + + // opportunistic check to avoid unnecessary lock reacquisition + processNewSources(); + destroyIfNecessary(); + } + catch (Throwable t) { + failure = t; } finally { - try { - try { - processNewSources(); - } - finally { - destroyIfNecessary(); - } - } - finally { - exclusiveLock.unlock(); - } + exclusiveLock.unlock(); } // If there are more assignment updates available, attempt to reacquire the lock and process them. @@ -727,16 +727,40 @@ private Optional tryWithLock(long timeout, TimeUnit unit, boolean interru try { processNewSources(); } - finally { + catch (Throwable t) { + if (failure == null) { + failure = t; + } + else if (failure != t) { + failure.addSuppressed(t); + } + } + + try { destroyIfNecessary(); } + catch (Throwable t) { + if (failure == null) { + failure = t; + } + else if (failure != t) { + failure.addSuppressed(t); + } + } } finally { exclusiveLock.unlock(); } } - return result; + if (failure != null) { + throwIfUnchecked(failure); + // should never happen + throw new AssertionError(failure); + } + + verify(result != null, "result is null"); + return Optional.of(result); } private static class DriverLock