1616import org .elasticsearch .action .ActionListener ;
1717import org .elasticsearch .common .lucene .Lucene ;
1818import org .elasticsearch .common .lucene .store .InputStreamIndexInput ;
19+ import org .elasticsearch .core .IOUtils ;
1920import org .elasticsearch .core .Nullable ;
2021import org .elasticsearch .core .Releasable ;
2122import 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