diff --git a/server/src/main/java/org/elasticsearch/index/shard/PrimaryReplicaSyncer.java b/server/src/main/java/org/elasticsearch/index/shard/PrimaryReplicaSyncer.java index ad34a1c4f8a7d..e001f1144d901 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/PrimaryReplicaSyncer.java +++ b/server/src/main/java/org/elasticsearch/index/shard/PrimaryReplicaSyncer.java @@ -222,7 +222,24 @@ public void onResponse(ResyncReplicationResponse response) { @Override public void onFailure(Exception e) { if (closed.compareAndSet(false, true)) { - listener.onFailure(e); + executor.execute(new AbstractRunnable() { + @Override + public void onFailure(Exception ex) { + e.addSuppressed(ex); + + // We are on the generic threadpool so shouldn't be rejected, and listener#onFailure shouldn't throw anything, + // so getting here should be impossible. + assert false : e; + + // Notify the listener on the current thread anyway, just in case. + listener.onFailure(e); + } + + @Override + protected void doRun() { + listener.onFailure(e); + } + }); } }