Skip to content

Commit d1787a9

Browse files
committed
[java] ensure the complete output is read #13091
1 parent 1bccc05 commit d1787a9

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

java/src/org/openqa/selenium/os/ExternalProcess.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public ExternalProcess start() throws UncheckedIOException {
196196
try {
197197
CircularOutputStream circular = new CircularOutputStream(bufferSize);
198198

199-
new Thread(
199+
Thread worker =
200+
new Thread(
200201
() -> {
201202
// copyOutputTo might be system.out or system.err, do not to close
202203
OutputStream output = new MultiOutputStream(circular, copyOutputTo);
@@ -212,10 +213,11 @@ public ExternalProcess start() throws UncheckedIOException {
212213
Level.WARNING, "failed to copy the output of process " + process.pid(), ex);
213214
}
214215
LOG.log(Level.FINE, "completed to copy the output of process " + process.pid());
215-
})
216-
.start();
216+
});
217217

218-
return new ExternalProcess(process, circular);
218+
worker.start();
219+
220+
return new ExternalProcess(process, circular, worker);
219221
} catch (Throwable t) {
220222
// ensure we do not leak a process in case of failures
221223
try {
@@ -234,10 +236,12 @@ public static Builder builder() {
234236

235237
private final Process process;
236238
private final CircularOutputStream outputStream;
239+
private final Thread worker;
237240

238-
public ExternalProcess(Process process, CircularOutputStream outputStream) {
241+
public ExternalProcess(Process process, CircularOutputStream outputStream, Thread worker) {
239242
this.process = process;
240243
this.outputStream = outputStream;
244+
this.worker = worker;
241245
}
242246

243247
/**
@@ -255,7 +259,13 @@ public boolean isAlive() {
255259
}
256260

257261
public boolean waitFor(Duration duration) throws InterruptedException {
258-
return process.waitFor(duration.toMillis(), TimeUnit.MILLISECONDS);
262+
boolean exited = process.waitFor(duration.toMillis(), TimeUnit.MILLISECONDS);
263+
264+
if (exited) {
265+
worker.join();
266+
}
267+
268+
return exited;
259269
}
260270

261271
public int exitValue() {
@@ -282,6 +292,7 @@ public void shutdown(Duration timeout) {
282292

283293
try {
284294
if (process.waitFor(timeout.toMillis(), MILLISECONDS)) {
295+
worker.join();
285296
return;
286297
}
287298
} catch (InterruptedException ex) {
@@ -290,5 +301,10 @@ public void shutdown(Duration timeout) {
290301
}
291302

292303
process.destroyForcibly();
304+
try {
305+
worker.join();
306+
} catch (InterruptedException ex) {
307+
Thread.interrupted();
308+
}
293309
}
294310
}

0 commit comments

Comments
 (0)