You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Edward Costello <ed...@orionhealth.com> on 2008/02/12 23:44:32 UTC

Null Pointer In java.lang.Package.isSealed because of Thread Saftey Issue with Webapp Classloader

Morning,

With tomcat 5.5.20 I've been getting NullPointer Exceptions out of
java.lang.Package.isSealed when multiple threads hit a web-app as it's
starting up. Basically the stack trace looks like the below. I believe
that what's triggering the error in this case is an RMI thread loading
the class at the same time as the webapp context listener is still
initializing.


java.lang.NullPointerException
        at java.lang.Package.isSealed(Package.java:179)
        at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1800)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:869)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1322)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:242)
        at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:430)



Tracking through this, the null pointer seems to be triggered by a null
url being passed ot the isSealed method. This is becuase the codebase
field of the ResourceEntry in the webapp classloader is null. I believe
this is because.

The findResourceInternal method of webapp classloader contains the
following block (starting at line 2091)

        synchronized (resourceEntries) {
            // Ensures that all the threads which may be in a race to load
            // a particular class all end up with the same ResourceEntry
            // instance
            ResourceEntry entry2 = (ResourceEntry) resourceEntries.get(name);
            if (entry2 == null) {
                resourceEntries.put(name, entry);
            } else {
                entry = entry2;
            }
        }


This results in the two threads that are trying to load the class both
returning the same ResourceEntry object, the first one that was loaded.
findResourceInternal was called from findClassInternal. findClassInteral
does the sealing check (on line 1800) then proceeds to the following
block (starting at line 1813)

        synchronized (this) {
            if (entry.loadedClass == null) {
                clazz = defineClass(name, entry.binaryContent, 0,
                        entry.binaryContent.length, 
                        codeSource);
                entry.loadedClass = clazz;
                entry.binaryContent = null;
                entry.source = null;
                entry.codeBase = null;
                entry.manifest = null;
                entry.certificates = null;
            } else {
                clazz = entry.loadedClass;
            }
        }

This synchronized block here ensures that the class is only defined
once, however because this sets the codebase to null, and the package
sealed check is outside the synchronized block, this results in the
second thread calling isSealed with a null code base, resulting in the
null pointer.

Has anyone else encountered this? Does anyone know if this is fixed in a
later version of tomcat, or if there is a work around?

Cheers
Ed

Re: Null Pointer In java.lang.Package.isSealed because of Thread Saftey Issue with Webapp Classloader

Posted by Mark Thomas <ma...@apache.org>.
Edward Costello wrote:
> Has anyone else encountered this? Does anyone know if this is fixed in a
> later version of tomcat, or if there is a work around?

You obviously have spent some time looking at the code. You may not be 
aware of the web interface to svn. Take a look at 
http://svn.apache.org/viewvc/tomcat/ and specifically 
http://svn.apache.org/viewvc/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/loader/WebappClassLoader.java?r1=466608&r2=469360&diff_format=h 
That should answer your questions.

Mark


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org