Skip to content

Commit e4d31f2

Browse files
committed
[java] close the streams after I/O failure #13096
1 parent afa349a commit e4d31f2

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,26 @@ public ExternalProcess start() throws UncheckedIOException {
193193
throw new UncheckedIOException(ex);
194194
}
195195

196-
CircularOutputStream circular;
197196
try {
198-
circular = new CircularOutputStream(bufferSize);
197+
CircularOutputStream circular = new CircularOutputStream(bufferSize);
199198

200199
new Thread(
201200
() -> {
202-
InputStream input = process.getInputStream();
203201
// use the CircularOutputStream as mandatory, we know it will never raise a
204202
// IOException
205-
OutputStream output = new MultiOutputStream(circular, copyOutputTo);
206-
while (process.isAlive()) {
207-
try {
208-
// we must read the output to ensure the process will not lock up
209-
input.transferTo(output);
210-
} catch (IOException ex) {
211-
LOG.log(
212-
Level.WARNING,
213-
"failed to copy the output of process " + process.pid(),
214-
ex);
215-
}
203+
try (InputStream input = process.getInputStream();
204+
OutputStream output = new MultiOutputStream(circular, copyOutputTo)) {
205+
// we must read the output to ensure the process will not lock up
206+
input.transferTo(output);
207+
} catch (IOException ex) {
208+
LOG.log(
209+
Level.WARNING, "failed to copy the output of process " + process.pid(), ex);
216210
}
211+
LOG.log(Level.FINE, "completed to copy the output of process " + process.pid());
217212
})
218213
.start();
214+
215+
return new ExternalProcess(process, circular);
219216
} catch (Throwable t) {
220217
// ensure we do not leak a process in case of failures
221218
try {
@@ -225,8 +222,6 @@ public ExternalProcess start() throws UncheckedIOException {
225222
}
226223
throw t;
227224
}
228-
229-
return new ExternalProcess(process, circular);
230225
}
231226
}
232227

0 commit comments

Comments
 (0)