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 @@ -212,8 +212,12 @@ private void handleResponse(
long readUpTo =
maxResponseBodySize == Long.MAX_VALUE ? Long.MAX_VALUE : maxResponseBodySize + 1;
Buffer buffer = new Buffer();
// The GzipSource wrapper owns a native Inflater that must be released. The underlying
// body.source() is owned by `body` (already in the outer try-with-resources), so only
// close it when we created the wrapper.
Source source = null;
try {
Source source = decompress ? new GzipSource(body.source()) : body.source();
source = decompress ? new GzipSource(body.source()) : body.source();
while (buffer.size() <= maxResponseBodySize) {
long n = source.read(buffer, readUpTo - buffer.size());
if (n == -1L) {
Expand All @@ -222,6 +226,14 @@ private void handleResponse(
}
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to read response body", e);
} finally {
if (decompress && source != null) {
try {
source.close();
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to close gzip response source", e);
}
}
}

if (buffer.size() > maxResponseBodySize) {
Expand Down Expand Up @@ -306,10 +318,12 @@ public MediaType contentType() {
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
if (compressor != null) {
BufferedSink compressedSink =
Okio.buffer(Okio.sink(compressor.compress(bufferedSink.outputStream())));
requestBodyWriter.writeMessage(compressedSink.outputStream());
compressedSink.close();
// writeMessage can throw, which must still close the compressed sink to release the
// underlying GZIPOutputStream's native Deflater.
try (BufferedSink compressedSink =
Okio.buffer(Okio.sink(compressor.compress(bufferedSink.outputStream())))) {
requestBodyWriter.writeMessage(compressedSink.outputStream());
}
} else {
requestBodyWriter.writeMessage(bufferedSink.outputStream());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import com.linecorp.armeria.testing.junit5.server.SelfSignedCertificateExtension;
import io.opentelemetry.exporter.internal.TlsUtil;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.export.Compressor;
import io.opentelemetry.sdk.common.export.HttpResponse;
import io.opentelemetry.sdk.common.export.MessageWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.net.ServerSocket;
import java.net.URI;
import java.nio.charset.StandardCharsets;
Expand All @@ -32,6 +35,7 @@
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import javax.net.ssl.KeyManagerFactory;
Expand All @@ -42,6 +46,9 @@
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -363,6 +370,69 @@ void constructor_usesDefaultTrustManagerWhenTrustManagerIsNull() throws Exceptio
.doesNotThrowAnyException();
}

@Test
void requestBody_compressedWriterThrows_closesCompressedSink() throws Exception {
// A throw from writeMessage used to skip the compressedSink.close() line below it, leaking
// the compressor's native Deflater.
AtomicInteger streamCloseCount = new AtomicInteger();
Compressor trackingCompressor =
new Compressor() {
@Override
public String getEncoding() {
return "fake";
}

@Override
public OutputStream compress(OutputStream outputStream) {
return new OutputStream() {
@Override
public void write(int b) throws IOException {
outputStream.write(b);
}

@Override
public void close() throws IOException {
streamCloseCount.incrementAndGet();
outputStream.close();
}
};
}
};

MessageWriter throwingWriter =
new MessageWriter() {
@Override
public void writeMessage(OutputStream output) throws IOException {
throw new IOException("boom");
}

@Override
public int getContentLength() {
return 0;
}
};

// RequestBodyImpl is a private inner class; construct it via reflection.
Class<?> cls =
Class.forName(
"io.opentelemetry.exporter.sender.okhttp.internal.OkHttpHttpSender$RequestBodyImpl");
Constructor<?> ctor =
cls.getDeclaredConstructor(MessageWriter.class, Compressor.class, MediaType.class);
ctor.setAccessible(true);
RequestBody body =
(RequestBody)
ctor.newInstance(throwingWriter, trackingCompressor, MediaType.parse("text/plain"));

Buffer buffer = new Buffer();
assertThatThrownBy(() -> body.writeTo(buffer))
.isInstanceOf(IOException.class)
.hasMessage("boom");
assertThat(streamCloseCount.get())
.withFailMessage(
"compressed sink was not closed after writeMessage threw; native Deflater leaked")
.isEqualTo(1);
}

@Test
void constructor_wrapsDefaultTrustManagerFailure() throws Exception {
String originalAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
Expand Down
Loading