From fbca68a7c4affcf0a3f03f49abee834e27c2677d Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Fri, 22 Jan 2016 10:15:40 +0100 Subject: [PATCH] Translog: close channel on failures while converting a writer to a reader TranslogWriter.closeIntoReader transfers the file ownership from a writer to a reader and closes the writer. If the transfer fails, we need to make sure we closed the underlying channel as the writer is already closes. See: http://build-us-00.elastic.co/job/es_core_master_regression/4355 Relates to #16142 --- .../org/elasticsearch/index/translog/TranslogWriter.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java b/core/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java index f7d0cd571e8..3ead27f3111 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java +++ b/core/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java @@ -192,7 +192,13 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable { throw e; } if (closed.compareAndSet(false, true)) { - return new TranslogReader(generation, channel, path, firstOperationOffset, getWrittenOffset(), operationCounter); + try { + return new TranslogReader(generation, channel, path, firstOperationOffset, getWrittenOffset(), operationCounter); + } catch (Throwable t) { + // close the channel, as we are closed and failed to create a new reader + IOUtils.closeWhileHandlingException(channel); + throw t; + } } else { throw new AlreadyClosedException("translog [" + getGeneration() + "] is already closed (path [" + path + "]", tragedy); }