Skip to content

Commit 2ca224a

Browse files
committed
fix: don't throw exception after a successful read of data from a channel when an exception is thrown during close()
1 parent c2d9d47 commit 2ca224a

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/FsIoPolicy.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public ReadData materialize(final long offset, final long length) {
112112
throw new N5Exception.N5IOException("FileLazyRead is already closed.");
113113
}
114114

115+
ReadData readData = null;
115116
try (final FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) {
116117

117118
channel.position(offset);
@@ -129,13 +130,19 @@ public ReadData materialize(final long offset, final long length) {
129130
final byte[] data = new byte[(int) size];
130131
final ByteBuffer buf = ByteBuffer.wrap(data);
131132
channel.read(buf);
132-
return ReadData.from(data);
133+
readData = ReadData.from(data);
133134

134135
} catch (final NoSuchFileException e) {
135136
throw new N5Exception.N5NoSuchKeyException("No such file", e);
136137
} catch (IOException | UncheckedIOException e) {
137-
throw new N5Exception.N5IOException(e);
138+
/* Occasionally (frequently for some source remote mounted file systems) this can throw exceptions during
139+
* `channel.close()` which is called automatically in the try-with-resources block. In this case, we have
140+
* successfully read the data, and we can return it, and ignore the exception.
141+
* */
142+
if (readData == null)
143+
throw new N5Exception.N5IOException(e);
138144
}
145+
return readData;
139146
}
140147

141148
@Override

0 commit comments

Comments
 (0)