From f58416bb2a8437f0e50fc2f6ed2e2e2f41805076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Wed, 10 Jan 2024 22:16:06 +0100 Subject: [PATCH] [java] ensure the worker thread is stopped --- .../openqa/selenium/os/ExternalProcess.java | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/java/src/org/openqa/selenium/os/ExternalProcess.java b/java/src/org/openqa/selenium/os/ExternalProcess.java index 1ea97ee496d35..1766071d767a0 100644 --- a/java/src/org/openqa/selenium/os/ExternalProcess.java +++ b/java/src/org/openqa/selenium/os/ExternalProcess.java @@ -290,24 +290,42 @@ public void shutdown() { * @param timeout the duration for a process to terminate before destroying it forcibly. */ public void shutdown(Duration timeout) { - if (process.supportsNormalTermination()) { - process.destroy(); + try { + if (process.supportsNormalTermination()) { + process.destroy(); - try { - if (process.waitFor(timeout.toMillis(), MILLISECONDS)) { - worker.join(); - return; + try { + if (process.waitFor(timeout.toMillis(), MILLISECONDS)) { + // the outer finally block will take care of the worker + return; + } + } catch (InterruptedException ex) { + Thread.interrupted(); } + } + + process.destroyForcibly(); + try { + process.waitFor(timeout.toMillis(), MILLISECONDS); } catch (InterruptedException ex) { Thread.interrupted(); } - } - - process.destroyForcibly(); - try { - worker.join(); - } catch (InterruptedException ex) { - Thread.interrupted(); + } finally { + try { + // the worker might not stop even when process.destroyForcibly is called + worker.join(8000); + } catch (InterruptedException ex) { + Thread.interrupted(); + } finally { + // if already stopped interrupt is ignored, otherwise raises I/O exceptions in the worker + worker.interrupt(); + try { + // now we might be able to join + worker.join(2000); + } catch (InterruptedException ex) { + Thread.interrupted(); + } + } } } }