3434import java .net .http .HttpTimeoutException ;
3535import java .nio .ByteBuffer ;
3636import java .time .Duration ;
37+ import java .util .ArrayList ;
3738import java .util .List ;
3839import java .util .Objects ;
3940import java .util .concurrent .CancellationException ;
@@ -66,7 +67,7 @@ public class JdkHttpClient implements HttpClient {
6667 public static final Logger LOG = Logger .getLogger (JdkHttpClient .class .getName ());
6768 private final JdkHttpMessages messages ;
6869 private java .net .http .HttpClient client ;
69- private WebSocket websocket ;
70+ private final List < WebSocket > websockets ;
7071 private final ExecutorService executorService ;
7172 private final Duration readTimeout ;
7273
@@ -75,6 +76,7 @@ public class JdkHttpClient implements HttpClient {
7576
7677 this .messages = new JdkHttpMessages (config );
7778 this .readTimeout = config .readTimeout ();
79+ this .websockets = new ArrayList <>();
7880
7981 executorService = Executors .newCachedThreadPool ();
8082
@@ -239,7 +241,7 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
239241 throw new TimeoutException (e );
240242 }
241243
242- this . websocket =
244+ WebSocket websocket =
243245 new WebSocket () {
244246 @ Override
245247 public WebSocket send (Message message ) {
@@ -305,14 +307,11 @@ public WebSocket send(Message message) {
305307 @ Override
306308 public void close () {
307309 LOG .fine ("Closing websocket" );
308- synchronized (underlyingSocket ) {
309- if (!underlyingSocket .isOutputClosed ()) {
310- underlyingSocket .sendClose (1000 , "WebDriver closing socket" );
311- }
312- }
310+ send (new CloseMessage (1000 , "WebDriver closing socket" ));
313311 }
314312 };
315- return this .websocket ;
313+ websockets .add (websocket );
314+ return websocket ;
316315 }
317316
318317 private URI getWebSocketUri (HttpRequest request ) {
@@ -443,11 +442,18 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
443442
444443 @ Override
445444 public void close () {
446- executorService .shutdownNow ();
447- if (this .websocket != null ) {
448- this .websocket .close ();
445+ if (this .client == null ) {
446+ return ;
449447 }
450448 this .client = null ;
449+ for (WebSocket websocket : websockets ) {
450+ try {
451+ websocket .close ();
452+ } catch (Exception e ) {
453+ LOG .log (Level .WARNING , "failed to close the websocket: " + websocket , e );
454+ }
455+ }
456+ executorService .shutdownNow ();
451457 }
452458
453459 @ AutoService (HttpClient .Factory .class )
0 commit comments