Skip to content

[grid] ensure all states of StartOrDie are handled #11297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/os/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.File;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class CommandLine {

Expand Down Expand Up @@ -100,6 +101,10 @@ public void execute() {
waitFor();
}

public boolean waitForProcessStarted(long duration, TimeUnit unit) {
return process.waitForProcessStarted(duration, unit);
}

public void waitFor() {
try {
process.waitFor();
Expand Down
34 changes: 22 additions & 12 deletions java/src/org/openqa/selenium/os/OsProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,32 @@ public void executeAsync() {
}
}

public boolean waitForProcessStarted(long duration, TimeUnit unit) {
return executeWatchdog.waitForProcessStarted(duration, unit);
}

private OutputStream getOutputStream() {
return drainTo == null ? inputOut
: new MultiOutputStream(inputOut, drainTo);
}

public int destroy() {
SeleniumWatchDog watchdog = executeWatchdog;
watchdog.waitForProcessStarted();

// I literally have no idea why we don't try and kill the process nicely on Windows. If you do,
// answers on the back of a postcard to SeleniumHQ, please.
if (!Platform.getCurrent().is(WINDOWS)) {
watchdog.destroyProcess();
watchdog.waitForTerminationAfterDestroy(2, SECONDS);
}
if (watchdog.waitForProcessStarted(2, TimeUnit.MINUTES)) {
// I literally have no idea why we don't try and kill the process nicely on Windows. If you do,
// answers on the back of a postcard to SeleniumHQ, please.
if (!Platform.getCurrent().is(WINDOWS)) {
watchdog.destroyProcess();
watchdog.waitForTerminationAfterDestroy(2, SECONDS);
}

if (isRunning()) {
watchdog.destroyHarder();
watchdog.waitForTerminationAfterDestroy(1, SECONDS);
if (isRunning()) {
watchdog.destroyHarder();
watchdog.waitForTerminationAfterDestroy(1, SECONDS);
}
} else {
log.warning("Tried to destory a process which never started.");
}

// Make a best effort to drain the streams.
Expand Down Expand Up @@ -236,15 +243,18 @@ public void reset() {
starting = true;
}

private void waitForProcessStarted() {
while (starting) {
private boolean waitForProcessStarted(long duration, TimeUnit unit) {
long end = System.currentTimeMillis() + unit.toMillis(duration);
while (starting && System.currentTimeMillis() < end) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}

return !starting;
}

private void waitForTerminationAfterDestroy(int duration, TimeUnit unit) {
Expand Down
15 changes: 10 additions & 5 deletions java/src/org/openqa/selenium/remote/service/DriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ public void start() throws IOException {
process.setEnvironmentVariables(environment);
process.copyOutputTo(getOutputStream());
process.executeAsync();
if (!process.waitForProcessStarted(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
throw new WebDriverException("Timed out waiting for driver process to start.");
}

CompletableFuture<StartOrDie> serverStarted = CompletableFuture.supplyAsync(() -> {
waitUntilAvailable();
Expand All @@ -231,13 +234,15 @@ public void start() throws IOException {
try {
StartOrDie status = (StartOrDie) CompletableFuture.anyOf(serverStarted, processFinished)
.get(getTimeout().toMillis() * 2, TimeUnit.MILLISECONDS);
if (status == StartOrDie.SERVER_STARTED) {
processFinished.cancel(true);
} else {
if (status == StartOrDie.PROCESS_DIED) {
switch (status) {
case SERVER_STARTED:
processFinished.cancel(true);
break;
case PROCESS_DIED:
process = null;
throw new WebDriverException("Driver server process died prematurely.");
}
case PROCESS_IS_ACTIVE:
throw new WebDriverException("Timed out waiting for driver server to bind the port.");
}
} catch (ExecutionException | TimeoutException e) {
throw new WebDriverException("Timed out waiting for driver server to start.", e);
Expand Down