From 63a191cb118afc09e206e609dabe45b1fcd3462b Mon Sep 17 00:00:00 2001 From: fanyong920 <1023079644@qq.com> Date: Fri, 17 Jan 2025 11:06:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:closeBrowser=E6=96=B9=E6=B3=95=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20process.waitFor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvppeteer/cdp/core/BrowserRunner.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserRunner.java b/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserRunner.java index a7f74e5..6a29a1a 100644 --- a/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserRunner.java +++ b/src/main/java/com/ruiyun/jvppeteer/cdp/core/BrowserRunner.java @@ -110,25 +110,23 @@ public void destroy() { this.destroyProcess(this.process); return; } - Process exec = null; - String command = ""; - this.destroyProcess(this.process); + Process exec; + String command; if (Helper.isLinux() || Helper.isMac()) { command = "kill -9 " + pid; exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command}); } else { - if (this.process.isAlive()) { - command = "cmd.exe /c taskkill /PID " + pid + " /F /T "; + command = "cmd.exe /c taskkill /pid " + pid + " /F /T "; exec = Runtime.getRuntime().exec(command); - } } try { - if (exec != null) { + if (Objects.nonNull(exec)) { if(LOGGER.isDebugEnabled()){ LOGGER.debug("kill chrome process by pid,command: {}", command); } exec.waitFor(Constant.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); } + this.destroyProcess(this.process); } finally { this.destroyProcess(exec); } @@ -149,8 +147,11 @@ public void destroyProcess(Process process) { return; } process.destroy(); - if (process.isAlive()) { - process.destroyForcibly(); + try { + process.waitFor(30L, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOGGER.error("operation interrupt", e); } }