JDK-6427854 : (se) NullPointerException in Selector.open()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-05-19
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
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 7
7 b08Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
bug is OS independent

A DESCRIPTION OF THE PROBLEM :
sun.nio.ch.Util contains code which is not thread safe and can throw a NullPointerException:

    private static String bugLevel = null;

    static boolean atBugLevel(String bl) {		// package-private
        if (bugLevel == null) {
            if (!sun.misc.VM.isBooted())
                return false;
            java.security.PrivilegedAction pa =
                new GetPropertyAction("sun.nio.ch.bugLevel");
// the next line can reset bugLevel to null
            bugLevel = (String)AccessController.doPrivileged(pa);
            if (bugLevel == null)
                bugLevel = "";
        }
        return (bugLevel != null) && bugLevel.equals(bl);
    }

Suppose that two threads enter the "if (buglevel == null)" body at the same time. The first one runs until the return line and gets scheduled out right after the (buglevel != null) check. The second one then runs until right after the doPrivileged() call, sets bugLevel to null and gets scheduled out. The first one continues and hits a NullPointerException while calling bugLevel.equals() with bugLevel being null.

The chances to hit this are low, but I did see it in a test run (see stacktrace below).


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NullPointerException
   at sun.nio.ch.Util.atBugLevel(Util.java:290)
   at sun.nio.ch.SelectorImpl.<init>(SelectorImpl.java:40)
   at sun.nio.ch.WindowsSelectorImpl.<init>(WindowsSelectorImpl.java:104)
   at sun.nio.ch.WindowsSelectorProvider.openSelector(WindowsSelectorProvider.java:26)
   at java.nio.channels.Selector.open(Selector.java:209)


REPRODUCIBILITY :
This bug can be reproduced rarely.

Comments
EVALUATION Yes, there is a timing issue. We expect the issue is rare and will fix early in dolphin.
12-06-2006