8321718: ProcessTools.executeProcess calls waitFor before logging

Reviewed-by: dholmes, jpai
This commit is contained in:
Stefan Karlsson 2024-01-03 07:48:57 +00:00
parent ba426d6887
commit 9ab29f8dcd
3 changed files with 57 additions and 21 deletions

View File

@ -108,6 +108,14 @@ public final class OutputAnalyzer {
buffer = OutputBuffer.of(stdout, stderr, exitValue);
}
/**
* Delegate waitFor to the OutputBuffer. This ensures that
* the progress and timestamps are logged correctly.
*/
public void waitFor() {
buffer.waitFor();
}
/**
* Verify that the stdout contents of output buffer is empty
*

View File

@ -44,6 +44,12 @@ public interface OutputBuffer {
}
}
/**
* Waits for a process to finish, if there is one assocated with
* this OutputBuffer.
*/
public void waitFor();
/**
* Returns the stdout result
*
@ -67,6 +73,13 @@ public interface OutputBuffer {
* @return stderr result
*/
public String getStderr();
/**
* Returns the exit value
*
* @return exit value
*/
public int getExitValue();
/**
@ -135,6 +148,31 @@ public interface OutputBuffer {
errTask = new StreamTask(p.getErrorStream(), cs);
}
@Override
public void waitFor() {
if (exitValue != null) {
// Already waited for this process
return;
}
try {
logProgress("Waiting for completion");
boolean aborted = true;
try {
exitValue = p.waitFor();
logProgress("Waiting for completion finished");
aborted = false;
} finally {
if (aborted) {
logProgress("Waiting for completion FAILED");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OutputBufferException(e);
}
}
@Override
public String getStdout() {
return outTask.get();
@ -147,26 +185,8 @@ public interface OutputBuffer {
@Override
public int getExitValue() {
if (exitValue != null) {
return exitValue;
}
try {
logProgress("Waiting for completion");
boolean aborted = true;
try {
exitValue = p.waitFor();
logProgress("Waiting for completion finished");
aborted = false;
return exitValue;
} finally {
if (aborted) {
logProgress("Waiting for completion FAILED");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OutputBufferException(e);
}
waitFor();
return exitValue;
}
@Override
@ -186,6 +206,11 @@ public interface OutputBuffer {
this.exitValue = exitValue;
}
@Override
public void waitFor() {
// Nothing to do since this buffer is not associated with a Process.
}
@Override
public String getStdout() {
return stdout;

View File

@ -697,7 +697,10 @@ public final class ProcessTools {
}
output = new OutputAnalyzer(p, cs);
p.waitFor();
// Wait for the process to finish. Call through the output
// analyzer to get correct logging and timestamps.
output.waitFor();
{ // Dumping the process output to a separate file
var fileName = String.format("pid-%d-output.log", p.pid());