diff --git a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java index f03fa0b1014e..8db805596a8e 100644 --- a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java +++ b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java @@ -319,8 +319,8 @@ private long acquireLock() throws UnknownHostException, TException, InterruptedE long duration = 0; boolean timeout = false; - if (state.get().equals(LockState.WAITING)) { - try { + try { + if (state.get().equals(LockState.WAITING)) { // Retry count is the typical "upper bound of retries" for Tasks.run() function. In fact, the maximum number of // attempts the Tasks.run() would try is `retries + 1`. Here, for checking locks, we use timeout as the // upper bound of retries. So it is just reasonable to set a large retry count. However, if we set @@ -348,9 +348,13 @@ private long acquireLock() throws UnknownHostException, TException, InterruptedE LOG.warn("Interrupted while waiting for lock.", e); } }, TException.class); - } catch (WaitingForLockException waitingForLockException) { - timeout = true; - duration = System.currentTimeMillis() - start; + } + } catch (WaitingForLockException waitingForLockException) { + timeout = true; + duration = System.currentTimeMillis() - start; + } finally { + if (!state.get().equals(LockState.ACQUIRED)) { + unlock(Optional.of(lockId)); } }