Skip to content

Commit 76701ad

Browse files
joerg1985diemol
andauthored
[java] JdkHttpClient - Handle a possible freeze when an incomplete http response is received (#12055)
Handle a possible freeze when an incomplete http response is received Co-authored-by: Diego Molina <[email protected]>
1 parent dd435a1 commit 76701ad

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,31 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
211211
}
212212
});
213213

214-
java.net.http.WebSocket underlyingSocket = webSocketCompletableFuture.join();
214+
java.net.http.WebSocket underlyingSocket;
215+
216+
try {
217+
underlyingSocket = webSocketCompletableFuture.get(readTimeout.toMillis(), TimeUnit.MILLISECONDS);
218+
} catch (CancellationException e) {
219+
throw new WebDriverException(e.getMessage(), e);
220+
} catch (ExecutionException e) {
221+
Throwable cause = e.getCause();
222+
223+
if (cause instanceof HttpTimeoutException) {
224+
throw new TimeoutException(cause);
225+
} else if (cause instanceof IOException) {
226+
throw new UncheckedIOException((IOException) cause);
227+
} else if (cause instanceof RuntimeException) {
228+
throw (RuntimeException) cause;
229+
}
230+
231+
throw new WebDriverException((cause != null) ? cause : e);
232+
} catch (InterruptedException e) {
233+
Thread.currentThread().interrupt();
234+
throw new RuntimeException(e);
235+
} catch (java.util.concurrent.TimeoutException e) {
236+
webSocketCompletableFuture.cancel(true);
237+
throw new TimeoutException(e);
238+
}
215239

216240
this.websocket =
217241
new WebSocket() {
@@ -340,7 +364,31 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
340364
// - not run into https://bugs.openjdk.org/browse/JDK-8304701
341365
for (int i = 0; i < 100; i++) {
342366
java.net.http.HttpRequest request = messages.createRequest(req, rawUri);
343-
java.net.http.HttpResponse<byte[]> response = client.send(request, byteHandler);
367+
java.net.http.HttpResponse<byte[]> response;
368+
369+
// use sendAsync to not run into https://bugs.openjdk.org/browse/JDK-8258397
370+
CompletableFuture<java.net.http.HttpResponse<byte[]>> future = client.sendAsync(request, byteHandler);
371+
372+
try {
373+
response = future.get(readTimeout.toMillis(), TimeUnit.MILLISECONDS);
374+
} catch (CancellationException e) {
375+
throw new WebDriverException(e.getMessage(), e);
376+
} catch (ExecutionException e) {
377+
Throwable cause = e.getCause();
378+
379+
if (cause instanceof HttpTimeoutException) {
380+
throw new TimeoutException(cause);
381+
} else if (cause instanceof IOException) {
382+
throw (IOException) cause;
383+
} else if (cause instanceof RuntimeException) {
384+
throw (RuntimeException) cause;
385+
}
386+
387+
throw new WebDriverException((cause != null) ? cause : e);
388+
} catch (java.util.concurrent.TimeoutException e) {
389+
future.cancel(true);
390+
throw new TimeoutException(e);
391+
}
344392

345393
switch (response.statusCode()) {
346394
case 301:
@@ -376,8 +424,6 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
376424
}
377425

378426
throw new ProtocolException("Too many redirects: 101");
379-
} catch (HttpTimeoutException e) {
380-
throw new TimeoutException(e);
381427
} catch (IOException e) {
382428
throw new UncheckedIOException(e);
383429
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)