Skip to content

Commit

Permalink
[java] Applying again changes from 5c891ce
Browse files Browse the repository at this point in the history
Credits to @joerg1985

Fixes #11826
  • Loading branch information
diemol committed Jun 16, 2023
1 parent c3b226c commit eb23213
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 Down Expand Up @@ -66,7 +67,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 @@ -75,6 +76,7 @@ public class JdkHttpClient implements HttpClient {

this.messages = new JdkHttpMessages(config);
this.readTimeout = config.readTimeout();
this.websockets = new ArrayList<>();

executorService = Executors.newCachedThreadPool();

Expand Down Expand Up @@ -239,7 +241,7 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
throw new TimeoutException(e);
}

this.websocket =
WebSocket websocket =
new WebSocket() {
@Override
public WebSocket send(Message message) {
Expand Down Expand Up @@ -305,14 +307,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 @@ -443,11 +442,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 eb23213

Please sign in to comment.