Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8222027

TEST_BUG: java/util/logging/LogManager/TestLoggerNames.java generates intermittent ClassCastException

XMLWordPrintable

        The test java/util/logging/LogManager/TestLoggerNames.java generates intermittent ClassCastException following some garbage collection.
        Here is the test code snippet from TestLoggerNames.java:

         LogManager.getLogManager().addLogger(new TestLogger("com.foo.bar.zzz", null));
        ...
        TestLogger test = (TestLogger)Logger.getLogger("com.foo.bar.zzz"); --> j.l.ClassCastException here

        As per Java doc java.util.logging.LogManager.addLogger():
        ```
        The application should retain its own reference to the Logger object to avoid it being garbage collected. The LogManager may only retain a weak reference.
        ```
        Java doc for java.util.logging.Logger.getLogger():
        ```
        Find or create a logger for a named subsystem. If a logger has already been created with the given name it is returned. Otherwise a new logger is created.
        ```
        Here is how a java.lang.ClassCastException happened:

        1) LogManager added a new TestLogger("com.foo.bar.zzz") which is a subclass of java.util.logging.Logger;
        2) GC was triggered and collected the newly added TestLogger instance;
        3)Logger.getLogger("com.foo.bar.zzz") can't find a logger for the name com.foo.bar.zzz hence creates a new logger which is an instance of java.util.logging.Logger, then ClassCastException is thrown when the test attempts to cast the return instance to TestLogger.

        If the TestLogger("com.foo.bar.zzz") instance wasn't collected by GC, Logger.getLogger("com.foo.bar.zzz") can retrieve it successfully and the test passes.

        This was consistently recreated in a simple test case by adding explicit System.gc() call between the TestLogger being added and retrieved from the LogManager, the ClassCastException is thrown consistently.

              sgroeger Steve Groeger
              sgroeger Steve Groeger
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: