Skip to content

Commit

Permalink
Fixed the issue of NPE caused by unexpected exceptions thrown during …
Browse files Browse the repository at this point in the history
…the use of AsyncClient. (#14556) (#14563)

(cherry picked from commit 80e564b)
  • Loading branch information
luoluoyuyu authored Dec 26, 2024
1 parent 739d625 commit c87d6df
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.reflect.InvocationTargetException;
import java.net.ConnectException;
import java.net.SocketException;
import java.util.Optional;

/**
* This class defines the failed interfaces that thrift client needs to support so that the Thrift
Expand Down Expand Up @@ -107,11 +108,17 @@ static void resolveException(Throwable t, ThriftClient o) {
static boolean isConnectionBroken(Throwable cause) {
return (cause instanceof SocketException && cause.getMessage().contains("Broken pipe"))
|| (cause instanceof TTransportException
&& (cause.getMessage().contains("Socket is closed by peer")
|| cause.getMessage().contains("Read call frame size failed")))
&& (hasExpectedMessage(cause, "Socket is closed by peer")
|| hasExpectedMessage(cause, "Read call frame size failed")))
|| (cause instanceof IOException
&& (cause.getMessage().contains("Connection reset by peer")
|| cause.getMessage().contains("Broken pipe")))
|| (cause instanceof ConnectException && cause.getMessage().contains("Connection refused"));
&& (hasExpectedMessage(cause, "Connection reset by peer")
|| hasExpectedMessage(cause, "Broken pipe")))
|| (cause instanceof ConnectException && hasExpectedMessage(cause, "Connection refused"));
}

static boolean hasExpectedMessage(Throwable cause, String expectedMessage) {
return Optional.ofNullable(cause.getMessage())
.map(m -> m.contains(expectedMessage))
.orElse(false);
}
}

0 comments on commit c87d6df

Please sign in to comment.