Skip to content

Properly close the Apache response so that connections can be reused #3861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
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 @@ -462,7 +462,7 @@ public ClientResponse apply(final ClientRequest clientRequest) throws Processing
}

try {
responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response)));
responseContext.setEntityStream(getInputStream(response));
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, null, e);
}
Expand Down Expand Up @@ -601,18 +601,6 @@ private static Map<String, String> writeOutBoundHeaders(final MultivaluedMap<Str
return stringHeaders;
}

private static final class HttpClientResponseInputStream extends FilterInputStream {

HttpClientResponseInputStream(final InputStream inputStream) throws IOException {
super(inputStream);
}

@Override
public void close() throws IOException {
super.close();
}
}

private static InputStream getInputStream(final CloseableHttpResponse response) throws IOException {

final InputStream inputStream;
Expand All @@ -631,8 +619,13 @@ private static InputStream getInputStream(final CloseableHttpResponse response)
return new FilterInputStream(inputStream) {
@Override
public void close() throws IOException {
response.close();
super.close();
try {
super.close();
} catch (IOException ex) {
// Ignore
} finally {
response.close();
}
Comment on lines +622 to +628
Copy link

@isopropylcyanide isopropylcyanide Aug 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @agherardi for fixing this

2 years down the line and because of a missing response.close() in previous versions of Jersey in a finally block, the pooled http connection was never released.

Our leased http connections never were returned to the pool because response wasn't being explicitly read into entities in some cases.

Our request threads talked to a database and had written few rows (REPEATABLE_READ). As a result, the JDBI handle was never closed (thread was in PARKED state due to no more available connections in the pool) and exclusive locks were held as the request was neither rolled back nor committed but suspended.

We have finally figured out the root cause. Thank you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please understand that this also depends on the Apache HTTP Client version. See also a followup discussion.

}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.Response;
import javax.inject.Singleton;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.server.ChunkedOutput;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
Expand All @@ -40,14 +43,16 @@
* @author Petr Janouch (petr.janouch at oracle.com)
*/
public class StreamingTest extends JerseyTest {
private PoolingHttpClientConnectionManager connectionManager;

/**
* Test that a data stream can be terminated from the client side.
*/
@Test
public void clientCloseTest() throws IOException {
// start streaming
InputStream inputStream = target().path("/streamingEndpoint").request().get(InputStream.class);
InputStream inputStream = target().path("/streamingEndpoint").request()
.property(ClientProperties.READ_TIMEOUT, 1_000).get(InputStream.class);

WebTarget sendTarget = target().path("/streamingEndpoint/send");
// trigger sending 'A' to the stream; OK is sent if everything on the server was OK
Expand All @@ -61,8 +66,35 @@ public void clientCloseTest() throws IOException {
assertEquals("NOK", sendTarget.request().get().readEntity(String.class));
}

/**
* Tests that closing a response after completely reading the entity reuses the connection
*/
@Test
public void reuseConnectionTest() throws IOException {
Response response = target().path("/streamingEndpoint/get").request().get();
InputStream is = response.readEntity(InputStream.class);
byte[] buf = new byte[8192];
is.read(buf);
is.close();
response.close();

assertEquals(1, connectionManager.getTotalStats().getAvailable());
assertEquals(0, connectionManager.getTotalStats().getLeased());
}

/**
* Tests that closing a request without reading the entity does not throw an exception.
*/
@Test
public void clientCloseThrowsNoExceptionTest() throws IOException {
Response response = target().path("/streamingEndpoint/get").request().get();
response.close();
}

@Override
protected void configureClient(ClientConfig config) {
connectionManager = new PoolingHttpClientConnectionManager();
config.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
config.connectorProvider(new ApacheConnectorProvider());
}

Expand Down Expand Up @@ -94,5 +126,12 @@ public String sendEvent() {
public ChunkedOutput<String> get() {
return output;
}

@GET
@Path("get")
@Produces(MediaType.TEXT_PLAIN)
public String getString() {
return "OK";
}
}
}