Skip to content

Commit

Permalink
bugfix: 升级JDK17导致worker无法强杀进程 #11320
Browse files Browse the repository at this point in the history
  • Loading branch information
tangruotian committed Dec 17, 2024
1 parent d392f91 commit 0a6ca50
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,19 @@ public synchronized EnvVars getEnvironmentVariables() {
}

private static final class UnixReflection {
private static final Field PID_FIELD;
private static final Method DESTROY_PROCESS;
private static Field PID_FIELD;
private static Method DESTROY_PROCESS;

private UnixReflection() {
}

// TODO: 升级到JDK17后,这里可以使用 java.lang.Process 重构
public static void destroy(int pid, boolean forceFlag) throws IllegalAccessException, InvocationTargetException {
if (isJava17()) {
BkProcessTree.log("Killing by jdk17");
destroyProcessJava17(pid, forceFlag);
} else {
BkProcessTree.log("Killing by jdk8");
DESTROY_PROCESS.invoke((Object) null, pid, forceFlag);
}
}
Expand All @@ -656,9 +658,13 @@ private static boolean isJava17() {
DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE, Boolean.TYPE);
DESTROY_PROCESS.setAccessible(true);
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) {
x = new LinkageError();
x.initCause(e);
throw x;
if (isJava17()) {
BkProcessTree.log("java17 ignore java8 class error");
} else {
x = new LinkageError();
x.initCause(e);
throw x;
}
}
}

Expand Down Expand Up @@ -689,6 +695,8 @@ private static void destroyProcessJava17(int pid, boolean forceFlag) {
destroyMethod = processHandleClass.getMethod("destroy");
}
destroyMethod.invoke(processHandle);
} else {
BkProcessTree.log("Failed to terminate pid=" + pid + "no present");
}
} catch (Exception e) {
BkProcessTree.log("Failed to terminate pid=" + pid, e);
Expand Down

0 comments on commit 0a6ca50

Please sign in to comment.