@@ -196,7 +196,8 @@ public ExternalProcess start() throws UncheckedIOException {
196
196
try {
197
197
CircularOutputStream circular = new CircularOutputStream (bufferSize );
198
198
199
- new Thread (
199
+ Thread worker =
200
+ new Thread (
200
201
() -> {
201
202
// copyOutputTo might be system.out or system.err, do not to close
202
203
OutputStream output = new MultiOutputStream (circular , copyOutputTo );
@@ -212,10 +213,11 @@ public ExternalProcess start() throws UncheckedIOException {
212
213
Level .WARNING , "failed to copy the output of process " + process .pid (), ex );
213
214
}
214
215
LOG .log (Level .FINE , "completed to copy the output of process " + process .pid ());
215
- })
216
- .start ();
216
+ });
217
217
218
- return new ExternalProcess (process , circular );
218
+ worker .start ();
219
+
220
+ return new ExternalProcess (process , circular , worker );
219
221
} catch (Throwable t ) {
220
222
// ensure we do not leak a process in case of failures
221
223
try {
@@ -234,10 +236,12 @@ public static Builder builder() {
234
236
235
237
private final Process process ;
236
238
private final CircularOutputStream outputStream ;
239
+ private final Thread worker ;
237
240
238
- public ExternalProcess (Process process , CircularOutputStream outputStream ) {
241
+ public ExternalProcess (Process process , CircularOutputStream outputStream , Thread worker ) {
239
242
this .process = process ;
240
243
this .outputStream = outputStream ;
244
+ this .worker = worker ;
241
245
}
242
246
243
247
/**
@@ -255,7 +259,13 @@ public boolean isAlive() {
255
259
}
256
260
257
261
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 ;
259
269
}
260
270
261
271
public int exitValue () {
@@ -282,6 +292,7 @@ public void shutdown(Duration timeout) {
282
292
283
293
try {
284
294
if (process .waitFor (timeout .toMillis (), MILLISECONDS )) {
295
+ worker .join ();
285
296
return ;
286
297
}
287
298
} catch (InterruptedException ex ) {
@@ -290,5 +301,10 @@ public void shutdown(Duration timeout) {
290
301
}
291
302
292
303
process .destroyForcibly ();
304
+ try {
305
+ worker .join ();
306
+ } catch (InterruptedException ex ) {
307
+ Thread .interrupted ();
308
+ }
293
309
}
294
310
}
0 commit comments