diff --git a/src/main/java/org/elasticsearch/indices/recovery/RecoveryStatus.java b/src/main/java/org/elasticsearch/indices/recovery/RecoveryStatus.java index 093edffb312..1db369548d9 100644 --- a/src/main/java/org/elasticsearch/indices/recovery/RecoveryStatus.java +++ b/src/main/java/org/elasticsearch/indices/recovery/RecoveryStatus.java @@ -188,10 +188,13 @@ public class RecoveryStatus extends AbstractRefCounted { public void markAsDone() { if (finished.compareAndSet(false, true)) { assert tempFileNames.isEmpty() : "not all temporary files are renamed"; - indexShard.postRecovery("peer recovery done"); - // release the initial reference. recovery files will be cleaned as soon as ref count goes to zero, potentially now - decRef(); - listener.onRecoveryDone(state()); + try { + indexShard.postRecovery("peer recovery done"); + } finally { + // release the initial reference. recovery files will be cleaned as soon as ref count goes to zero, potentially now + decRef(); + listener.onRecoveryDone(state()); + } } }