From 379545e7ddf021fac8e4baaf49f4e5603dbd9bda Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 30 Aug 2023 10:06:30 -0700 Subject: [PATCH] handle jcmd oddities on windows --- .../qa/die_with_dignity/DieWithDignityIT.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java b/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java index 912b95a396717..1476aee96b068 100644 --- a/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java +++ b/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.qa.die_with_dignity; +import org.apache.lucene.util.Constants; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.core.PathUtils; @@ -27,6 +28,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -76,11 +78,28 @@ public void testDieWithDignity() throws Exception { assertTrue(fatalErrorInThreadExiting); } - private void assertJvmArgs(long pid, Matcher matcher) throws IOException { + private Process startJcmd(long pid) throws IOException { final String jcmdPath = PathUtils.get(System.getProperty("tests.runtime.java"), "bin/jcmd").toString(); - final Process jcmdProcess = new ProcessBuilder().command(jcmdPath, Long.toString(pid), "VM.command_line") + return new ProcessBuilder().command(jcmdPath, Long.toString(pid), "VM.command_line") .redirectErrorStream(true) .start(); + } + + private void assertJvmArgs(long pid, Matcher matcher) throws IOException, InterruptedException { + Process jcmdProcess = startJcmd(pid); + + if (Constants.WINDOWS) { + // jcmd on windows appears to have a subtle bug where if the process being connected to + // dies while jcmd is running, it can hang indefinitely. Here we detect this case by + // waiting a fixed amount of time, and then killing/retrying the process + boolean exited = jcmdProcess.waitFor(10, TimeUnit.SECONDS); + if (exited == false) { + logger.warn("jcmd hung, killing process and retrying"); + jcmdProcess.destroyForcibly(); + jcmdProcess = startJcmd(pid); + } + } + List outputLines = readLines(jcmdProcess.getInputStream()); String jvmArgs = null;