Skip to content

Commit

Permalink
fix #90
Browse files Browse the repository at this point in the history
  • Loading branch information
pron committed Sep 16, 2015
1 parent f2caec4 commit 1affcb6
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1743,8 +1743,12 @@ private void receiveLoop() {
while (receive())
;
} catch (IOException e) {
if (isChildAlive())
printError(LOG_QUIET, e);
try {
final Process p = child;
if (p != null && !waitFor(p, 100))
printError(LOG_QUIET, e);
} catch (InterruptedException ex) {
}
}
}

Expand Down Expand Up @@ -4827,6 +4831,19 @@ private static boolean isAlive(Process p) {
}
}

private static boolean waitFor(Process p, long millis) throws InterruptedException {
// return p.waitFor(millis, TimeUnit.MILLISECONDS); // JDK 8
final long deadline = System.nanoTime() + millis * 1_000_000;
for (;;) {
if (!isAlive(p))
return true;
long sleep = Math.min((deadline - System.nanoTime()) / 1_000_000, 20);
if (sleep <= 0)
return false;
Thread.sleep(sleep);
}
}

/**
* Executes a command and returns its output as a list of lines.
* The method will wait for the child process to terminate, and throw an exception if the command returns an exit value {@code != 0}.
Expand Down Expand Up @@ -4876,7 +4893,7 @@ protected static Iterable<String> exec(ProcessBuilder pb) throws IOException {
protected static Iterable<String> exec(int numLines, ProcessBuilder pb) throws IOException {
return exec(numLines, false, pb);
}

private static Iterable<String> exec(int numLines, boolean error, ProcessBuilder pb) throws IOException {
final List<String> lines = new ArrayList<>();
final Process p = pb.start();
Expand Down

0 comments on commit 1affcb6

Please sign in to comment.