Yes, there is a way to clean up a session but it should be used when the instance that acquires the lock is gone. And this is as you foundnd out works for only safe mode.
But if you release a session, all the resources that instance holds will be cleaned up. This is dangerous if the instance is still alive. Someone else could be using another lock/semaphore, and that is cleaned up as well leaving you with a violation of a critical section.
I would suggest handling the cleanup properly on the thread that acquires the lock, which is best handled by the following:
fencedLock.lock();
try {
someWork();
} catch (InterruptedException e) {
} finally {
fencedLock.unlock();
}
Even if we interrupt this thread, the finally block will always run a release the lock itself. Does that work for you?