Skip to content

Commit

Permalink
[java] JdkHttpClient - Close all websockets before shutting down the …
Browse files Browse the repository at this point in the history
…executor (#12035)

Close all websockets before shutting down the executor

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
joerg1985 and diemol committed May 15, 2023
1 parent a2235cd commit 5c891ce
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Expand Up @@ -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;
Expand All @@ -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<WebSocket> websockets;
private final ExecutorService executorService;
private final Duration readTimeout;

Expand All @@ -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()
Expand Down Expand Up @@ -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<CompletableFuture<java.net.http.WebSocket>> makeCall;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5c891ce

Please sign in to comment.