JDK-8067796 : (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8,9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2014-12-17
  • Updated: 2020-05-25
  • Resolved: 2015-03-23
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 8 JDK 9 Other
8-poolResolved 9 b57Fixed openjdk8u262Fixed
Description
Spec of j.l.Process.waitFor(timeout, unit) says:
"Throws: NullPointerException - if unit is null".

However, j.l.Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero. Instead of this it returns false.

Minimized test:
---------------------------------------------------------------

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println(Runtime.getRuntime().exec("java").waitFor(0, null));
    System.out.println(Runtime.getRuntime().exec("java").waitFor(-50, null));
}
---------------------------------------------------------------

Output:
---------------------------------------------------------------

false
false
---------------------------------------------------------------

Tested with JDK9 b23.
Failed JCK test is under development.
Comments
Fix Request (8u) Backport to 8u requested because it is a part of 8u251-oracle. Patch doesn't apply cleanly - test is adjusted for 8u. The patch must be applied after JDK-8037866 Review thread: http://mail.openjdk.java.net/pipermail/jdk8u-dev/2020-April/011541.html
15-04-2020

A panel of experts have recommended that the implementation throw NPE be thrown in this case to match the spec.
19-12-2014

A second opinion about specification language would be useful. Generally, the @throws clause descriptions are not universal assertions; they are qualified by the description of the method.
18-12-2014

I think it's not obvious here. It looks like "returns immediately" means here just that current thread won't wait. This method doesn't throw NPE, also, if the process is already terminated. Of cause this logic is Ok. I just suggest to supplement throws clause like "Throws: NullPointerException - if process has not terminated, unit is null and timeout value is less than, or equal to, zero." to avoid misunderstandings.
18-12-2014

The javadoc for the method also says: " If the subprocess has already terminated then this method returns * immediately with the value {@code true}. If the process has not * terminated and the timeout value is less than, or equal to, zero, then * this method returns immediately with the value {@code false}. *" The description of the behavior especially with the clause "returns immediately" takes precedence over the more general description on @throws NPE
17-12-2014