Skip to content

Commit 6307f5d

Browse files
committed
Clean up immediately if starting a driver or session errors in RemoteWebDriver.
Fixes SeleniumHQ#6811: Grid nodes create more browsers than limit in configuration.
1 parent c1b034f commit 6307f5d

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,22 @@ public RemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilitie
109109
if (executor instanceof NeedsLocalLogs) {
110110
((NeedsLocalLogs)executor).setLocalLogs(localLogs);
111111
}
112-
startClient();
113-
startSession(desiredCapabilities, requiredCapabilities);
112+
113+
try {
114+
startClient();
115+
} catch (Exception e) {
116+
stopClient();
117+
118+
throw new WebDriverException("Failed to start client.", e);
119+
}
120+
121+
try {
122+
startSession(desiredCapabilities, requiredCapabilities);
123+
} catch (Exception e) {
124+
quit();
125+
126+
throw new WebDriverException("Failed to start session.", e);
127+
}
114128
}
115129

116130
public RemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities) {

java/server/test/org/openqa/selenium/remote/server/RemoteWebDriverTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.json.JSONException;
2525
import org.json.JSONObject;
2626
import org.junit.Test;
27+
import org.openqa.selenium.Capabilities;
28+
import org.openqa.selenium.WebDriverException;
2729
import org.openqa.selenium.remote.CapabilityType;
2830
import org.openqa.selenium.remote.CommandExecutor;
2931
import org.openqa.selenium.remote.ErrorCodes;
@@ -34,8 +36,11 @@
3436
import java.io.IOException;
3537
import java.net.HttpURLConnection;
3638
import java.net.URL;
39+
import java.util.concurrent.atomic.AtomicBoolean;
3740

3841
public class RemoteWebDriverTest extends JUnit4TestBase {
42+
private boolean stopClientCalled = false;
43+
private boolean quitCalled = false;
3944

4045
@Test
4146
public void testCanCheckServerStatusIndependentlyOfSessions() throws IOException, JSONException {
@@ -78,10 +83,95 @@ public void testCanCheckServerStatusIndependentlyOfSessions() throws IOException
7883
}
7984
}
8085

86+
@Test
87+
public void testStopsClientIfStartClientFails() {
88+
if (!(driver instanceof RemoteWebDriver)) {
89+
System.out.println("Skipping test: driver is not a remote webdriver");
90+
return;
91+
}
92+
93+
RemoteWebDriver remote = (RemoteWebDriver) driver;
94+
boolean exceptionThrown = false;
95+
AtomicBoolean stopCalled = new AtomicBoolean(false);
96+
97+
try {
98+
new BadStartClientRemoteWebDriver(remote.getCommandExecutor(),
99+
remote.getCapabilities(),
100+
remote.getCapabilities(),
101+
stopCalled);
102+
} catch (WebDriverException e) {
103+
assertTrue(e.getMessage().contains("Failed to start client."));
104+
105+
exceptionThrown = true;
106+
}
107+
108+
assertTrue(exceptionThrown);
109+
assertTrue(stopClientCalled);
110+
}
111+
112+
@Test
113+
public void testQuitsIfStartSessionFails() {
114+
if (!(driver instanceof RemoteWebDriver)) {
115+
System.out.println("Skipping test: driver is not a remote webdriver");
116+
return;
117+
}
118+
119+
RemoteWebDriver remote = (RemoteWebDriver) driver;
120+
boolean exceptionThrown = false;
121+
122+
try {
123+
new BadStartSessionRemoteWebDriver(remote.getCommandExecutor(),
124+
remote.getCapabilities(),
125+
remote.getCapabilities());
126+
} catch (WebDriverException e) {
127+
assertTrue(e.getMessage().contains("Failed to start session."));
128+
129+
exceptionThrown = true;
130+
}
131+
132+
assertTrue(exceptionThrown);
133+
assertTrue(quitCalled);
134+
}
135+
81136
private static void assertHasKeys(JSONObject object, String... keys) {
82137
for (String key : keys) {
83138
assertTrue("Object does not contain expected key: " + key + " (" + object + ")",
84139
object.has(key));
85140
}
86141
}
142+
143+
private class BadStartClientRemoteWebDriver extends RemoteWebDriver {
144+
public BadStartClientRemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities,
145+
Capabilities requiredCapabilities, AtomicBoolean stopCalled) {
146+
super(executor, desiredCapabilities, requiredCapabilities);
147+
}
148+
149+
@Override
150+
protected void startClient() {
151+
throw new RuntimeException("Stub client that should fail");
152+
}
153+
154+
@Override
155+
protected void stopClient() {
156+
stopClientCalled = true;
157+
}
158+
}
159+
160+
private class BadStartSessionRemoteWebDriver extends RemoteWebDriver {
161+
public BadStartSessionRemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities,
162+
Capabilities requiredCapabilities) {
163+
super(executor, desiredCapabilities, requiredCapabilities);
164+
}
165+
166+
@Override
167+
protected void startSession(Capabilities desiredCapabilities,
168+
Capabilities requiredCapabilities) {
169+
throw new RuntimeException("Stub session that should fail");
170+
}
171+
172+
@Override
173+
public void quit() {
174+
quitCalled = true;
175+
}
176+
}
87177
}

0 commit comments

Comments
 (0)