50
50
import java .net .http .HttpTimeoutException ;
51
51
import java .nio .ByteBuffer ;
52
52
import java .time .Duration ;
53
+ import java .util .ArrayList ;
53
54
import java .util .List ;
54
55
import java .util .Objects ;
55
56
import java .util .concurrent .CancellationException ;
@@ -67,7 +68,7 @@ public class JdkHttpClient implements HttpClient {
67
68
public static final Logger LOG = Logger .getLogger (JdkHttpClient .class .getName ());
68
69
private final JdkHttpMessages messages ;
69
70
private java .net .http .HttpClient client ;
70
- private WebSocket websocket ;
71
+ private final List < WebSocket > websockets ;
71
72
private final ExecutorService executorService ;
72
73
private final Duration readTimeout ;
73
74
@@ -76,7 +77,7 @@ public class JdkHttpClient implements HttpClient {
76
77
77
78
this .messages = new JdkHttpMessages (config );
78
79
this .readTimeout = config .readTimeout ();
79
-
80
+ websockets = new ArrayList <>();
80
81
executorService = Executors .newCachedThreadPool ();
81
82
82
83
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) {
202
203
203
204
java .net .http .WebSocket underlyingSocket = webSocketCompletableFuture .join ();
204
205
205
- this . websocket = new WebSocket () {
206
+ WebSocket websocket = new WebSocket () {
206
207
@ Override
207
208
public WebSocket send (Message message ) {
208
209
Supplier <CompletableFuture <java .net .http .WebSocket >> makeCall ;
@@ -259,14 +260,11 @@ public WebSocket send(Message message) {
259
260
@ Override
260
261
public void close () {
261
262
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" ));
267
264
}
268
265
};
269
- return this .websocket ;
266
+ websockets .add (websocket );
267
+ return websocket ;
270
268
}
271
269
272
270
private URI getWebSocketUri (HttpRequest request ) {
@@ -347,11 +345,18 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
347
345
348
346
@ Override
349
347
public void close () {
350
- executorService .shutdownNow ();
351
- if (this .websocket != null ) {
352
- this .websocket .close ();
348
+ if (this .client == null ) {
349
+ return ;
353
350
}
354
351
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 ();
355
360
}
356
361
357
362
@ AutoService (HttpClient .Factory .class )
0 commit comments