@@ -89,8 +89,8 @@ public class Service implements AutoCloseable {
8989 private Thread stderrThread ;
9090 private Thread monitorThread ;
9191
92- private Consumer <String > debugListener ;
93- private String initScript ;
92+ private @ Nullable Consumer <String > debugListener ;
93+ private @ Nullable String initScript ;
9494 private ScriptSyntax syntax ;
9595
9696
@@ -514,7 +514,7 @@ private void stdoutLoop() {
514514 line = stdout .readLine ();
515515 }
516516 catch (IOException exc ) {
517- // Something went wrong reading the line. Panic!
517+ // Something went wrong reading the stdout line. Panic!
518518 debugService (Messages .stackTrace (exc ));
519519 break ;
520520 }
@@ -556,7 +556,7 @@ private void stderrLoop() {
556556 line = stderr .readLine ();
557557 }
558558 catch (IOException exc ) {
559- // Something went wrong reading the line. Panic!
559+ // Something went wrong reading the stderr line. Panic!
560560 debugService (Messages .stackTrace (exc ));
561561 break ;
562562 }
@@ -569,12 +569,11 @@ private void stderrLoop() {
569569 }
570570 }
571571
572- @ SuppressWarnings ("BusyWait" )
573572 private void monitorLoop () {
574573 // Wait until the worker process terminates.
575574 while (process .isAlive () || stdoutThread .isAlive () || stderrThread .isAlive ()) {
576575 try {
577- Thread . sleep ( 10 );
576+ process . waitFor ( );
578577 }
579578 catch (InterruptedException exc ) {
580579 // Treat interruption as a request to shut down.
@@ -588,24 +587,21 @@ private void monitorLoop() {
588587 int exitCode = process .exitValue ();
589588 if (exitCode != 0 ) debugService ("<worker process terminated with exit code " + exitCode + ">" );
590589 int taskCount = tasks .size ();
591- if (taskCount > 0 ) {
592- debugService ("<worker process terminated with " +
593- taskCount + " pending task" + (taskCount == 1 ? "" : "s" ) + ">" );
594- }
595-
596- Collection <Task > remainingTasks = tasks .values ();
597- if (!remainingTasks .isEmpty ()) {
598- // Notify any remaining tasks about the process crash.
599- StringBuilder sb = new StringBuilder ();
600- String nl = System .lineSeparator ();
601- sb .append ("Worker crashed with exit code " ).append (exitCode ).append ("." ).append (nl );
602- String stdout = invalidLines .isEmpty () ? "<none>" : String .join (nl , invalidLines );
603- String stderr = errorLines .isEmpty () ? "<none>" : String .join (nl , errorLines );
604- sb .append (nl ).append ("[stdout]" ).append (nl ).append (stdout ).append (nl );
605- sb .append (nl ).append ("[stderr]" ).append (nl ).append (stderr ).append (nl );
606- String error = sb .toString ();
607- remainingTasks .forEach (task -> task .crash (error ));
608- }
590+ if (taskCount == 0 ) return ; // No hanging tasks to clean up.
591+
592+ debugService ("<worker process terminated with " +
593+ taskCount + " pending task" + (taskCount == 1 ? "" : "s" ) + ">" );
594+
595+ // Notify remaining tasks about the process crash.
596+ StringBuilder sb = new StringBuilder ();
597+ String nl = System .lineSeparator ();
598+ sb .append ("Worker crashed with exit code " ).append (exitCode ).append ("." ).append (nl );
599+ String stdout = invalidLines .isEmpty () ? "<none>" : String .join (nl , invalidLines );
600+ String stderr = errorLines .isEmpty () ? "<none>" : String .join (nl , errorLines );
601+ sb .append (nl ).append ("[stdout]" ).append (nl ).append (stdout ).append (nl );
602+ sb .append (nl ).append ("[stderr]" ).append (nl ).append (stderr ).append (nl );
603+ String error = sb .toString ();
604+ tasks .values ().forEach (task -> task .crash (error ));
609605 tasks .clear ();
610606 }
611607
0 commit comments