@@ -66,21 +66,28 @@ public class Connection implements Closeable {
66
66
return thread ;
67
67
});
68
68
private static final AtomicLong NEXT_ID = new AtomicLong (1L );
69
- private final WebSocket socket ;
69
+ private WebSocket socket ;
70
70
private final Map <Long , Consumer <Either <Throwable , JsonInput >>> methodCallbacks =
71
71
new ConcurrentHashMap <>();
72
72
private final ReadWriteLock callbacksLock = new ReentrantReadWriteLock (true );
73
73
private final Map <Event <?>, List <Consumer <?>>> eventCallbacks = new HashMap <>();
74
74
private final HttpClient client ;
75
- private final AtomicBoolean underlyingSocketClosed ;
75
+ private final AtomicBoolean underlyingSocketClosed = new AtomicBoolean () ;
76
76
77
77
public Connection (HttpClient client , String url ) {
78
78
Require .nonNull ("HTTP client" , client );
79
79
Require .nonNull ("URL to connect to" , url );
80
80
81
81
this .client = client ;
82
- socket = this .client .openSocket (new HttpRequest (GET , url ), new Listener ());
83
- underlyingSocketClosed = new AtomicBoolean ();
82
+ // If WebDriver close() is called, it closes the session if it is the last browsing context.
83
+ // It also closes the WebSocket from the remote end.
84
+ // If WebDriver quit() is called, it also tries to close an already closed websocket and that
85
+ // causes errors.
86
+ // Ideally, such errors should not prevent freeing up resources.
87
+ // This measure is needed until "session.end" from BiDi is implemented by the browsers.
88
+ if (!underlyingSocketClosed .get ()) {
89
+ socket = this .client .openSocket (new HttpRequest (GET , url ), new Listener ());
90
+ }
84
91
}
85
92
86
93
private static class NamedConsumer <X > implements Consumer <X > {
@@ -230,7 +237,10 @@ public void clearListeners() {
230
237
231
238
@ Override
232
239
public void close () {
233
- socket .close ();
240
+ if (!underlyingSocketClosed .get ()) {
241
+ underlyingSocketClosed .set (true );
242
+ socket .close ();
243
+ }
234
244
client .close ();
235
245
}
236
246
0 commit comments