diff --git a/core/src/main/java/com/linecorp/armeria/client/HttpResponseWrapper.java b/core/src/main/java/com/linecorp/armeria/client/HttpResponseWrapper.java index dfd3a91fb6a..d0112aeeccb 100644 --- a/core/src/main/java/com/linecorp/armeria/client/HttpResponseWrapper.java +++ b/core/src/main/java/com/linecorp/armeria/client/HttpResponseWrapper.java @@ -232,14 +232,16 @@ void close(@Nullable Throwable cause, boolean cancel) { requestAutoAbortDelayMillis, TimeUnit.MILLISECONDS); } - private void closeAction(@Nullable Throwable cause) { + private boolean closeAction(@Nullable Throwable cause) { + final boolean closed; if (cause != null) { - delegate.close(cause); + closed = delegate.tryClose(cause); ctx.logBuilder().endResponse(cause); } else { - delegate.close(); + closed = delegate.tryClose(); ctx.logBuilder().endResponse(); } + return closed; } private void cancelAction(@Nullable Throwable cause) { @@ -262,8 +264,10 @@ private void cancelTimeoutAndLog(@Nullable Throwable cause, boolean cancel) { cancelAction(cause); return; } - if (delegate.isOpen()) { - closeAction(cause); + + // don't log if the cause will be exposed via the response/log + if (delegate.isOpen() && closeAction(cause)) { + return; } // the context has been cancelled either by timeout or by user invocation diff --git a/core/src/main/java/com/linecorp/armeria/common/stream/DefaultStreamMessage.java b/core/src/main/java/com/linecorp/armeria/common/stream/DefaultStreamMessage.java index 856e593c18c..58ae76a7d38 100644 --- a/core/src/main/java/com/linecorp/armeria/common/stream/DefaultStreamMessage.java +++ b/core/src/main/java/com/linecorp/armeria/common/stream/DefaultStreamMessage.java @@ -441,28 +441,41 @@ private void handleCloseEvent(SubscriptionImpl subscription, CloseEvent o) { @Override public void close() { - if (setState(State.OPEN, State.CLOSED)) { - addObjectOrEvent(SUCCESSFUL_CLOSE); - } + tryClose(); } @Override public final void close(Throwable cause) { requireNonNull(cause, "cause"); - if (cause instanceof CancelledSubscriptionException) { - throw new IllegalArgumentException("cause: " + cause + " (must use Subscription.cancel())"); - } tryClose(cause); } + /** + * Tries to close the stream. + * + * @return {@code true} if the stream has been closed by this method call. + * {@code false} if the stream has been closed already by another party. + */ + @UnstableApi + public final boolean tryClose() { + if (setState(State.OPEN, State.CLOSED)) { + addObjectOrEvent(SUCCESSFUL_CLOSE); + return true; + } + return false; + } + /** * Tries to close the stream with the specified {@code cause}. * * @return {@code true} if the stream has been closed by this method call. - * {@code false} if the stream has been closed already by other party. + * {@code false} if the stream has been closed already by another party. */ public final boolean tryClose(Throwable cause) { + if (cause instanceof CancelledSubscriptionException) { + throw new IllegalArgumentException("cause: " + cause + " (must use Subscription.cancel())"); + } if (setState(State.OPEN, State.CLOSED)) { addObjectOrEvent(new CloseEvent(cause)); return true;