JDK-8208715 : Conversion of milliseconds to nanoseconds in UNIXProcess contains bug.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8,11
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86_64
  • Submitted: 2018-08-01
  • Updated: 2020-02-21
  • Resolved: 2018-08-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 12 JDK 8 Other
11.0.7-oracleFixed 12 b08Fixed 8u251Fixed openjdk8u242Fixed
Description
A DESCRIPTION OF THE PROBLEM :
Please consider this code snippet:

```
public class Temp {
    public static void main(String[] args) throws IOException, InterruptedException {
        java.lang.Process unixProcess = new ProcessBuilder("ls").directory(new File(".")).start();
        unixProcess.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }
}
```

it should work, but it dies with "IllegalArgumentException: timeout value is negative" exception.
Looking at `java.lang.UNIXProcess#waitFor(long timeout, TimeUnit unit)` method:

long remainingNanos = unit.toNanos(timeout);  //reporter: this returns Long.MAX_VALUE
long deadline = System.nanoTime() + remainingNanos;

```
...
do {
    // Round up to next millisecond
    wait(TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L)); //reporter: remainingNanos + 999_999L evaluates to negative value, because remainingNanos == Long.MAX_VALUE
...
```


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
public class Temp {
    public static void main(String[] args) throws IOException, InterruptedException {
        java.lang.Process unixProcess = new ProcessBuilder("ls").directory(new File(".")).start();
        unixProcess.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }
}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Successful execution
ACTUAL -
Exception in thread "main" java.lang.IllegalArgumentException: timeout value is negative
	at java.lang.Object.wait(Native Method)
	at java.lang.UNIXProcess.waitFor(UNIXProcess.java:412)

---------- BEGIN SOURCE ----------
public class Temp {
    public static void main(String[] args) throws IOException, InterruptedException {
        java.lang.Process unixProcess = new ProcessBuilder("ls").directory(new File(".")).start();
        unixProcess.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Specify smaller timeout


Comments
Fix Request (11u) Low risk. Patch applies cleanly and updated test (Basic.java) passes.
13-12-2019

Andrew Hughes commented: ----- Am I right in thinking that the additional test changes are from JDK-8029629? https://hg.openjdk.java.net/jdk/jdk/rev/8e5afc67dca87179 If so, that bug should be backported first. ----- I've tagged JDK-8029629 with jdk8u-fix-request. Once that patch is applied, the patch for this backport applies cleanly net of code reorganization. The test (Basic.java) passes.
13-12-2019

8u review approval: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-October/010483.html
04-12-2019

Fix Request (8u) on behalf of junguoj@amazon.com This backport fixes the milliseconds to nanoseconds conversion bug in UNIXProcess and passes attached test. RFR: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-August/010179.html
30-08-2019

The deadline in ProcessImpl should saturate at Long.MAX_VALUE.
09-08-2018

Reproduced the issue on ubuntu 14.0.4 with : JDK 8u172 - Fail JDK 8u181 - Fail JDK 11-ea+18 - Fail Output: Exception in thread "main" java.lang.IllegalArgumentException: timeout value is negative at java.base/java.lang.Object.wait(Native Method) at java.base/java.lang.ProcessImpl.waitFor(ProcessImpl.java:510) at JI9056393.main(JI9056393.java:9)
03-08-2018