eclipse-vertx / eclipse-vertx/vert.x

Request Timeout Issue with HttpClient

Open
#5,231 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
14.7k
Forks
2.1k
Avg merge
2d 7h
Merged PRs (30d)
28

Description

### Version
4.5.4

### Context
It seems like there is some race condition in some cases when setting a timeout on http request, consider the following example:
```
HttpClient client = vertx.createHttpClient(
new HttpClientOptions(options).setProtocolVersion(HttpVersion.HTTP_2));
RequestOptions options = new RequestOptions().setTimeout(10);
return client
.request(options)
.onComplete(...);
```

There are cases in which onCompletion handler will never be called.

When timeout is set, there is a background thread that checks whether the timeout has exceeded and if it does it sets reset variable to timeOutException:
```
public synchronized HttpClientRequest setTimeout(long timeoutMs) {
cancelTimeout();
currentTimeoutMs = timeoutMs;
currentTimeoutTimerId = context.setTimer(timeoutMs, id -> handleTimeout(timeoutMs));
return this;
}

boolean reset(Throwable cause) {
synchronized (this) {
if (reset != null) {
return false;
}
reset = cause;
}
stream.reset(cause);
return true;
}
```
If the timeout occurs after this code:
```
try {
createStream(request, headers);
} catch (Http2Exception ex) {
if (handler != null) {
handler.handle(context.failedFuture(ex));
}
handleException(ex);
return;
}

```

reset variable will be not null and the future will never complete:
```
void handleResponse(HttpClientResponse resp) {
if (reset == null) {
handleResponse(responsePromise, resp, cancelTimeout());
}
}
```

I saw that there are some fixes for it for vert.x 5 release, is it possible to have a fix for 4.5.x as well?

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.