Skip to content

Commit 7acc040

Browse files
authored
[java] Ensure calling close() and quit() don't cause BiDi websocket errors (#13333)
1 parent c75b7f0 commit 7acc040

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

java/src/org/openqa/selenium/bidi/BiDi.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ public class BiDi implements Closeable {
2929

3030
private final Duration timeout = Duration.ofSeconds(30);
3131
private final Connection connection;
32-
private final BiDiSessionStatus status;
3332

3433
public BiDi(Connection connection) {
3534
this.connection = Require.nonNull("WebSocket connection", connection);
36-
this.status =
37-
send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
3835
}
3936

4037
@Override
@@ -119,6 +116,6 @@ public void clearListeners() {
119116
}
120117

121118
public BiDiSessionStatus getBidiSessionStatus() {
122-
return status;
119+
return send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
123120
}
124121
}

java/src/org/openqa/selenium/bidi/Connection.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,28 @@ public class Connection implements Closeable {
6666
return thread;
6767
});
6868
private static final AtomicLong NEXT_ID = new AtomicLong(1L);
69-
private final WebSocket socket;
69+
private WebSocket socket;
7070
private final Map<Long, Consumer<Either<Throwable, JsonInput>>> methodCallbacks =
7171
new ConcurrentHashMap<>();
7272
private final ReadWriteLock callbacksLock = new ReentrantReadWriteLock(true);
7373
private final Map<Event<?>, List<Consumer<?>>> eventCallbacks = new HashMap<>();
7474
private final HttpClient client;
75-
private final AtomicBoolean underlyingSocketClosed;
75+
private final AtomicBoolean underlyingSocketClosed = new AtomicBoolean();
7676

7777
public Connection(HttpClient client, String url) {
7878
Require.nonNull("HTTP client", client);
7979
Require.nonNull("URL to connect to", url);
8080

8181
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+
}
8491
}
8592

8693
private static class NamedConsumer<X> implements Consumer<X> {
@@ -230,7 +237,10 @@ public void clearListeners() {
230237

231238
@Override
232239
public void close() {
233-
socket.close();
240+
if (!underlyingSocketClosed.get()) {
241+
underlyingSocketClosed.set(true);
242+
socket.close();
243+
}
234244
client.close();
235245
}
236246

0 commit comments

Comments
 (0)