From 5c891ce374dd126b361c23864cee33bf3695053a Mon Sep 17 00:00:00 2001 From: joerg1985 <16140691+joerg1985@users.noreply.github.com> Date: Mon, 15 May 2023 19:46:43 +0200 Subject: [PATCH] [java] JdkHttpClient - Close all websockets before shutting down the executor (#12035) Close all websockets before shutting down the executor Co-authored-by: Diego Molina --- .../remote/http/jdk/JdkHttpClient.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java index e55fcc67fcd48..dd34e531832e3 100644 --- a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java +++ b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java @@ -50,6 +50,7 @@ import java.net.http.HttpTimeoutException; import java.nio.ByteBuffer; import java.time.Duration; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.concurrent.CancellationException; @@ -67,7 +68,7 @@ public class JdkHttpClient implements HttpClient { public static final Logger LOG = Logger.getLogger(JdkHttpClient.class.getName()); private final JdkHttpMessages messages; private java.net.http.HttpClient client; - private WebSocket websocket; + private final List websockets; private final ExecutorService executorService; private final Duration readTimeout; @@ -76,7 +77,7 @@ public class JdkHttpClient implements HttpClient { this.messages = new JdkHttpMessages(config); this.readTimeout = config.readTimeout(); - + websockets = new ArrayList<>(); executorService = Executors.newCachedThreadPool(); java.net.http.HttpClient.Builder builder = java.net.http.HttpClient.newBuilder() @@ -202,7 +203,7 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) { java.net.http.WebSocket underlyingSocket = webSocketCompletableFuture.join(); - this.websocket = new WebSocket() { + WebSocket websocket = new WebSocket() { @Override public WebSocket send(Message message) { Supplier> makeCall; @@ -259,14 +260,11 @@ public WebSocket send(Message message) { @Override public void close() { LOG.fine("Closing websocket"); - synchronized (underlyingSocket) { - if (!underlyingSocket.isOutputClosed()) { - underlyingSocket.sendClose(1000, "WebDriver closing socket"); - } - } + send(new CloseMessage(1000, "WebDriver closing socket")); } }; - return this.websocket; + websockets.add(websocket); + return websocket; } private URI getWebSocketUri(HttpRequest request) { @@ -347,11 +345,18 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException { @Override public void close() { - executorService.shutdownNow(); - if (this.websocket != null) { - this.websocket.close(); + if (this.client == null) { + return ; } this.client = null; + for (WebSocket websocket : websockets) { + try { + websocket.close(); + } catch (Exception e) { + LOG.log(Level.WARNING, "failed to close the websocket: " + websocket, e); + } + } + executorService.shutdownNow(); } @AutoService(HttpClient.Factory.class)