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); 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 * 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 * Returns the stdout result
* *
@ -67,6 +73,13 @@ public interface OutputBuffer {
* @return stderr result * @return stderr result
*/ */
public String getStderr(); public String getStderr();
/**
* Returns the exit value
*
* @return exit value
*/
public int getExitValue(); public int getExitValue();
/** /**
@ -135,6 +148,31 @@ public interface OutputBuffer {
errTask = new StreamTask(p.getErrorStream(), cs); 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 @Override
public String getStdout() { public String getStdout() {
return outTask.get(); return outTask.get();
@ -147,27 +185,9 @@ public interface OutputBuffer {
@Override @Override
public int getExitValue() { public int getExitValue() {
if (exitValue != null) { waitFor();
return exitValue; 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);
}
}
@Override @Override
public long pid() { public long pid() {
@ -186,6 +206,11 @@ public interface OutputBuffer {
this.exitValue = exitValue; this.exitValue = exitValue;
} }
@Override
public void waitFor() {
// Nothing to do since this buffer is not associated with a Process.
}
@Override @Override
public String getStdout() { public String getStdout() {
return stdout; return stdout;

View File

@ -697,7 +697,10 @@ public final class ProcessTools {
} }
output = new OutputAnalyzer(p, cs); 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 { // Dumping the process output to a separate file
var fileName = String.format("pid-%d-output.log", p.pid()); var fileName = String.format("pid-%d-output.log", p.pid());