From e7c9ff7d7dddfa1e2c6c465d927afa14715061c4 Mon Sep 17 00:00:00 2001 From: udaysagar2177 Date: Tue, 18 Sep 2018 12:24:58 -0700 Subject: [PATCH] [CURATOR-468] release acquired leadership mutex properly --- .../framework/recipes/leader/LeaderSelector.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java index 6ad105309e..e7abc95eed 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java @@ -444,6 +444,11 @@ void doWork() throws Exception if ( hasLeadership ) { hasLeadership = false; + // The following clears and returns the interrupt status. One reason a + // thread may be here is because it was interrupted. However the interrupt + // being set will cause releasing the mutex to fail, so clear the interrupt + // status. + boolean wasInterruped = Thread.currentThread().interrupted(); try { mutex.release(); @@ -454,6 +459,13 @@ void doWork() throws Exception log.error("The leader threw an exception", e); // ignore errors - this is just a safety } + finally + { + if ( wasInterruped ) + { + Thread.currentThread().interrupt(); + } + } } } }