Connections leaking with state CLOSE_WAIT with HttpClientException using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocatedCreating a memory leak with Javahow to reuse httpclient connection to get multiple small filesRESTful Authentication via SpringHTTPClient Example - Exception in thread “main” java.lang.NoSuchFieldError: INSTANCEMake HttpClient consume “garbage” before next requestOkhttp javax.net.ssl.sslexception: connection closed by peerGZIP with httpclient 4.5.3Apache HttpClient custom dynamic header on every requestApache HttpClient connection pooling with Tomcat keep-alive
What is this type of notehead called?
Is there a word to describe the feeling of being transfixed out of horror?
Journal losing indexing services
Using a siddur to Daven from in a seforim store
Will adding a BY-SA image to a blog post make the entire post BY-SA?
Do Legal Documents Require Signing In Standard Pen Colors?
How do ground effect vehicles perform turns?
My friend sent me a screenshot of a transaction hash, but when I search for it I find divergent data. What happened?
Is it possible to use .desktop files to open local pdf files on specific pages with a browser?
When quoting, must I also copy hyphens used to divide words that continue on the next line?
In Star Trek IV, why did the Bounty go back to a time when whales are already rare?
Melting point of aspirin, contradicting sources
Folder comparison
THT: What is a squared annular “ring”?
What does this horizontal bar at the first measure mean?
Longest common substring in linear time
Fly on a jet pack vs fly with a jet pack?
Why in book's example is used 言葉(ことば) instead of 言語(げんご)?
Why does the integral domain "being trapped between a finite field extension" implies that it is a field?
We have a love-hate relationship
Have I saved too much for retirement so far?
Can someone explain how this makes sense electrically?
Would it be legal for a US State to ban exports of a natural resource?
Why is Arduino resetting while driving motors?
Connections leaking with state CLOSE_WAIT with HttpClient
Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocatedCreating a memory leak with Javahow to reuse httpclient connection to get multiple small filesRESTful Authentication via SpringHTTPClient Example - Exception in thread “main” java.lang.NoSuchFieldError: INSTANCEMake HttpClient consume “garbage” before next requestOkhttp javax.net.ssl.sslexception: connection closed by peerGZIP with httpclient 4.5.3Apache HttpClient custom dynamic header on every requestApache HttpClient connection pooling with Tomcat keep-alive
We are using JDK11 java.net.http
HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT
, which means the client must close the connection.
From RFC 793 terminology:
CLOSE-WAIT - represents waiting for a connection termination request
from the local user.
This is our client code which runs on WildFly 16 running on Java 12 as a stateless REST API. We don't understand why this is happening.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class SocketSandbox
public static void main(String[] args) throws Exception
HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build();
try (var listener = new ServerSocket(59090))
System.out.println("Server is running...");
while (true)
try (var socket = listener.accept())
HttpRequest request = HttpRequest
.newBuilder(URI.create("<remote_URL>"))
.header("content-type", "application/json").build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
var out = new PrintWriter(socket.getOutputStream(), true);
out.println(String.format("Response HTTP status: %s", response.statusCode()));
We get the "status code" meaning the http response was processed.
When using the same code to call other endpoints connections are fine. It seems to be a particular issue with the remote API we are calling, but still we don't understand why the Java HTTP client is keeping connections opened.
We tried both Windows and Linux machines, and even standalone outside of WildfFly, but the same result happens. After each request, even doing it from our stateless client and receiving the response, each one is left as CLOSE_WAIT
and never close.
Connections will disappear if we shutdown the Java process.
Headers that are sent by the HTTP client:
connection: 'Upgrade, HTTP2-Settings','content-length': '0',
host: 'localhost:3000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA',
upgrade: 'h2c',
user-agent': 'Java-http-client/12'
Server returns response with header: Connection: close
Update (1)
We tried to fine-tune the pool parameters in implementation class jdk.internal.net.http.ConnectionPool
.
It did not solve the problem.
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
Update (2)
With Apache HTTP the connections l getting left in CLOSE_WAIT state for about 90 seconds, but it is able to the connections after that time.
Calling method HttpGet.releaseConnection()
force the connection close immediately.
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://<placeholderdomain>/api/incidents/list");
get.addHeader("content-type", "application/json");
HttpResponse response = client.execute(get);
// This right here did the trick
get.releaseConnection();
return response.getStatusLine().getStatusCode();
And with OkHttp client it worked as expected out of the box, no connections stuck.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://<placeholderdomain>/grb/sb/incidentes/listar")
.header("content-type", "application/json").build();
Response response = client.newCall(request).execute();
return response.body().string();
We are still trying to find how to make it work in java-http-client so that we don't have to rewrite the code.
java java-11 connection-leaks java-http-client
add a comment |
We are using JDK11 java.net.http
HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT
, which means the client must close the connection.
From RFC 793 terminology:
CLOSE-WAIT - represents waiting for a connection termination request
from the local user.
This is our client code which runs on WildFly 16 running on Java 12 as a stateless REST API. We don't understand why this is happening.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class SocketSandbox
public static void main(String[] args) throws Exception
HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build();
try (var listener = new ServerSocket(59090))
System.out.println("Server is running...");
while (true)
try (var socket = listener.accept())
HttpRequest request = HttpRequest
.newBuilder(URI.create("<remote_URL>"))
.header("content-type", "application/json").build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
var out = new PrintWriter(socket.getOutputStream(), true);
out.println(String.format("Response HTTP status: %s", response.statusCode()));
We get the "status code" meaning the http response was processed.
When using the same code to call other endpoints connections are fine. It seems to be a particular issue with the remote API we are calling, but still we don't understand why the Java HTTP client is keeping connections opened.
We tried both Windows and Linux machines, and even standalone outside of WildfFly, but the same result happens. After each request, even doing it from our stateless client and receiving the response, each one is left as CLOSE_WAIT
and never close.
Connections will disappear if we shutdown the Java process.
Headers that are sent by the HTTP client:
connection: 'Upgrade, HTTP2-Settings','content-length': '0',
host: 'localhost:3000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA',
upgrade: 'h2c',
user-agent': 'Java-http-client/12'
Server returns response with header: Connection: close
Update (1)
We tried to fine-tune the pool parameters in implementation class jdk.internal.net.http.ConnectionPool
.
It did not solve the problem.
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
Update (2)
With Apache HTTP the connections l getting left in CLOSE_WAIT state for about 90 seconds, but it is able to the connections after that time.
Calling method HttpGet.releaseConnection()
force the connection close immediately.
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://<placeholderdomain>/api/incidents/list");
get.addHeader("content-type", "application/json");
HttpResponse response = client.execute(get);
// This right here did the trick
get.releaseConnection();
return response.getStatusLine().getStatusCode();
And with OkHttp client it worked as expected out of the box, no connections stuck.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://<placeholderdomain>/grb/sb/incidentes/listar")
.header("content-type", "application/json").build();
Response response = client.newCall(request).execute();
return response.body().string();
We are still trying to find how to make it work in java-http-client so that we don't have to rewrite the code.
java java-11 connection-leaks java-http-client
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago
add a comment |
We are using JDK11 java.net.http
HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT
, which means the client must close the connection.
From RFC 793 terminology:
CLOSE-WAIT - represents waiting for a connection termination request
from the local user.
This is our client code which runs on WildFly 16 running on Java 12 as a stateless REST API. We don't understand why this is happening.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class SocketSandbox
public static void main(String[] args) throws Exception
HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build();
try (var listener = new ServerSocket(59090))
System.out.println("Server is running...");
while (true)
try (var socket = listener.accept())
HttpRequest request = HttpRequest
.newBuilder(URI.create("<remote_URL>"))
.header("content-type", "application/json").build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
var out = new PrintWriter(socket.getOutputStream(), true);
out.println(String.format("Response HTTP status: %s", response.statusCode()));
We get the "status code" meaning the http response was processed.
When using the same code to call other endpoints connections are fine. It seems to be a particular issue with the remote API we are calling, but still we don't understand why the Java HTTP client is keeping connections opened.
We tried both Windows and Linux machines, and even standalone outside of WildfFly, but the same result happens. After each request, even doing it from our stateless client and receiving the response, each one is left as CLOSE_WAIT
and never close.
Connections will disappear if we shutdown the Java process.
Headers that are sent by the HTTP client:
connection: 'Upgrade, HTTP2-Settings','content-length': '0',
host: 'localhost:3000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA',
upgrade: 'h2c',
user-agent': 'Java-http-client/12'
Server returns response with header: Connection: close
Update (1)
We tried to fine-tune the pool parameters in implementation class jdk.internal.net.http.ConnectionPool
.
It did not solve the problem.
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
Update (2)
With Apache HTTP the connections l getting left in CLOSE_WAIT state for about 90 seconds, but it is able to the connections after that time.
Calling method HttpGet.releaseConnection()
force the connection close immediately.
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://<placeholderdomain>/api/incidents/list");
get.addHeader("content-type", "application/json");
HttpResponse response = client.execute(get);
// This right here did the trick
get.releaseConnection();
return response.getStatusLine().getStatusCode();
And with OkHttp client it worked as expected out of the box, no connections stuck.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://<placeholderdomain>/grb/sb/incidentes/listar")
.header("content-type", "application/json").build();
Response response = client.newCall(request).execute();
return response.body().string();
We are still trying to find how to make it work in java-http-client so that we don't have to rewrite the code.
java java-11 connection-leaks java-http-client
We are using JDK11 java.net.http
HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT
, which means the client must close the connection.
From RFC 793 terminology:
CLOSE-WAIT - represents waiting for a connection termination request
from the local user.
This is our client code which runs on WildFly 16 running on Java 12 as a stateless REST API. We don't understand why this is happening.
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class SocketSandbox
public static void main(String[] args) throws Exception
HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build();
try (var listener = new ServerSocket(59090))
System.out.println("Server is running...");
while (true)
try (var socket = listener.accept())
HttpRequest request = HttpRequest
.newBuilder(URI.create("<remote_URL>"))
.header("content-type", "application/json").build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
var out = new PrintWriter(socket.getOutputStream(), true);
out.println(String.format("Response HTTP status: %s", response.statusCode()));
We get the "status code" meaning the http response was processed.
When using the same code to call other endpoints connections are fine. It seems to be a particular issue with the remote API we are calling, but still we don't understand why the Java HTTP client is keeping connections opened.
We tried both Windows and Linux machines, and even standalone outside of WildfFly, but the same result happens. After each request, even doing it from our stateless client and receiving the response, each one is left as CLOSE_WAIT
and never close.
Connections will disappear if we shutdown the Java process.
Headers that are sent by the HTTP client:
connection: 'Upgrade, HTTP2-Settings','content-length': '0',
host: 'localhost:3000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA',
upgrade: 'h2c',
user-agent': 'Java-http-client/12'
Server returns response with header: Connection: close
Update (1)
We tried to fine-tune the pool parameters in implementation class jdk.internal.net.http.ConnectionPool
.
It did not solve the problem.
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
Update (2)
With Apache HTTP the connections l getting left in CLOSE_WAIT state for about 90 seconds, but it is able to the connections after that time.
Calling method HttpGet.releaseConnection()
force the connection close immediately.
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://<placeholderdomain>/api/incidents/list");
get.addHeader("content-type", "application/json");
HttpResponse response = client.execute(get);
// This right here did the trick
get.releaseConnection();
return response.getStatusLine().getStatusCode();
And with OkHttp client it worked as expected out of the box, no connections stuck.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://<placeholderdomain>/grb/sb/incidentes/listar")
.header("content-type", "application/json").build();
Response response = client.newCall(request).execute();
return response.body().string();
We are still trying to find how to make it work in java-http-client so that we don't have to rewrite the code.
java java-11 connection-leaks java-http-client
java java-11 connection-leaks java-http-client
edited 2 days ago
BonanzaOne
asked Mar 20 at 22:37
BonanzaOneBonanzaOne
3,01442767
3,01442767
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago
add a comment |
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
only apply to HTTP/1.1 connections, not HTTP/2. Also take care that these properties are only read once at class loading time. So setting them after
the java.net.http
classes have been loaded will have no effect.
Finally it may take some time after an HttpClient
is released before all kept alive connections are closed - the internal mechanism to do so is basically relying on GC - and this is not very friendly with short lived HttpClients.
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 withHttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE anyjava.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that theHttpClient
may be closing the underlyingSocketChannel
- without going through the SSL graceful shutdown.
– daniel
2 days ago
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55271192%2fconnections-leaking-with-state-close-wait-with-httpclient%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
only apply to HTTP/1.1 connections, not HTTP/2. Also take care that these properties are only read once at class loading time. So setting them after
the java.net.http
classes have been loaded will have no effect.
Finally it may take some time after an HttpClient
is released before all kept alive connections are closed - the internal mechanism to do so is basically relying on GC - and this is not very friendly with short lived HttpClients.
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 withHttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE anyjava.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that theHttpClient
may be closing the underlyingSocketChannel
- without going through the SSL graceful shutdown.
– daniel
2 days ago
|
show 5 more comments
I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
only apply to HTTP/1.1 connections, not HTTP/2. Also take care that these properties are only read once at class loading time. So setting them after
the java.net.http
classes have been loaded will have no effect.
Finally it may take some time after an HttpClient
is released before all kept alive connections are closed - the internal mechanism to do so is basically relying on GC - and this is not very friendly with short lived HttpClients.
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 withHttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE anyjava.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that theHttpClient
may be closing the underlyingSocketChannel
- without going through the SSL graceful shutdown.
– daniel
2 days ago
|
show 5 more comments
I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
only apply to HTTP/1.1 connections, not HTTP/2. Also take care that these properties are only read once at class loading time. So setting them after
the java.net.http
classes have been loaded will have no effect.
Finally it may take some time after an HttpClient
is released before all kept alive connections are closed - the internal mechanism to do so is basically relying on GC - and this is not very friendly with short lived HttpClients.
I wouldn't recommend creating a new client for every new request. This is defeating the purpose of HTTP/2 which allows multiplexing requests on a single connection.
The second thing is that the two properties:
System.setProperty("jdk.httpclient.keepalive.timeout", "5"); // seconds
System.setProperty("jdk.httpclient.connectionPoolSize", "1");
only apply to HTTP/1.1 connections, not HTTP/2. Also take care that these properties are only read once at class loading time. So setting them after
the java.net.http
classes have been loaded will have no effect.
Finally it may take some time after an HttpClient
is released before all kept alive connections are closed - the internal mechanism to do so is basically relying on GC - and this is not very friendly with short lived HttpClients.
answered 2 days ago
danieldaniel
37010
37010
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 withHttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE anyjava.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that theHttpClient
may be closing the underlyingSocketChannel
- without going through the SSL graceful shutdown.
– daniel
2 days ago
|
show 5 more comments
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 withHttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE anyjava.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that theHttpClient
may be closing the underlyingSocketChannel
- without going through the SSL graceful shutdown.
– daniel
2 days ago
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
The production code uses a single client, I shortened the code to make in simple for this thread. Also connections are getting stuck forever. About this API behavior, that's another thing I find annoying because I also found out about this behavior but I had to dig into the code. Is there any documentation about all of it? I was not able to find it. Also how can I force HTTP/1.1 to see what happens?
– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 with
HttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE any java.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.– BonanzaOne
2 days ago
Ok I found how to set HTTP/1.1 with
HttpClient.version(Version.HTTP_1_1)
and set the properties BEFORE any java.net.http
loading, and it did not work. I am running only those lines of code in a clean environment. Also notified GC and it should be cleaning it right away. Nothing is happening.– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
BTW, the remote server does NOT support HTTP/2, so I was using HTTP/1.1 all along because of the implementation automatic downgrade.
– BonanzaOne
2 days ago
1
1
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
There are a couple of fixes like this one bugs.openjdk.java.net/browse/JDK-8217094 that have not made it to 12 GA.
– daniel
2 days ago
1
1
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that the
HttpClient
may be closing the underlying SocketChannel
- without going through the SSL graceful shutdown.– daniel
2 days ago
The debug logs are very verbose and there could be cases where the EOF/EOFException might be expected. Anyway - thanks for submitting an issue. Note that the connection pool is probably not involved here. One thing that could be happenning is that the
HttpClient
may be closing the underlying SocketChannel
- without going through the SSL graceful shutdown.– daniel
2 days ago
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55271192%2fconnections-leaking-with-state-close-wait-with-httpclient%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I believe you are expected to close the response input stream
– user207421
Mar 21 at 22:12
@user207421 I did not find an API method to do so. Can you demonstrate how it can be done with this API?
– BonanzaOne
2 days ago