34
34
import java .net .http .HttpTimeoutException ;
35
35
import java .nio .ByteBuffer ;
36
36
import java .time .Duration ;
37
+ import java .util .ArrayList ;
37
38
import java .util .List ;
38
39
import java .util .Objects ;
39
40
import java .util .concurrent .CancellationException ;
@@ -66,7 +67,7 @@ public class JdkHttpClient implements HttpClient {
66
67
public static final Logger LOG = Logger .getLogger (JdkHttpClient .class .getName ());
67
68
private final JdkHttpMessages messages ;
68
69
private java .net .http .HttpClient client ;
69
- private WebSocket websocket ;
70
+ private final List < WebSocket > websockets ;
70
71
private final ExecutorService executorService ;
71
72
private final Duration readTimeout ;
72
73
@@ -75,6 +76,7 @@ public class JdkHttpClient implements HttpClient {
75
76
76
77
this .messages = new JdkHttpMessages (config );
77
78
this .readTimeout = config .readTimeout ();
79
+ this .websockets = new ArrayList <>();
78
80
79
81
executorService = Executors .newCachedThreadPool ();
80
82
@@ -239,7 +241,7 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
239
241
throw new TimeoutException (e );
240
242
}
241
243
242
- this . websocket =
244
+ WebSocket websocket =
243
245
new WebSocket () {
244
246
@ Override
245
247
public WebSocket send (Message message ) {
@@ -305,14 +307,11 @@ public WebSocket send(Message message) {
305
307
@ Override
306
308
public void close () {
307
309
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" ));
313
311
}
314
312
};
315
- return this .websocket ;
313
+ websockets .add (websocket );
314
+ return websocket ;
316
315
}
317
316
318
317
private URI getWebSocketUri (HttpRequest request ) {
@@ -443,11 +442,18 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
443
442
444
443
@ Override
445
444
public void close () {
446
- executorService .shutdownNow ();
447
- if (this .websocket != null ) {
448
- this .websocket .close ();
445
+ if (this .client == null ) {
446
+ return ;
449
447
}
450
448
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 ();
451
457
}
452
458
453
459
@ AutoService (HttpClient .Factory .class )
0 commit comments