Skip to content

Commit c9499c4

Browse files
Nazukin-Dmitryvelogithub-actions[bot]
authored
Closes Response.body() in Logger after rebuffering to avoid resources leak (#2530)
* Closes Response.body() in Logger after rebuffering to avoid resources leak Fixes #2529 * Add test * Update core/src/test/java/feign/LoggerMethodsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update core/src/test/java/feign/LoggerMethodsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Make code style changes --------- Co-authored-by: Dmitrii Nazukin <[email protected]> Co-authored-by: Marvin <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3d1b72c commit c9499c4

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

core/src/main/java/feign/Logger.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 The Feign Authors
2+
* Copyright 2012-2024 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -15,6 +15,7 @@
1515

1616
import static feign.Util.UTF_8;
1717
import static feign.Util.decodeOrDefault;
18+
import static feign.Util.ensureClosed;
1819
import static feign.Util.valuesOrEmpty;
1920
import static java.util.Objects.nonNull;
2021
import java.io.IOException;
@@ -126,6 +127,7 @@ protected Response logAndRebufferResponse(String configKey,
126127
log(configKey, ""); // CRLF
127128
}
128129
byte[] bodyData = Util.toByteArray(response.body().asInputStream());
130+
ensureClosed(response.body());
129131
bodyLength = bodyData.length;
130132
if (logLevel.ordinal() >= Level.FULL.ordinal() && bodyLength > 0) {
131133
log(configKey, "%s", decodeOrDefault(bodyData, UTF_8, "Binary data"));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2024 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign;
15+
16+
import feign.Logger.Level;
17+
import org.junit.jupiter.api.Test;
18+
import java.io.IOException;
19+
import java.util.Collections;
20+
import static feign.Util.UTF_8;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.mockito.Mockito.spy;
23+
import static org.mockito.Mockito.verify;
24+
25+
public class LoggerMethodsTest {
26+
27+
Logger logger = new Logger() {
28+
@Override
29+
protected void log(String configKey, String format, Object... args) {}
30+
};
31+
32+
@Test
33+
void responseIsClosedAfterRebuffer() throws IOException {
34+
Request request =
35+
Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, UTF_8, null);
36+
Response response = Response.builder()
37+
.status(200)
38+
.reason("OK")
39+
.request(request)
40+
.headers(Collections.emptyMap())
41+
.body("some text", UTF_8)
42+
.build();
43+
Response.Body spyBody = spy(response.body());
44+
response = response.toBuilder().body(spyBody).build();
45+
46+
Response rebufferedResponse =
47+
logger.logAndRebufferResponse("someMethod()", Level.FULL, response, 100);
48+
49+
verify(spyBody).close();
50+
assertThat(rebufferedResponse.body()).isNotSameAs(spyBody);
51+
}
52+
}

0 commit comments

Comments
 (0)