Skip to content

Commit d8f0d20

Browse files
committed
Read process output on separate thread
If the process's output stream isn't read from, the buffer will get too full and the process will block. Thus our process.waitFor call will never finish. We need to instead read the process output in parallel as we wait for the process to finish.
1 parent 6fe428c commit d8f0d20

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/main/java/net/imagej/ui/swing/updater/LauncherMigrator.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,25 @@ private static void extractAndSetProperty(
533533
private static List<String> collectProcessOutput(Process p)
534534
throws IOException, InterruptedException
535535
{
536+
List<String> outputLines = new ArrayList<>();
537+
Thread outputReader = new Thread(() -> {
538+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
539+
String line;
540+
while ((line = reader.readLine()) != null) {
541+
outputLines.add(line);
542+
}
543+
} catch (IOException e) {
544+
e.printStackTrace();
545+
}
546+
});
547+
548+
outputReader.start();
536549
boolean completed = p.waitFor(5, TimeUnit.SECONDS);
537550
if (!completed) {
538551
p.destroyForcibly();
539552
throw new IOException("Process took too long to complete.");
540553
}
541-
p.exitValue();
542-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
543-
return reader.lines().collect(Collectors.toList());
544-
}
554+
return outputLines;
545555
}
546556

547557
/** Annoying code to discern the AWT/Swing main application frame, if any. */

0 commit comments

Comments
 (0)