@@ -234,14 +234,18 @@ public void start() {
234234 .redirectOutput (outFile )
235235 .redirectError (errFile )
236236 .start ();
237- p .waitFor (5 , TimeUnit .MINUTES );
238- if (!ignorePullError && p .exitValue () != 0 ) {
239- dumpServerLogs (outPath , errPath );
240- throw new IllegalStateException (
241- String .format (
242- Locale .US ,
243- "Non-zero status while attempting to pull docker image '%s'" ,
244- dockerImage ));
237+ try {
238+ p .waitFor (5 , TimeUnit .MINUTES );
239+ if (!ignorePullError && p .exitValue () != 0 ) {
240+ dumpServerLogs (outPath , errPath );
241+ throw new IllegalStateException (
242+ String .format (
243+ Locale .US ,
244+ "Non-zero status while attempting to pull docker image '%s'" ,
245+ dockerImage ));
246+ }
247+ } finally {
248+ p .destroyForcibly ();
245249 }
246250 } catch (InterruptedException | IllegalThreadStateException e ) {
247251 dumpServerLogs (outPath , errPath );
@@ -279,12 +283,16 @@ public void start() {
279283 .start ();
280284 LOGGER .info (command .toString ());
281285 try {
282- if (!process .waitFor (10 , TimeUnit .SECONDS )) {
283- throw new IllegalStateException ("docker run timed out" );
284- }
285- if (process .exitValue () != 0 ) {
286- dumpServerLogs (outPath , errPath );
287- throw new IllegalStateException ("docker run failed with exit code " + process .exitValue ());
286+ try {
287+ if (!process .waitFor (10 , TimeUnit .SECONDS )) {
288+ throw new IllegalStateException ("docker run timed out" );
289+ }
290+ if (process .exitValue () != 0 ) {
291+ dumpServerLogs (outPath , errPath );
292+ throw new IllegalStateException ("docker run failed with exit code " + process .exitValue ());
293+ }
294+ } finally {
295+ process .destroyForcibly ();
288296 }
289297
290298 if (baseUri == null ) {
@@ -350,10 +358,18 @@ public void stop() {
350358 LOGGER .warn ("Stopping container: {}" , containerName );
351359 ImmutableList <String > killCommand = ImmutableList .of ("docker" , "kill" , containerName );
352360 LOGGER .warn (killCommand .toString ());
353- Process shutdownProcess = new ProcessBuilder (killCommand ).start ();
354- shutdownProcess .waitFor (5 , TimeUnit .SECONDS );
355- int shutdownProcessExitValue = shutdownProcess .exitValue ();
356- LOGGER .warn ("Container exit value = {}" , shutdownProcessExitValue );
361+ ProcessBuilder pb = new ProcessBuilder (killCommand );
362+ File nullFile = new File (System .getProperty ("os.name" ).startsWith ("Windows" ) ? "NUL" : "/dev/null" );
363+ pb .redirectOutput (ProcessBuilder .Redirect .to (nullFile ));
364+ pb .redirectError (ProcessBuilder .Redirect .to (nullFile ));
365+ Process shutdownProcess = pb .start ();
366+ try {
367+ shutdownProcess .waitFor (5 , TimeUnit .SECONDS );
368+ int shutdownProcessExitValue = shutdownProcess .exitValue ();
369+ LOGGER .warn ("Container exit value = {}" , shutdownProcessExitValue );
370+ } finally {
371+ shutdownProcess .destroyForcibly ();
372+ }
357373
358374 try {
359375 Files .delete (errPath );
@@ -368,24 +384,31 @@ public void stop() {
368384 }
369385
370386 private int getDockerPort (String containerName , int containerPort ) throws IOException , InterruptedException {
371- Process p = new ProcessBuilder ("docker" , "port" , containerName , String .valueOf (containerPort )).start ();
372- if (!p .waitFor (5 , TimeUnit .SECONDS )) {
373- throw new IllegalStateException ("docker port timed out" );
374- }
375- if (p .exitValue () != 0 ) {
376- throw new IllegalStateException ("docker port failed" );
377- }
378- try (BufferedReader reader = new BufferedReader (new InputStreamReader (p .getInputStream (), StandardCharsets .UTF_8 ))) {
379- String line = reader .readLine ();
380- if (line == null ) {
381- throw new IllegalStateException ("No port mapping found" );
387+ ProcessBuilder pb = new ProcessBuilder ("docker" , "port" , containerName , String .valueOf (containerPort ));
388+ File nullFile = new File (System .getProperty ("os.name" ).startsWith ("Windows" ) ? "NUL" : "/dev/null" );
389+ pb .redirectError (ProcessBuilder .Redirect .to (nullFile ));
390+ Process p = pb .start ();
391+ try {
392+ if (!p .waitFor (5 , TimeUnit .SECONDS )) {
393+ throw new IllegalStateException ("docker port timed out" );
382394 }
383- // Line format is like "0.0.0.0:49153" or "[::]:49153"
384- int colonIndex = line .lastIndexOf (':' );
385- if (colonIndex == -1 ) {
386- throw new IllegalStateException ("Invalid port mapping: " + line );
395+ if (p .exitValue () != 0 ) {
396+ throw new IllegalStateException ("docker port failed" );
387397 }
388- return Integer .parseInt (line .substring (colonIndex + 1 ));
398+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (p .getInputStream (), StandardCharsets .UTF_8 ))) {
399+ String line = reader .readLine ();
400+ if (line == null ) {
401+ throw new IllegalStateException ("No port mapping found" );
402+ }
403+ // Line format is like "0.0.0.0:49153" or "[::]:49153"
404+ int colonIndex = line .lastIndexOf (':' );
405+ if (colonIndex == -1 ) {
406+ throw new IllegalStateException ("Invalid port mapping: " + line );
407+ }
408+ return Integer .parseInt (line .substring (colonIndex + 1 ));
409+ }
410+ } finally {
411+ p .destroyForcibly ();
389412 }
390413 }
391414
0 commit comments