From c3c79290c6f99fb7ba956f5b733a4a89dff6ceaf Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 29 May 2015 11:59:27 +0200 Subject: [PATCH] Close lock even if we fail to obtain --- .../java/org/elasticsearch/common/lucene/Lucene.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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; }