Skip to content

8340182: Java HttpClient does not follow default retry limit of 3 retries #25490

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -328,7 +328,7 @@ public HttpRequestImpl response(Response r) throws IOException {
req = HttpRequestImpl.newInstanceForAuthentication(req);
addBasicCredentials(req, proxy, pw, isUTF8);
return req;
} else if (au.retries > retry_limit) {
} else if (au.retries >= retry_limit) {
throw new IOException("too many authentication attempts. Limit: " +
retry_limit);
} else {
Expand Down
116 changes: 116 additions & 0 deletions test/jdk/java/net/httpclient/HttpClientRetryLimitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/


/*
* @test
* @bug 8340182
* @summary Retry limit system property
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer
* @run junit HttpClientRetryLimitTest
* @run junit/othervm -Djdk.httpclient.auth.retrylimit=1 HttpClientRetryLimitTest
Copy link
Member

Choose a reason for hiding this comment

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

Do you think adding a retrylimit=0 would be beneficial too? This way every scenario would be covered

Copy link
Author

Choose a reason for hiding this comment

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

Added additional test case scenarios in 18bac9f

Copy link
Contributor

Choose a reason for hiding this comment

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

@dfuch, shall we test against zero and negative values too? (Both are accepted by AuthenticationFilter as valid.)

Copy link
Member

Choose a reason for hiding this comment

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

We could. We could also throw in garbage to see what happens.

Copy link
Author

Choose a reason for hiding this comment

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

New test scenarios have been added in 18bac9f. We have default, positive, zero and negative values.

* @run junit/othervm -Djdk.httpclient.auth.retrylimit=0 HttpClientRetryLimitTest
* @run junit/othervm -Djdk.httpclient.auth.retrylimit=-1 HttpClientRetryLimitTest
*/

import jdk.httpclient.test.lib.common.HttpServerAdapters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;


class HttpClientRetryLimitTest implements HttpServerAdapters {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we rename this to HttpClientAuthRetryLimit, since there are several other retry limits?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds reasonable to rename - and fix the @summary in the jtreg headers too


// This is the system default value for jdk.httpclient.auth.retrylimit
private static final int DEFAULT_RETRY_LIMIT = 3;
private static final int RETRY_LIMIT = Integer.getInteger(
"jdk.httpclient.auth.retrylimit", DEFAULT_RETRY_LIMIT);

static Stream<HttpClient.Version> args() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should also test against with SSL and without SSL cases. See HttpResponseLimitingTest.ServerClientPair for inspiration.

return Stream.of(
HttpClient.Version.HTTP_1_1,
HttpClient.Version.HTTP_2
);
}

@ParameterizedTest
@MethodSource("args")
public void testDefaultSystemProperty(HttpClient.Version version) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

I see you made the class package-private in 18bac9f. You could have additionally made the method package-private too.

AtomicInteger requestCount = new AtomicInteger(0);

try (HttpTestServer httpTestServer = HttpTestServer.create(version)) {

HttpTestHandler httpTestHandler = t -> {
t.getResponseHeaders()
.addHeader("WWW-Authenticate", "Basic realm=\"Test\"");
t.sendResponseHeaders(401,0);
};

httpTestServer.addHandler(httpTestHandler, "/");
Copy link
Contributor

Choose a reason for hiding this comment

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

In the past, there has been occasions in CI where a test server received connections from a totally unrelated test running in parallel on the same host. To avoid such mishaps, we better salt the path a bit by, say, appending the class' simple name?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the catch - the class simple name has been appended in 18bac9f

Copy link
Contributor

Choose a reason for hiding this comment

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

@p-nima, the handler still accepts all calls to /, you only salted the request URI. Would you mind doing the same in httpTestServer.addHandler(...) too, please? (You better create a String requestPath variable and use it both at the handler and the request.)

httpTestServer.start();
try (
HttpClient client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
Copy link
Contributor

Choose a reason for hiding this comment

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

To ensure the client will fire the request using the protocol version of our preference, could you also pass version to the client builder too, please?

@Override
protected PasswordAuthentication getPasswordAuthentication() {
requestCount.incrementAndGet();
return new PasswordAuthentication("username", "password".toCharArray());
}
})
.build()) {

HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(new URI("http://" + httpTestServer.serverAuthority() + "/" + this.getClass().getSimpleName() + "/"))
Copy link
Contributor

Choose a reason for hiding this comment

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

To ensure the client will fire the request using the protocol version of our preference, could you also pass version to the request builder too, please?

.build();
throw assertThrows(IOException.class, () -> client.send(request, HttpResponse.BodyHandlers.discarding()));
} catch (Exception e) {
if (RETRY_LIMIT > 0){
assertEquals(RETRY_LIMIT, requestCount.get(),
"Expected number of request retries was " + RETRY_LIMIT + " but actual was "+requestCount);
}
else{
requestCount.decrementAndGet();
assertEquals(0, requestCount.get(),
"Expected number of request retries was 0 but actual was "+requestCount);
}
e.printStackTrace();
}
Comment on lines +101 to +113
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT, you should be using assertThrows as follows:

IOException exception = assertThrows(...);
assertEquals(exception.message(), "too many authentication attempts. Limit: " + RETRY_LIMIT);
assertEquals(requestCount.get(), RETRY_LIMIT > 0 ? RETRY_LIMIT : 1);

}
}
}