Java 9 HttpClient hanging

I am experimenting with an HTTP/2 client from jdk 9-ea+171 . The code is taken from in this example :

 HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(new URI("https://www.google.com/")) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString()); 

But the client hangs on the last line forever. Please advice how to fix this?

Debugging shows that it waits endlessly in the waitUntilPrefaceSent() method.

+8
java java-9
source share
1 answer

This is a mistake in the latest implementation of the HTTP2 connection assembly. This does not happen with previous builds.

First of all, you need to specify a GET method to avoid getting a null pointer exception.

What happens is that the main thread is waiting for the connection preface to be sent. He locks the counter latch to wait for this introduction. To wake itself up, any HttpClient creates an auxiliary stream that reads incoming traffic. This thread should wake up the main thread, but sometimes it never happens. If you run your example often enough, you will see that this sometimes works. I suppose there is a race to read the preface.

Unfortunately, reading the preface does not take into account any timeouts, so there is no way to wake up the main thread, except for interrupting the main thread.

Here is the official ticket: https://bugs.openjdk.java.net/browse/JDK-8181430

+4
source

All Articles