curator icon indicating copy to clipboard operation
curator copied to clipboard

[CURATOR-347] Shared read/write lock acquired by one thread cannot be released by another

Open jira-importer opened this issue 9 years ago • 2 comments

Consider the following: lock is being acquired by main thread but released by another thread. This throws an exception:

java.lang.IllegalMonitorStateException: You do not own the lock: /locks/abc
at org.apache.curator.framework.recipes.locks.InterProcessMutex.release(InterProcessMutex.java:140)

Are locks thread specific? That wouldn't make sense. How else can I achieve this? Also would be nice to have a non-reentrant read/write shared lock.

public static void main(String[] args) throws Exception {
final CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(ApplicationProperties.getConfig().getMessagebusSyncServers()).sessionTimeoutMs(5000)
.connectionTimeoutMs(3000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
client.start();
InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client, "/locks/abc");
lock.writeLock().acquire();

Thread r = new Thread() {

@Override
public void run() {
try

{ InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client, "/locks/abc"); lock.writeLock().release();
}

catch (Exception e)

{ e.printStackTrace(); }

}
};
r.start();
r.join();
}


Originally reported by meni, imported from: Shared read/write lock acquired by one thread cannot be released by another
  • status: Open
  • priority: Major
  • resolution: Unresolved
  • imported: 2025-01-21

jira-importer avatar Aug 31 '16 23:08 jira-importer

meni:

Also should add, this works perfectly fine with InterProcessSemaphoreMutex. Question is why are these two behave different...?

I need non-reentrant, read/write lock which are thread agnostic.

jira-importer avatar Aug 31 '16 23:08 jira-importer

cammckenzie:

The InterprocessReadWriteLock works per thread due to it using underlying InterprocessMutexes for the read and write locks.

Currently, I'm unaware of a thread agnostic read / write lock that's part of Curator. I think that it would be possible to refactor the InterprocessReadWriteLock to support both per thread or per JVM locks.

jira-importer avatar Oct 25 '16 21:10 jira-importer