Skip to content

Handle remote peer exceptions #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import io.undertow.UndertowMessages;
import io.undertow.io.UndertowOutputStream;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
Expand All @@ -40,6 +41,7 @@
import java.io.OutputStream;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import org.xnio.IoUtils;
Expand All @@ -56,6 +58,9 @@ public enum ConjureExceptions implements ExceptionHandler {
private static final Serializer<ConjureError> serializer =
new ConjureBodySerDe(Collections.singletonList(Encodings.json())).serializer(new TypeMarker<>() {});

private static final String COULD_NOT_READ_CONTENT_LENGTH_DATA_MESSAGE =
UndertowMessages.MESSAGES.couldNotReadContentLengthData().getMessage();

// Log at most once every second
private static final RateLimiter qosLoggingRateLimiter = RateLimiter.create(1);

Expand All @@ -65,24 +70,22 @@ public void handle(HttpServerExchange exchange, Throwable throwable) {
setFailure(exchange, throwable);
if (throwable instanceof CheckedServiceException checkedServiceException) {
checkedServiceException(exchange, checkedServiceException);
} else if (throwable instanceof ServiceException) {
serviceException(exchange, (ServiceException) throwable);
} else if (throwable instanceof QosException) {
qosException(exchange, (QosException) throwable);
} else if (throwable instanceof RemoteException) {
remoteException(exchange, (RemoteException) throwable);
} else if (throwable instanceof ServiceException serviceException) {
serviceException(exchange, serviceException);
} else if (throwable instanceof QosException qosException) {
qosException(exchange, qosException);
} else if (throwable instanceof RemoteException remoteException) {
remoteException(exchange, remoteException);
} else if (throwable instanceof IllegalArgumentException) {
illegalArgumentException(exchange, throwable);
} else if (throwable instanceof FrameworkException) {
frameworkException(exchange, (FrameworkException) throwable);
} else if (throwable instanceof DeadlineExpiredException) {
deadlineExpiredException(exchange, (DeadlineExpiredException) throwable);
} else if (throwable instanceof Error) {
error(exchange, (Error) throwable);
} else if (throwable instanceof IOException && !exchange.getConnection().isOpen()) {
log.info(
"I/O exception from a closed connection. The request may have been aborted by the client",
throwable);
} else if (throwable instanceof FrameworkException frameworkException) {
frameworkException(exchange, frameworkException);
} else if (throwable instanceof DeadlineExpiredException deadlineExpiredException) {
deadlineExpiredException(exchange, deadlineExpiredException);
} else if (throwable instanceof Error error) {
error(exchange, error);
} else if (throwable instanceof IOException ioException) {
ioException(exchange, ioException);
} else {
ServiceException exception = new ServiceException(ErrorType.INTERNAL, throwable);
log(exception, throwable);
Expand Down Expand Up @@ -199,6 +202,31 @@ private static void deadlineExpiredException(HttpServerExchange exchange, Deadli
exception, exchange, UndertowDeadlineReasonResponseEncodingAdapter.INSTANCE);
}

private static void ioException(HttpServerExchange exchange, IOException ioException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Move this below error to match the ordering of the cases above.

if (!exchange.getConnection().isOpen()) {
log.info(
"I/O exception from a closed connection. The request may have been aborted by the client",
ioException);
return;
}

if (Objects.equals(ioException.getMessage(), COULD_NOT_READ_CONTENT_LENGTH_DATA_MESSAGE)) {
log.info(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to not send an error here? Or should it be a 400 error.
For reference, this error message is pulled directly from UndertowMessages

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could not read the full request from the client and client has closed its side of the connection so we can't send a response, but we need to close and clean up our exchange.

"Remote peer closed connection before all data could be read. "
+ "The request may have been aborted by the client.",
ioException);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ioException);
ioException);
IoUtils.safeClose(exchange.getConnection());

IoUtils.safeClose(exchange.getConnection());
return;
}

ServiceException exception = new ServiceException(ErrorType.INTERNAL, ioException);
log(exception, ioException);
writeResponse(
exchange,
Optional.of(ConjureError.fromServiceException(exception)),
exception.getErrorType().httpErrorCode());
}

private static void error(HttpServerExchange exchange, Error error) {
// log errors in order to associate the log line with the correct traceId but
// avoid doing work beyond setting a 500 response code, no response body is sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;

Expand All @@ -39,12 +40,14 @@
import com.palantir.deadlines.DeadlineExpiredException;
import com.palantir.logsafe.SafeArg;
import io.undertow.Undertow;
import io.undertow.UndertowMessages;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.BlockingHandler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
Expand Down Expand Up @@ -361,6 +364,15 @@ public void handlesErrorWithoutRethrowing() {
.doesNotThrowAnyException();
}

@Test
public void handlesPeerRemoteExceptionsWithoutRethrowing() throws IOException {
exception = UndertowMessages.MESSAGES.couldNotReadContentLengthData();
HttpURLConnection connection = execute();
assertThatThrownBy(() -> connection.getResponseCode())
.isInstanceOf(SocketException.class)
.hasMessageContaining("Unexpected end of file from server");
}

private static String getErrorBody(HttpURLConnection connection) {
try (InputStream response = connection.getErrorStream()) {
return new String(response.readAllBytes(), StandardCharsets.UTF_8);
Expand Down