Skip to content

Commit cd7bf09

Browse files
committed
close executor services on filesystem.close()
1 parent 819a414 commit cd7bf09

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

src/main/java/org/cryptomator/cryptofs/CryptoFileSystemImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ public void close() throws IOException {
213213
open = false;
214214
finallyUtil.guaranteeInvocationOf( //
215215
() -> cryptoFileSystems.remove(this), //
216-
() -> openCryptoFiles.close(), //
217-
() -> directoryStreamFactory.close(), //
218-
() -> cryptor.destroy());
216+
openCryptoFiles::close, //
217+
directoryStreamFactory::close, //
218+
inUseManager::close, //
219+
cryptor::destroy //
220+
);
219221
}
220222
}
221223

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

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

3+
import java.io.Closeable;
34
import java.nio.file.Path;
45
import java.util.Optional;
56

@@ -11,7 +12,7 @@
1112
* <li>ignore the in-use-file for a ciphertext path</li>
1213
* </ul>
1314
*/
14-
public interface InUseManager {
15+
public interface InUseManager extends Closeable {
1516

1617
/**
1718
* Checks if the given ciphertext path is used by others.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ public void ignoreInUse(Path ciphertextPath) {
213213
ignoredInUseFiles.put(inUseFilePath, Boolean.TRUE);
214214
}
215215

216+
@Override
217+
public void close() throws IOException {
218+
tokenRefresher.shutdown();
219+
tokenPersistor.shutdown();
220+
try {
221+
if (!tokenRefresher.awaitTermination(5, TimeUnit.SECONDS)) {
222+
tokenRefresher.shutdownNow();
223+
}
224+
if (!tokenPersistor.awaitTermination(5, TimeUnit.SECONDS)) {
225+
tokenPersistor.shutdownNow();
226+
}
227+
} catch (InterruptedException e) {
228+
Thread.currentThread().interrupt();
229+
tokenRefresher.shutdownNow();
230+
tokenPersistor.shutdownNow();
231+
}
232+
233+
}
234+
216235
/**
217236
* @param p a path with a filename ending with {@value Constants#CRYPTOMATOR_FILE_SUFFIX}
218237
* @return a sibling path with the file extension {@value Constants#INUSE_FILE_SUFFIX}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.cryptomator.cryptofs.inuse;
22

3+
import java.io.IOException;
4+
35
public class StubInUseManager implements InUseManager {
46

7+
@Override
8+
public void close() throws IOException {
9+
10+
}
511
}

src/test/java/org/cryptomator/cryptofs/CryptoFileSystemImplTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ public void setup() {
288288
((RunnableThrowingException<?>) runnable).run();
289289
}
290290
return null;
291-
}).when(finallyUtil).guaranteeInvocationOf(any(RunnableThrowingException.class), any(RunnableThrowingException.class), any(RunnableThrowingException.class), any(RunnableThrowingException.class));
291+
}).when(finallyUtil).guaranteeInvocationOf(any(RunnableThrowingException.class),
292+
any(RunnableThrowingException.class),
293+
any(RunnableThrowingException.class),
294+
any(RunnableThrowingException.class),
295+
any(RunnableThrowingException.class));
292296
}
293297

294298
@Test
@@ -298,6 +302,13 @@ public void testCloseRemovesThisFromCryptoFileSystems() throws IOException {
298302
verify(cryptoFileSystems).remove(inTest);
299303
}
300304

305+
@Test
306+
public void testClosesInUseManager() throws IOException {
307+
inTest.close();
308+
309+
verify(inUseManager).close();
310+
}
311+
301312
@Test
302313
public void testCloseDestroysCryptor() throws IOException {
303314
inTest.close();
@@ -353,7 +364,11 @@ public void setup() {
353364
((RunnableThrowingException<?>) runnable).run();
354365
}
355366
return null;
356-
}).when(finallyUtil).guaranteeInvocationOf(any(RunnableThrowingException.class), any(RunnableThrowingException.class), any(RunnableThrowingException.class), any(RunnableThrowingException.class));
367+
}).when(finallyUtil).guaranteeInvocationOf(any(RunnableThrowingException.class), //
368+
any(RunnableThrowingException.class), //
369+
any(RunnableThrowingException.class), //
370+
any(RunnableThrowingException.class), //
371+
any(RunnableThrowingException.class));
357372
}
358373

359374
@Test

0 commit comments

Comments
 (0)