5050import java .net .http .HttpTimeoutException ;
5151import java .nio .ByteBuffer ;
5252import java .time .Duration ;
53+ import java .util .ArrayList ;
5354import java .util .List ;
5455import java .util .Objects ;
5556import java .util .concurrent .CancellationException ;
@@ -67,7 +68,7 @@ public class JdkHttpClient implements HttpClient {
6768 public static final Logger LOG = Logger .getLogger (JdkHttpClient .class .getName ());
6869 private final JdkHttpMessages messages ;
6970 private java .net .http .HttpClient client ;
70- private WebSocket websocket ;
71+ private final List < WebSocket > websockets ;
7172 private final ExecutorService executorService ;
7273 private final Duration readTimeout ;
7374
@@ -76,7 +77,7 @@ public class JdkHttpClient implements HttpClient {
7677
7778 this .messages = new JdkHttpMessages (config );
7879 this .readTimeout = config .readTimeout ();
79-
80+ websockets = new ArrayList <>();
8081 executorService = Executors .newCachedThreadPool ();
8182
8283 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) {
202203
203204 java .net .http .WebSocket underlyingSocket = webSocketCompletableFuture .join ();
204205
205- this . websocket = new WebSocket () {
206+ WebSocket websocket = new WebSocket () {
206207 @ Override
207208 public WebSocket send (Message message ) {
208209 Supplier <CompletableFuture <java .net .http .WebSocket >> makeCall ;
@@ -259,14 +260,11 @@ public WebSocket send(Message message) {
259260 @ Override
260261 public void close () {
261262 LOG .fine ("Closing websocket" );
262- synchronized (underlyingSocket ) {
263- if (!underlyingSocket .isOutputClosed ()) {
264- underlyingSocket .sendClose (1000 , "WebDriver closing socket" );
265- }
266- }
263+ send (new CloseMessage (1000 , "WebDriver closing socket" ));
267264 }
268265 };
269- return this .websocket ;
266+ websockets .add (websocket );
267+ return websocket ;
270268 }
271269
272270 private URI getWebSocketUri (HttpRequest request ) {
@@ -347,11 +345,18 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
347345
348346 @ Override
349347 public void close () {
350- executorService .shutdownNow ();
351- if (this .websocket != null ) {
352- this .websocket .close ();
348+ if (this .client == null ) {
349+ return ;
353350 }
354351 this .client = null ;
352+ for (WebSocket websocket : websockets ) {
353+ try {
354+ websocket .close ();
355+ } catch (Exception e ) {
356+ LOG .log (Level .WARNING , "failed to close the websocket: " + websocket , e );
357+ }
358+ }
359+ executorService .shutdownNow ();
355360 }
356361
357362 @ AutoService (HttpClient .Factory .class )
0 commit comments