Skip to content

Commit c254aff

Browse files
committed
8321718: ProcessTools.executeProcess calls waitFor before logging
Backport-of: 9ab29f8dcd1c0092e4251f996bd53c704e87a74a
1 parent 38a2173 commit c254aff

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

test/lib/jdk/test/lib/process/OutputAnalyzer.java

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public OutputAnalyzer(String stdout, String stderr, int exitValue)
106106
buffer = OutputBuffer.of(stdout, stderr, exitValue);
107107
}
108108

109+
/**
110+
* Delegate waitFor to the OutputBuffer. This ensures that
111+
* the progress and timestamps are logged correctly.
112+
*/
113+
public void waitFor() {
114+
buffer.waitFor();
115+
}
116+
109117
/**
110118
* Verify that the stdout contents of output buffer is empty
111119
*

test/lib/jdk/test/lib/process/OutputBuffer.java

+38-13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public OutputBufferException(Throwable cause) {
4444
}
4545
}
4646

47+
/**
48+
* Waits for a process to finish, if there is one assocated with
49+
* this OutputBuffer.
50+
*/
51+
public void waitFor();
52+
4753
/**
4854
* Returns the stdout result
4955
*
@@ -67,6 +73,13 @@ default public List<String> getStdoutAsList() {
6773
* @return stderr result
6874
*/
6975
public String getStderr();
76+
77+
78+
/**
79+
* Returns the exit value
80+
*
81+
* @return exit value
82+
*/
7083
public int getExitValue();
7184

7285
/**
@@ -136,28 +149,19 @@ private LazyOutputBuffer(Process p, Charset cs) {
136149
}
137150

138151
@Override
139-
public String getStdout() {
140-
return outTask.get();
141-
}
142-
143-
@Override
144-
public String getStderr() {
145-
return errTask.get();
146-
}
147-
148-
@Override
149-
public int getExitValue() {
152+
public void waitFor() {
150153
if (exitValue != null) {
151-
return exitValue;
154+
// Already waited for this process
155+
return;
152156
}
157+
153158
try {
154159
logProgress("Waiting for completion");
155160
boolean aborted = true;
156161
try {
157162
exitValue = p.waitFor();
158163
logProgress("Waiting for completion finished");
159164
aborted = false;
160-
return exitValue;
161165
} finally {
162166
if (aborted) {
163167
logProgress("Waiting for completion FAILED");
@@ -169,6 +173,22 @@ public int getExitValue() {
169173
}
170174
}
171175

176+
@Override
177+
public String getStdout() {
178+
return outTask.get();
179+
}
180+
181+
@Override
182+
public String getStderr() {
183+
return errTask.get();
184+
}
185+
186+
@Override
187+
public int getExitValue() {
188+
waitFor();
189+
return exitValue;
190+
}
191+
172192
@Override
173193
public long pid() {
174194
return p.pid();
@@ -186,6 +206,11 @@ private EagerOutputBuffer(String stdout, String stderr, int exitValue) {
186206
this.exitValue = exitValue;
187207
}
188208

209+
@Override
210+
public void waitFor() {
211+
// Nothing to do since this buffer is not associated with a Process.
212+
}
213+
189214
@Override
190215
public String getStdout() {
191216
return stdout;

test/lib/jdk/test/lib/process/ProcessTools.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,10 @@ public static OutputAnalyzer executeProcess(ProcessBuilder pb, String input,
697697
}
698698

699699
output = new OutputAnalyzer(p, cs);
700-
p.waitFor();
700+
701+
// Wait for the process to finish. Call through the output
702+
// analyzer to get correct logging and timestamps.
703+
output.waitFor();
701704

702705
{ // Dumping the process output to a separate file
703706
var fileName = String.format("pid-%d-output.log", p.pid());

0 commit comments

Comments
 (0)