[jira] (JETTY-1555) Log.getLogger() could return null due to a thread issue

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[jira] (JETTY-1555) Log.getLogger() could return null due to a thread issue

JIRA jira@codehaus.org
Issue Type: Bug Bug
Affects Versions: 8.1.2
Assignee: Unassigned
Attachments: fix.txt
Created: 24/Jan/13 9:59 PM
Description:

When Log.getLogger() is called in two threads simultaneously, one of

org/eclipse/jetty/util/log/Log.java
132     private static Logger LOG;
133     private static boolean __initialized;
134 
135     public static boolean initialized()
136     {
137         if (LOG != null)
138         {
139             return true;
140         }
141 
142         synchronized (Log.class)
143         {
144             if (__initialized)
145             {
146                 return LOG != null;
147             }
148             __initialized = true;
149         }
150 
151         try
152         {
153             Class<?> log_class = Loader.loadClass(Log.class, __logClass);
154             if (LOG == null || !LOG.getClass().equals(log_class))
155             {
156                 LOG = (Logger)log_class.newInstance();
157                 LOG.debug("Logging to {} via {}", LOG, log_class.getName());
158             }
159         }
160         catch(Throwable e)
161         {
162             // Unable to load specified Logger implementation, default to standard logging.
163             initStandardLogging(e);
164         }
165 
166         return LOG != null;
167     }

If T1 runs to line 148 and update the __initialized flag to true, then T2 runs to line 144, T2 probably will hit line 146 and return false (if the master memory copy is flushed to T2). Then getLogger will return null. See line 430-431 below:

org/eclipse/jetty/util/log/Log.java
428     public static Logger getLogger(String name)
429     {
430         if (!initialized())
431             return null;

Most developers won't expect Log.getLogger() return null. In my application, I embedded jetty 8.1.2 and occasionally I see NPE when the jetty server starts up, around 5~10% probability.

Can we simplify and fix it this way?

private static volatile Logger LOG;

    public static void initialize()
    {   
        if (LOG != null)
        {   
            return;
        }

        synchronized (Log.class)
        {   
            if (LOG == null) {
                try 
                {   
                    Class<?> log_class = Loader.loadClass(Log.class, __logClass);
                    if (LOG == null || !LOG.getClass().equals(log_class))
                    {   
                        LOG = (Logger)log_class.newInstance();
                        LOG.debug("Logging to {} via {}", LOG, log_class.getName());
                    }   
                }   
                catch(Throwable e)
                {   
                    // Unable to load specified Logger implementation, default to standard logging.
                    initStandardLogging(e);
                }   
            }
        }   
    }
Project: Jetty
Priority: Major Major
Reporter: Daniel Wu
Original Estimate: 12 hours
Remaining Estimate: 12 hours
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

[jira] (JETTY-1555) Log.getLogger() could return null due to a thread issue

JIRA jira@codehaus.org
Daniel Wu commented on Bug JETTY-1555

this is the error I encountered

java.lang.NullPointerException
	at org.eclipse.jetty.util.component.Container.add(Container.java:200)
	at org.eclipse.jetty.util.component.Container.update(Container.java:164)
	at org.eclipse.jetty.util.component.Container.update(Container.java:106)
	at org.eclipse.jetty.server.Server.setConnectors(Server.java:186)
	at ....
Container.java
198     private void add(Object parent, Object child, String relationship)
199     {
200         if (LOG.isDebugEnabled()) // LOG is null and NPE will be thrown
201             LOG.debug("Container "+parent+" + "+child+" as "+relationship);
202         if (_listeners!=null)
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

[jira] (JETTY-1555) Log.getLogger() could return null due to a thread issue

JIRA jira@codehaus.org
In reply to this post by JIRA jira@codehaus.org
Change By: Joakim Erdfelt (25/Jan/13 6:45 AM)
Priority: Major Minor
Issue Type: Bug Improvement
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|

[jira] (JETTY-1555) Log.getLogger() could return null due to a thread issue

JIRA jira@codehaus.org
In reply to this post by JIRA jira@codehaus.org
Joakim Erdfelt commented on Improvement JETTY-1555

org.eclipse.jetty.util.log.Log is a singleton that has the classic Double-checked Locking bug.
Unfortunately, that will still be present, even with your proposed fix.

To alleviate this, initialize org.eclipse.jetty.util.log.Log at your application startup time, don't wait for it to lazy init.

Simple call:

Log.getLogger(); // initialize jetty logging

At your earliest convenience, before you attempt to use Jetty in a multi-threaded situation.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email