Search code examples
javaapache-zookeeperapache-curator

Can Apache curator InterProcessSemaphoreMutex release hang ever?


I am using the below recipe in our code to ensure singleton JVM application

 public void acquireLock(InterProcessSemaphoreMutex lock){
   lock.acquire();
 }

 public void releaseLock(InterProcessSemaphoreMutex lock){
      lock.release();
 }

I am releasing lock(via above function call) in 2 conditions:

  1. Work is done.
  2. I got a LOST state change from curator.

Is it ever possible that call to release() may hang?
Also should I add below predicate in releaseLock()?

if (lock.isAcquiredInThisProcess()) {
    lock.release();
}

Solution

  • (Note: I'm the main author of Curator)

    If you aren't currently connected (i.e. are in SUSPENDED/LOST) then lock.release() will hang based on your current retry policy. However, it won't hang indefinitely and will return once the retries have been exhausted. More importantly, internally Curator guarantees that the node associated with the lock/semaphore will get deleted so you should always call this method if you intend to release the lock/semaphore. Once the connection is repaired the node will get deleted and other processes waiting to acquire can continue.