Skip to content

Commit fbf3013

Browse files
committed
prevent semaphore leakage
1 parent cd64dd0 commit fbf3013

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/main/java/org/cryptomator/cryptofs/inuse/RealUseToken.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.cryptomator.cryptofs.inuse;
22

3-
import org.cryptomator.cryptofs.common.Constants;
43
import org.cryptomator.cryptofs.common.EncryptedChannels;
54
import org.cryptomator.cryptolib.api.Cryptor;
65
import org.slf4j.Logger;
@@ -92,20 +91,25 @@ private void scheduleRefresh(int count) {
9291
}
9392

9493
var delayedExecutor = delayExponentiallyWithCap(tokenPersistor, count);
95-
var nextPersistenceTask = currentTask.thenRunAsync(() -> {
94+
var nextPersistenceTask = currentTask.thenRunAsync(() -> runRefresh(count), delayedExecutor);
95+
tokenPersistenceTask.set(nextPersistenceTask);
96+
}
97+
98+
private void runRefresh(int count) {
99+
try {
100+
CONCURRENT_WRITES_SEMAPHORE.acquire();
96101
try {
97-
CONCURRENT_WRITES_SEMAPHORE.acquire();
98102
refresh();
99-
CONCURRENT_WRITES_SEMAPHORE.release();
100103
scheduleRefresh(count + 1);
101-
} catch (InterruptedException e) {
102-
LOG.debug("Interrupt during refresh of {}. Closing token.", filePath);
103-
close();
104-
Thread.currentThread().interrupt();
105-
throw new RuntimeException(e);
104+
} finally {
105+
CONCURRENT_WRITES_SEMAPHORE.release();
106106
}
107-
}, delayedExecutor);
108-
tokenPersistenceTask.set(nextPersistenceTask);
107+
} catch (InterruptedException e) {
108+
LOG.debug("Interrupt during refresh of {}. Closing token.", filePath);
109+
close();
110+
Thread.currentThread().interrupt();
111+
throw new RuntimeException(e); //mark the completion stage as failed
112+
}
109113
}
110114

111115
private Executor delayExponentiallyWithCap(Executor executor, int count) {

0 commit comments

Comments
 (0)