Parcourir la source

Fixed exception when closing channel that was opened with exception

Markus Kreusch il y a 9 ans
Parent
commit
0a1eaa8600

+ 13 - 5
main/filesystem-nio/src/main/java/org/cryptomator/filesystem/nio/SharedFileChannel.java

@@ -40,11 +40,19 @@ class SharedFileChannel {
 	public void open(OpenMode mode) {
 		doLocked(() -> {
 			Thread thread = Thread.currentThread();
-			if (openedBy.put(thread, thread) != null) {
-				throw new IllegalStateException("SharedFileChannel already open for current thread");
-			}
-			if (delegate == null) {
-				createChannel(mode);
+			boolean failed = true;
+			try {
+				if (openedBy.put(thread, thread) != null) {
+					throw new IllegalStateException("SharedFileChannel already open for current thread");
+				}
+				if (delegate == null) {
+					createChannel(mode);
+				}
+				failed = false;
+			} finally {
+				if (failed) {
+					openedBy.remove(thread);
+				}
 			}
 		});
 	}