Skip to content
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 @@ -407,9 +407,12 @@ private HttpResponse toHttpResponse(java.net.http.HttpResponse<InputStream> resp
boolean decompress = "gzip".equalsIgnoreCase(contentEncoding);

byte[] bodyBytes;
try (InputStream rawIs = response.body()) {
// The GZIPInputStream wrapper owns a native Inflater that must be released. Including it
// in the try-with-resources closes it on every path; closing the gzip stream also closes
// the underlying rawIs.
try (InputStream rawIs = response.body();
InputStream is = decompress ? new GZIPInputStream(rawIs) : rawIs) {
// The limit is applied to the decompressed bytes.
InputStream is = decompress ? new GZIPInputStream(rawIs) : rawIs;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4 * 0x400]; // 4KB
int n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.opentelemetry.sdk.common.export.MessageWriter;
import io.opentelemetry.sdk.common.export.RetryPolicy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -44,8 +45,10 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.SSLException;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -553,4 +556,63 @@ public int getContentLength() {
return 0;
}
}

@SuppressWarnings("unchecked")
@Test
void sendInternal_gzipResponse_closesGzipWrapper() throws Exception {
// A valid gzip payload (empty content) is enough — we care about close(), not content.
ByteArrayOutputStream raw = new ByteArrayOutputStream();
try (GZIPOutputStream gz = new GZIPOutputStream(raw)) {
gz.write(new byte[0]);
}
byte[] gzipped = raw.toByteArray();

AtomicInteger closeCount = new AtomicInteger();
java.net.http.HttpResponse<InputStream> mockJdkResponse =
mock(java.net.http.HttpResponse.class);
when(mockJdkResponse.statusCode()).thenReturn(200);
when(mockJdkResponse.body())
.thenReturn(
new ByteArrayInputStream(gzipped) {
@Override
public void close() throws IOException {
closeCount.incrementAndGet();
super.close();
}
});
when(mockJdkResponse.headers())
.thenReturn(
HttpHeaders.of(
Collections.singletonMap("Content-Encoding", Collections.singletonList("gzip")),
(a, b) -> true));
doReturn(mockJdkResponse).when(mockHttpClient).send(any(), any());

JdkHttpSender testSender =
new JdkHttpSender(
mockHttpClient,
URI.create("http://localhost"),
"text/plain",
null,
Duration.ofSeconds(10),
Collections::emptyMap,
null,
null,
Long.MAX_VALUE);

try {
HttpResponse response = testSender.sendInternal(new NoOpRequestBodyWriter());
assertThat(response.getStatusCode()).isEqualTo(200);
// Without closing the GZIPInputStream wrapper the body stream is closed once by the outer
// try-with-resources, leaking the wrapper's native Inflater. With the fix the wrapper is
// also closed, which forwards close() to the underlying stream — so the counter is >= 2.
assertThat(closeCount.get())
.withFailMessage(
"GZIPInputStream wrapper was not closed (close count = %d); its native Inflater"
+ " would leak on every gzip response",
closeCount.get())
.isGreaterThanOrEqualTo(2);
} finally {
testSender.shutdown();
}
}
}
Loading