-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a regression where response exceptions are logged unintentionally (…
…#6035) Motivation: We've received reports that the following log message is printed, and also exposed when completing the response exceptionally. ``` Unexpected exception while closing a request: com.linecorp.armeria.client.UnprocessedRequestException: com.linecorp.armeria.client.GoAwayReceivedException at com.linecorp.armeria.client.UnprocessedRequestException.of(UnprocessedRequestException.java:45) at com.linecorp.armeria.client.Http2ResponseDecoder.onStreamClosed(Http2ResponseDecoder.java:146) ``` It seems like this is a regression of the refactoring done at: https://github.com/line/armeria/pull/5800/files#diff-d82983b1c20c5f80257da8c39bb74a80d49e81efceb7a1ce63847776de9b40a9R255 I propose that if an exception completes the corresponding `HttpResponse` and `RequestLog`, then we don't log the exception in `HttpResponseWrapper` Modifications: - `boolean DefaultStreamMessage#tryClose` has been added which signals whether the call closed the `StreamMessage` - the `CancelledSubscriptionException` check has been moved to `tryClose` for more consistent behavior - If `HttpResponseWrapper#closeAction` actually closed the response, then return early so that a log isn't printed - Added a test as a separate module to avoid flakiness due to parallel test runs Result: - Unnecessary logs aren't left from `HttpResponseWrapper` <!-- Visit this URL to learn more about how to write a pull request description: https://armeria.dev/community/developer-guide#how-to-write-pull-request-description -->
- Loading branch information
Showing
6 changed files
with
251 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...nternal-logging/src/test/java/com/linecorp/armeria/client/HttpResponseWrapperLogTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.linecorp.armeria.client; | ||
|
||
import static com.linecorp.armeria.internal.testing.Http2ByteUtil.handleInitialExchange; | ||
import static com.linecorp.armeria.internal.testing.Http2ByteUtil.newClientFactory; | ||
import static com.linecorp.armeria.internal.testing.Http2ByteUtil.readFrame; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.InputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.ClosedSessionException; | ||
import com.linecorp.armeria.common.HttpMethod; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.testing.junit5.common.EventLoopExtension; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.read.ListAppender; | ||
import io.netty.handler.codec.http2.Http2FrameTypes; | ||
|
||
class HttpResponseWrapperLogTest { | ||
|
||
@RegisterExtension | ||
static final EventLoopExtension eventLoop = new EventLoopExtension(); | ||
|
||
private static final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
private static final Logger logger = | ||
(Logger) LoggerFactory.getLogger(HttpResponseWrapper.class); | ||
private static final ListAppender<ILoggingEvent> appender = new ListAppender<>(); | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
appender.setContext(context); | ||
appender.start(); | ||
logger.addAppender(appender); | ||
} | ||
|
||
@AfterEach | ||
void afterEach() { | ||
appender.stop(); | ||
logger.detachAppender(appender); | ||
} | ||
|
||
@Test | ||
void goAwayNotLogged() throws Exception { | ||
try (ServerSocket ss = new ServerSocket(0); | ||
ClientFactory clientFactory = newClientFactory(eventLoop.get())) { | ||
|
||
final int port = ss.getLocalPort(); | ||
|
||
final WebClient client = WebClient.builder("h2c://127.0.0.1:" + port) | ||
.factory(clientFactory) | ||
.build(); | ||
final HttpRequest req = HttpRequest.streaming(HttpMethod.GET, "/"); | ||
final CompletableFuture<AggregatedHttpResponse> resFuture = client.execute(req).aggregate(); | ||
try (Socket s = ss.accept()) { | ||
|
||
final InputStream in = s.getInputStream(); | ||
final BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream()); | ||
handleInitialExchange(in, bos); | ||
|
||
// Read a HEADERS frame. | ||
assertThat(readFrame(in).getByte(3)).isEqualTo(Http2FrameTypes.HEADERS); | ||
|
||
// Send a GOAWAY frame. | ||
bos.write(new byte[] { | ||
0x00, 0x00, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x03, // lastStreamId = 3 | ||
0x00, 0x00, 0x00, 0x00 // errorCode = 0 | ||
}); | ||
bos.flush(); | ||
|
||
// The second request should fail with UnprocessedRequestException | ||
// which has a cause of GoAwayReceivedException. | ||
await().untilAsserted(resFuture::isCompletedExceptionally); | ||
assertThatThrownBy(resFuture::join).isInstanceOf(CompletionException.class) | ||
.hasCauseInstanceOf(ClosedSessionException.class); | ||
|
||
// Read a GOAWAY frame. | ||
assertThat(readFrame(in).getByte(3)).isEqualTo(Http2FrameTypes.GO_AWAY); | ||
|
||
assertThat(in.read()).isEqualTo(-1); | ||
} | ||
} | ||
assertThat(appender.list).allSatisfy(event -> { | ||
assertThat(event.getMessage()) | ||
.doesNotContain(HttpResponseWrapper.UNEXPECTED_EXCEPTION_MSG); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.