Skip to content

Commit b8a9640

Browse files
committed
Avoid class cast exception from index writer (#28989)
When an index writer encounters a tragic exception, it could be a Throwable and not an Exception. Yet we blindly cast the tragic exception to an Exception which can encounter a ClassCastException. This commit addresses this by checking if the tragic exception is an Exception and otherwise wrapping the Throwable in a RuntimeException if it is not. We choose to wrap the Throwable instead of passing it around because passing it around leads to changing a lot of places where we handle Exception to handle Throwable instead. In general, we have tried to avoid handling Throwable and instead let those bubble up to the uncaught exception handler.
1 parent d82b13e commit b8a9640

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,13 @@ private boolean failOnTragicEvent(AlreadyClosedException ex) {
17691769
// we need to fail the engine. it might have already been failed before
17701770
// but we are double-checking it's failed and closed
17711771
if (indexWriter.isOpen() == false && indexWriter.getTragicException() != null) {
1772-
failEngine("already closed by tragic event on the index writer", (Exception) indexWriter.getTragicException());
1772+
final Exception tragicException;
1773+
if (indexWriter.getTragicException() instanceof Exception) {
1774+
tragicException = (Exception) indexWriter.getTragicException();
1775+
} else {
1776+
tragicException = new RuntimeException(indexWriter.getTragicException());
1777+
}
1778+
failEngine("already closed by tragic event on the index writer", tragicException);
17731779
engineFailed = true;
17741780
} else if (translog.isOpen() == false && translog.getTragicException() != null) {
17751781
failEngine("already closed by tragic event on the translog", translog.getTragicException());

0 commit comments

Comments
 (0)