-
Notifications
You must be signed in to change notification settings - Fork 46
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||
|
@@ -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; | ||||||||
|
@@ -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); | ||||||||
|
||||||||
|
@@ -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); | ||||||||
|
@@ -199,6 +202,31 @@ private static void deadlineExpiredException(HttpServerExchange exchange, Deadli | |||||||
exception, exchange, UndertowDeadlineReasonResponseEncodingAdapter.INSTANCE); | ||||||||
} | ||||||||
|
||||||||
private static void ioException(HttpServerExchange exchange, IOException ioException) { | ||||||||
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( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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. | ||||||||
|
There was a problem hiding this comment.
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.