Skip to content

Commit f4d47bc

Browse files
committed
Fix local primary file reader open and close order
Always releasing reference and ensure no unexpected assertion error leading to uncompleted listener
1 parent 036c16d commit f4d47bc

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

server/src/main/java/org/elasticsearch/repositories/LocalPrimarySnapshotShardContext.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.action.ActionListener;
1717
import org.elasticsearch.common.lucene.Lucene;
1818
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
19+
import org.elasticsearch.core.IOUtils;
1920
import org.elasticsearch.core.Nullable;
2021
import org.elasticsearch.core.Releasable;
2122
import org.elasticsearch.core.Releasables;
@@ -185,19 +186,26 @@ public void failStoreIfCorrupted(Exception e) {
185186

186187
@Override
187188
public SnapshotShardContext.FileReader fileReader(String file, StoreFileMetadata metadata) throws IOException {
188-
return new FileReader(file, metadata);
189+
Releasable commitRefReleasable = null;
190+
IndexInput indexInput = null;
191+
try {
192+
commitRefReleasable = withCommitRef();
193+
indexInput = store.openVerifyingInput(file, IOContext.DEFAULT, metadata);
194+
return new FileReader(commitRefReleasable, indexInput);
195+
} catch (Exception e) {
196+
IOUtils.close(e, indexInput, commitRefReleasable);
197+
throw e;
198+
}
189199
}
190200

191-
class FileReader implements SnapshotShardContext.FileReader {
201+
static class FileReader implements SnapshotShardContext.FileReader {
192202

193-
private final String file;
194203
private final Releasable commitRefReleasable;
195204
private final IndexInput indexInput;
196205

197-
FileReader(String file, StoreFileMetadata metadata) throws IOException {
198-
this.file = file;
199-
this.commitRefReleasable = withCommitRef();
200-
this.indexInput = store.openVerifyingInput(file, IOContext.DEFAULT, metadata);
206+
FileReader(Releasable commitRefReleasable, IndexInput indexInput) {
207+
this.commitRefReleasable = commitRefReleasable;
208+
this.indexInput = indexInput;
201209
}
202210

203211
@Override
@@ -207,8 +215,7 @@ public InputStream openInput(long limit) throws IOException {
207215

208216
@Override
209217
public void close() throws IOException {
210-
commitRefReleasable.close();
211-
indexInput.close();
218+
IOUtils.close(indexInput, commitRefReleasable);
212219
}
213220

214221
@Override

0 commit comments

Comments
 (0)