Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8208715

Conversion of milliseconds to nanoseconds in UNIXProcess contains bug.

XMLWordPrintable

    • b08
    • x86_64
    • linux

        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

              rriggs Roger Riggs
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Created:
                Updated:
                Resolved: