eclipse-ee4j / eclipse-ee4j/jersey

"Connection pool shut down" using ApacheConnectorProvider and PoolingHttpClientConnectionManager

Open
#4,449 10 comments 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

Just after a dozen or so requests, the client starts failing with this exception:
```
Exception

javax.servlet.ServletException: javax.ws.rs.ProcessingException:
java.lang.IllegalStateException: Connection pool shut down
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:410)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)

Root Cause

javax.ws.rs.ProcessingException: java.lang.IllegalStateException:
Connection pool shut down
org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:528)
org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:296)
org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:609)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:205)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:608)
org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:395)
org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:295)
com.atomgraph.core.client.ClientBase.get(ClientBase.java:101)
com.atomgraph.core.client.SPARQLClient.query(SPARQLClient.java:107)
com.atomgraph.linkeddatahub.server.model.impl.ResourceBase.describe(ResourceBase.java:869)
com.atomgraph.core.model.impl.QueriedResourceBase.get(QueriedResourceBase.java:120)
com.atomgraph.server.model.impl.ResourceBase.get(ResourceBase.java:249)
com.atomgraph.linkeddatahub.server.model.impl.ResourceBase.get(ResourceBase.java:228)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:244)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)
```
I've traced the cause down to the invocation from JDK's `Finalizer`:
```
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.shutdown(PoolingHttpClientConnectionManager.java:408)
at org.apache.http.pool.AbstractConnPool.shutdown(AbstractConnPool.java:144)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.shutdown(PoolingHttpClientConnectionManager.java:411)
at org.apache.http.impl.client.HttpClientBuilder$2.close(HttpClientBuilder.java:1244)
at org.apache.http.impl.client.InternalHttpClient.close(InternalHttpClient.java:201)
at org.glassfish.jersey.apache.connector.ApacheConnector.close(ApacheConnector.java:554)
at org.glassfish.jersey.client.ClientRuntime.close(ClientRuntime.java:364)
at org.glassfish.jersey.client.ClientRuntime.finalize(ClientRuntime.java:341)
at java.lang.System$2.invokeFinalize(System.java:1275)
at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:102)
at java.lang.ref.Finalizer.access$100(Finalizer.java:34)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:217)
```
Seems to be the same issue as https://github.com/spotify/docker-client/issues/446

I've also found this problem (and maybe workaround) mentioned here:
https://github.com/13567370952/docker-java-test/blob/master/src/main/java/com/github/dockerjava/jaxrs/JerseyDockerCmdExecFactory.java#L206
```
connManager = new PoolingHttpClientConnectionManager(getSchemeRegistry(
originalUri, sslContext)) {

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

@Override
public void shutdown() {
// Disable shutdown of the pool. This will be done later, when this factory is closed
// This is a workaround for finalize method on jerseys ClientRuntime which
// closes the client and shuts down the connection pool when it is garbage collected
}
};
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.