diff --git a/fdb-record-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/directory/FDBDirectoryLockFactory.java b/fdb-record-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/directory/FDBDirectoryLockFactory.java index ca57a79140..05d09363e4 100644 --- a/fdb-record-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/directory/FDBDirectoryLockFactory.java +++ b/fdb-record-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/directory/FDBDirectoryLockFactory.java @@ -92,6 +92,8 @@ protected static class FDBDirectoryLock extends Lock { */ private FDBRecordContext closingContext = null; private final Object fileLockSetLock = new Object(); + private boolean clearingLockNow = false; + private final Object clearingLockNowLock = new Object(); private FDBDirectoryLock(final AgilityContext agilityContext, final String lockName, byte[] fileLockKey, int timeWindowMilliseconds) { this.agilityContext = agilityContext; @@ -210,6 +212,17 @@ private void fileLockCheckNewLock(byte[] val, long nowMillis) { } private void fileLockClearFlushAndClose(boolean isRecovery) { + if (clearingLockNow) { + // Here: this function is being called from too many paths. Until cleanup, this guard is here to avoid recursions + return; + } + synchronized (clearingLockNowLock) { + // repeat under lock + if (clearingLockNow) { + return; + } + clearingLockNow = true; + } Function> fileLockFunc = aContext -> aContext.ensureActive().get(fileLockKey) .thenAccept(val -> { @@ -224,6 +237,12 @@ private void fileLockClearFlushAndClose(boolean isRecovery) { throw new AlreadyClosedException("FileLock: Expected to be locked during close.This=" + this + " existingUuid=" + existingUuid); // The string append methods should handle null arguments. } } + }) + .whenComplete((res, err) -> { + synchronized (clearingLockNowLock) { + // clearing under lock to avoid spotbugsMain's "Inconsistent synchronization" issue. + clearingLockNow = false; + } }); if (agilityContext.isClosed()) {