Skip to content

Fix throws only ResourceAccessException on timeout #34721

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 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -96,12 +96,13 @@ public URI getURI() {
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
CompletableFuture<HttpResponse<InputStream>> responseFuture = null;
TimeoutHandler timeoutHandler = null;
try {
HttpRequest request = buildRequest(headers, body);
responseFuture = this.httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream());

if (this.timeout != null) {
TimeoutHandler timeoutHandler = new TimeoutHandler(responseFuture, this.timeout);
timeoutHandler = new TimeoutHandler(responseFuture, this.timeout);
HttpResponse<InputStream> response = responseFuture.get();
InputStream inputStream = timeoutHandler.wrapInputStream(response);
return new JdkClientHttpResponse(response, inputStream);
Expand Down Expand Up @@ -136,6 +137,9 @@ else if (cause instanceof IOException ioEx) {
throw (message == null ? new IOException(cause) : new IOException(message, cause));
}
}
catch (CancellationException ex) {
throw new HttpTimeoutException("Request timed out");
}
}

private HttpRequest buildRequest(HttpHeaders headers, @Nullable Body body) {
Expand Down Expand Up @@ -224,6 +228,7 @@ public ByteBuffer map(byte[] b, int off, int len) {
private static final class TimeoutHandler {

private final CompletableFuture<Void> timeoutFuture;
private boolean isTimeout=false;

private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Duration timeout) {

Expand All @@ -232,6 +237,7 @@ private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Dura

this.timeoutFuture.thenRun(() -> {
if (future.cancel(true) || future.isCompletedExceptionally() || !future.isDone()) {
this.isTimeout = true;
return;
}
try {
Expand Down