Skip to content

Commit b0464e1

Browse files
committed
[grid] stop a stale session more graceful
1 parent fc03c5e commit b0464e1

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
2828
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;
2929
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;
30+
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
3031
import static org.openqa.selenium.remote.tracing.AttributeKey.SESSION_URI;
3132
import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;
3233

@@ -79,6 +80,7 @@
7980
import org.openqa.selenium.grid.data.NodeStatus;
8081
import org.openqa.selenium.grid.data.NodeStatusEvent;
8182
import org.openqa.selenium.grid.data.RequestId;
83+
import org.openqa.selenium.grid.data.Session;
8284
import org.openqa.selenium.grid.data.SessionRequest;
8385
import org.openqa.selenium.grid.data.SessionRequestCapability;
8486
import org.openqa.selenium.grid.data.Slot;
@@ -109,6 +111,7 @@
109111
import org.openqa.selenium.internal.Require;
110112
import org.openqa.selenium.remote.SessionId;
111113
import org.openqa.selenium.remote.http.HttpClient;
114+
import org.openqa.selenium.remote.http.HttpRequest;
112115
import org.openqa.selenium.remote.tracing.AttributeKey;
113116
import org.openqa.selenium.remote.tracing.AttributeMap;
114117
import org.openqa.selenium.remote.tracing.Span;
@@ -858,13 +861,29 @@ private void handleNewSessionRequest(SessionRequest sessionRequest) {
858861
if (!isSessionValid && response.isRight()) {
859862
LOG.log(
860863
Level.INFO,
861-
"Session for request {0} has been created but it has timed out, stopping it to avoid"
862-
+ " stalled browser",
864+
"Session for request {0} has been created but it has timed out or the connection"
865+
+ " dropped, stopping it to avoid stalled browser",
863866
reqId.toString());
864-
URI nodeURI = response.right().getSession().getUri();
865-
Node node = getNodeFromURI(nodeURI);
867+
Session session = response.right().getSession();
868+
Node node = getNodeFromURI(session.getUri());
866869
if (node != null) {
867-
node.stop(response.right().getSession().getId());
870+
boolean deleted;
871+
try {
872+
// Attempt to stop the session
873+
deleted =
874+
node.execute(new HttpRequest(DELETE, "/session/" + session.getId())).getStatus()
875+
== 200;
876+
} catch (Exception e) {
877+
LOG.log(
878+
Level.WARNING,
879+
String.format("Exception while trying to delete session %s", session.getId()),
880+
e);
881+
deleted = false;
882+
}
883+
if (!deleted) {
884+
// Kill the session
885+
node.stop(session.getId());
886+
}
868887
}
869888
}
870889
}

java/test/org/openqa/selenium/grid/router/DistributedTest.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,8 @@ void clientTimeoutDoesNotLeakARunningBrowser() throws Exception {
138138

139139
assertThat(nce.getMessage()).contains("TimeoutException");
140140

141-
Thread.sleep(
142-
// ensure the grid has some time to start the browser
143-
Duration.ofNanos((end - start) * 2)
144-
// and shutdown the browser
145-
.plusSeconds(20)
146-
.toMillis());
141+
// ensure the grid has some time to start the browser and shutdown the browser
142+
Thread.sleep(Duration.ofNanos((end - start) * 3).toMillis());
147143

148144
HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl());
149145
try {
@@ -193,7 +189,7 @@ void clientTimeoutDoesNotLeakARunningBrowser() throws Exception {
193189
Safely.safelyCall(client::close);
194190
}
195191
} finally {
196-
Safely.safelyCall(healthy::close);
192+
Safely.safelyCall(healthy::quit);
197193
}
198194
}
199195
}

0 commit comments

Comments
 (0)