Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2756

ClassLoaderContextSelector: WeakReference and GC

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.13.0
    • None
    • Core
    • None

    Description

      A race-condition between GC and WeakReference in ClassLoaderContextSelector can lead to a NPE in Log4jContextFactory.

      Exception in thread "main" java.lang.NullPointerException
          at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
          at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
          at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
          at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:602)
      

      In org.apache.logging.log4j.core.selector.ClassLoaderContextSelector there is the following creation of a LoggerContext in method locateContext:

                  LoggerContext ctx = createContext(name, configLocation);
                  final AtomicReference<WeakReference<LoggerContext>> r = new AtomicReference<>();
                  r.set(new WeakReference<>(ctx));
                  CONTEXT_MAP.putIfAbsent(name, r);
                  ctx = CONTEXT_MAP.get(name).get().get();
                  return ctx;
      

      The created LoggerContext is stored in local variable ctx. After storing the LoggerContext into a WeakReference the local variable isn't read until overridden by a new value out of the CONTEXT_MAP. A garbage collection between creating the WeakReference and reading the CONTEXT_MAP might remove the LoggerContext.

      This leads to intermittent failures in JVMs like OpenJ9 which mark locals as not-an-object so they can be collected as soon as possible. See also: https://github.com/eclipse/openj9/issues/4005

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            rogmann Sascha Rogmann
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment