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 @@ -218,11 +218,15 @@ private void endRequest(boolean isFailed) {
onRequestError(-1, writtenBytes, connection.getErrorStream(), e);
}
} finally {
HttpUtils.consumeAndClose(connection);
HttpURLConnection connectionToClose = connection;
connection = null;
os = null;
countingOs = null;
deflater.reset();
try {
HttpUtils.consumeAndClose(connectionToClose);
} finally {
deflater.reset();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package co.elastic.apm.agent.report;

import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import org.stagemonitor.util.IOUtils;

import javax.annotation.Nullable;
Expand All @@ -29,6 +31,8 @@

public class HttpUtils {

private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);

private HttpUtils() {
}

Expand Down Expand Up @@ -57,12 +61,23 @@ public static String readToString(final InputStream inputStream) throws IOExcept
*/
public static void consumeAndClose(@Nullable HttpURLConnection connection) {
if (connection != null) {
IOUtils.consumeAndClose(connection.getErrorStream());
try {
IOUtils.consumeAndClose(connection.getErrorStream());
} catch (RuntimeException e) {
logSuppressedException(e);
}
try {
IOUtils.consumeAndClose(connection.getInputStream());
} catch (IOException ignored) {
// silently ignored
} catch (RuntimeException e) {
logSuppressedException(e);
}
}
}

private static void logSuppressedException(RuntimeException e) {
logger.warn("Suppressed exception while consuming APM Server response: {}", e.getMessage());
logger.debug("Exception while consuming APM Server response", e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.net.HttpURLConnection;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -60,6 +61,26 @@ void consumeAndCloseException() throws IOException {
verify(errorStream).close();
}

@Test
void consumeAndCloseRuntimeException() throws IOException {
HttpURLConnection connection = mock(HttpURLConnection.class);
doThrow(new NullPointerException("broken connection")).when(connection).getInputStream();

assertDoesNotThrow(() -> HttpUtils.consumeAndClose(connection));
}

@Test
void consumeAndCloseResponseAfterErrorStreamRuntimeException() throws IOException {
HttpURLConnection connection = mock(HttpURLConnection.class);
doThrow(new NullPointerException("broken error stream")).when(connection).getErrorStream();
InputStream responseStream = mockEmptyInputStream();
doReturn(responseStream).when(connection).getInputStream();

assertDoesNotThrow(() -> HttpUtils.consumeAndClose(connection));

verify(responseStream).close();
}

@Test
void consumeAndCloseResponseContent() throws IOException {
HttpURLConnection connection = mock(HttpURLConnection.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import co.elastic.apm.agent.impl.transaction.TransactionImpl;
import co.elastic.apm.agent.report.processor.ProcessorEventHandler;
import co.elastic.apm.agent.report.serialize.DslJsonSerializer;
import co.elastic.apm.agent.report.serialize.SerializationConstants;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonWriter;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -49,6 +50,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
Expand All @@ -66,6 +68,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.serviceUnavailable;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

class IntakeV2ReportingEventHandlerTest {
Expand Down Expand Up @@ -104,6 +108,7 @@ void setUp() throws Exception {
final ConfigurationRegistry configurationRegistry = SpyConfiguration.createSpyConfig();
final ReporterConfigurationImpl reporterConfiguration = configurationRegistry.getConfig(ReporterConfigurationImpl.class);
final CoreConfigurationImpl coreConfiguration = configurationRegistry.getConfig(CoreConfigurationImpl.class);
SerializationConstants.init(coreConfiguration);
SystemInfo system = new SystemInfo("x64", "localhost", null, "platform");
final ProcessInfo title = new ProcessInfo("title");
final ServiceImpl service = new ServiceImpl();
Expand Down Expand Up @@ -191,6 +196,25 @@ void testNoopWhenNotConnected() throws Exception {
assertThat(nonConnectedReportingEventHandler.getBufferSize()).isEqualTo(0);
}

@Test
void testCleanupFailureDoesNotRetainConnection() throws Exception {
reportTransaction(reportingEventHandler);
reportingEventHandler.endRequest();

HttpURLConnection failedConnection = mock(HttpURLConnection.class);
doThrow(new AssertionError("cleanup failed")).when(failedConnection).getInputStream();
reportingEventHandler.connection = failedConnection;

assertThatThrownBy(reportingEventHandler::endRequest)
.isInstanceOf(AssertionError.class)
.hasMessage("cleanup failed");
assertThat(reportingEventHandler.connection).isNull();

reportTransaction(reportingEventHandler);
reportingEventHandler.endRequest();
mockApmServer1.verify(2, postRequestedFor(urlEqualTo(INTAKE_V2_URL)));
}

@Test
void testShutDown() throws Exception {
reportTransaction(reportingEventHandler);
Expand Down
Loading