diff --git a/src/main/java/org/elasticsearch/common/lucene/Lucene.java b/src/main/java/org/elasticsearch/common/lucene/Lucene.java index e3d787779c7..7154f83c71d 100644 --- a/src/main/java/org/elasticsearch/common/lucene/Lucene.java +++ b/src/main/java/org/elasticsearch/common/lucene/Lucene.java @@ -189,8 +189,16 @@ public class Lucene { @SuppressForbidden(reason = "this method uses trappy Directory#makeLock API") public static Lock acquireLock(Directory directory, String lockName, long timeout) throws IOException { final Lock writeLock = directory.makeLock(lockName); - if (writeLock.obtain(timeout) == false) { - throw new LockObtainFailedException("failed to obtain lock: " + writeLock); + boolean success = false; + try { + if (writeLock.obtain(timeout) == false) { + throw new LockObtainFailedException("failed to obtain lock: " + writeLock); + } + success = true; + } finally { + if (success == false) { + writeLock.close(); + } } return writeLock; }