rcaller icon indicating copy to clipboard operation
rcaller copied to clipboard

Restore Thread interrupt flag after wait

Open fbastian opened this issue 8 years ago • 3 comments

rcaller code makes the Thread to wait/sleep at some points (which is fine). The problem is that, if the Thread was interrupted, these methods throw an InterruptedException, which is caught and rethrown by rcaller without restoring the interrupted thread flag.

Example in rcaller 2.8: https://github.com/jbytecode/rcaller/blob/c586cb7c74d2e570d6b3ef242c8f3c1561cac49d/RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java

The method runRCode uses the method Process.waitFor, which throws an InterruptedException if the Thread was interrupted, and reinit the interrupted flag of the Thread. A good practice is to catch the InterruptedException and to restore the interrupted flag. For instance:

try {
    //this Process object is local to this method. Do not use the public one.
    process = exec(RscriptExecutable + " " + rSourceFile.toString());
    startStreamConsumers(process);
    returnCode = process.waitFor();
} catch (Exception e) {
    if (e instanceof InterruptedException) {
        //restore the interrupt flag so that applications don't loose this information
        Thread.currentThread().interrupt();
    }
    throw new ExecutionException("Can not run " + RscriptExecutable + ". Reason: " + e.toString());
}

fbastian avatar Oct 14 '16 07:10 fbastian

And also it would be good to formally provide the cause of the exception, not only in the message:

throw new ExecutionException("Can not run " + RscriptExecutable + ". Reason: " + e.toString(), e)

fbastian avatar Oct 14 '16 07:10 fbastian

You could implement it and then make a pull request.

paulcurcean avatar Mar 24 '17 14:03 paulcurcean

@fbastian any help or improvements for this issue?

jbytecode avatar Sep 15 '20 19:09 jbytecode