Skip to content

Commit 301a4ca

Browse files
committed
Using more native way to kill the whole process tree on Windows. Stop plugin-container crashes
1 parent 7830ff1 commit 301a4ca

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

java/client/src/org/openqa/selenium/os/Kernel32.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
3939
boolean AssignProcessToJobObject(HANDLE hJob, HANDLE hProcess);
4040
boolean TerminateJobObject(HANDLE hJob, long uExitCode);
4141
int ResumeThread(HANDLE hThread);
42+
int GetProcessId(HANDLE Process);
4243

4344
// 0x00000800
4445
int JOB_OBJECT_LIMIT_BREAKAWAY_OK = 2048;

java/client/src/org/openqa/selenium/os/ProcessUtils.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
import java.util.logging.Logger;
2828

2929
import static org.openqa.selenium.Platform.WINDOWS;
30+
import static org.openqa.selenium.os.WindowsUtils.killPID;
31+
import static org.openqa.selenium.os.WindowsUtils.thisIsWindows;
32+
33+
import com.sun.jna.Pointer;
34+
import com.sun.jna.platform.win32.WinNT;
3035

3136
public class ProcessUtils {
3237
static Logger log = Logger.getLogger(ProcessUtils.class.getName());
@@ -70,6 +75,14 @@ private static int waitForProcessDeath(Process p, long timeout) {
7075
* @return The exit value of the process.
7176
*/
7277
public static int killProcess(Process process) {
78+
if (thisIsWindows()) {
79+
return killWinProcess(process);
80+
} else {
81+
return killUnixProcess(process);
82+
}
83+
}
84+
85+
private static int killUnixProcess(Process process) {
7386
int exitValue;
7487

7588
// first, wait a second to see if the process will die on it's own (we will likely have asked
@@ -108,6 +121,31 @@ public static int killProcess(Process process) {
108121
return exitValue;
109122
}
110123

124+
private static int killWinProcess(Process process) {
125+
int exitValue;
126+
127+
try {
128+
Field f = process.getClass().getDeclaredField("handle");
129+
f.setAccessible(true);
130+
long hndl = f.getLong(process);
131+
132+
Kernel32 kernel = Kernel32.INSTANCE;
133+
WinNT.HANDLE handle = new WinNT.HANDLE();
134+
handle.setPointer(Pointer.createConstant(hndl));
135+
int pid = kernel.GetProcessId(handle);
136+
137+
killPID("" + pid);
138+
exitValue = waitForProcessDeath(process, 10000);
139+
} catch (Throwable ex) {
140+
log.warning("Process refused to die after 10 seconds, and couldn't killall it");
141+
ex.printStackTrace();
142+
throw new RuntimeException(
143+
"Process refused to die after 10 seconds, and couldn't killall it: " + ex.getMessage(),
144+
ex);
145+
}
146+
return exitValue;
147+
}
148+
111149
private static class ProcessWaiter implements Runnable {
112150

113151
private volatile InterruptedException t;

java/client/src/org/openqa/selenium/os/UnixProcess.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.collect.ImmutableMap.copyOf;
2222
import static java.util.concurrent.TimeUnit.SECONDS;
23+
import static org.openqa.selenium.os.WindowsUtils.thisIsWindows;
2324

2425
import com.google.common.annotations.VisibleForTesting;
2526
import com.google.common.collect.Maps;
@@ -111,10 +112,13 @@ private OutputStream getOutputStream() {
111112
public int destroy() {
112113
SeleniumWatchDog watchdog = executeWatchdog;
113114
watchdog.waitForProcessStarted();
114-
watchdog.destroyProcess();
115-
watchdog.waitForTerminationAfterDestroy(2, SECONDS);
116-
if (!isRunning()) {
117-
return getExitCode();
115+
116+
if (!thisIsWindows()) {
117+
watchdog.destroyProcess();
118+
watchdog.waitForTerminationAfterDestroy(2, SECONDS);
119+
if (!isRunning()) {
120+
return getExitCode();
121+
}
118122
}
119123

120124
watchdog.destroyHarder();
@@ -124,7 +128,7 @@ public int destroy() {
124128
}
125129

126130
log.severe(String.format("Unable to kill process with PID %s",
127-
watchdog.getPID()));
131+
watchdog.getPID()));
128132
int exitCode = -1;
129133
executor.setExitValue(exitCode);
130134
return exitCode;

java/client/src/org/openqa/selenium/os/WindowsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static void kill(String[] cmdarray) throws Exception {
168168
/**
169169
* Kills the specified process ID
170170
*/
171-
private static void killPID(String processID) {
171+
public static void killPID(String processID) {
172172
executeCommand("taskkill", "/f", "/t", "/pid", processID);
173173
}
174174

0 commit comments

Comments
 (0)