GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ClassLoader leak with Jetty and Karaf - static instances of java.lang.Throwable · Issue #868 · jetty/jetty.project

ClassLoader leak with Jetty and Karaf - static instances of java.lang.Throwable #868

New issue
Copy link
New issue
Copy link
Closed
Closed
ClassLoader leak with Jetty and Karaf - static instances of java.lang.Throwable#868
Copy link

Description

@grgrzybek
grgrzybek
opened on Aug 25, 2016
Issue body actions

There's magical (not visible via reflection, only in memory dumps) field inside java.lang.Throwable class called backtrace (source code).

At runtime, in native code, this field is filled with array of Objects which are actually java.lang.Class instances - and we know how sensitive they are in dynamic classloader environments.

In Karaf, through pax-web-jetty, when pax-web bundles are restarted without touching jetty bundles we have a memory leak visible in visualvm:

this     - value: org.apache.felix.framework.BundleWiringImpl #73
 <- m_wiring     - class: org.apache.felix.framework.BundleWiringImpl$BundleClassLoaderJava5, value: org.apache.felix.framework.BundleWiringImpl #73
  <- <classLoader>     - class: org.ops4j.pax.web.service.internal.Activator, value: org.apache.felix.framework.BundleWiringImpl$BundleClassLoaderJava5 #44
   <- [9]     - class: java.lang.Object[], value: org.ops4j.pax.web.service.internal.Activator class Activator
    <- [2]     - class: java.lang.Object[], value: java.lang.Object[] #14374
     <- backtrace     - class: java.lang.Throwable, value: java.lang.Object[] #14373
      <- COMPLETED     - class: org.eclipse.jetty.util.FutureCallback, value: java.lang.Throwable #1
       <- [68]     - class: java.lang.Object[], value: org.eclipse.jetty.util.FutureCallback class FutureCallback
        <- elementData     - class: java.util.Vector, value: java.lang.Object[] #11011
         <- classes     - class: org.apache.felix.framework.BundleWiringImpl$BundleClassLoaderJava5, value: java.util.Vector #112
          <- <classLoader>     - class: org.eclipse.jetty.util.thread.NonBlockingThread, value: org.apache.felix.framework.BundleWiringImpl$BundleClassLoaderJava5 #16
           <- <class> (Java frame)     - class: org.eclipse.jetty.util.thread.NonBlockingThread, value: org.eclipse.jetty.util.thread.NonBlockingThread class NonBlockingThread

The problem is with static instances of Throwable classes. They keep backtraces filled with Classes+ClassLoaders from previous incarnations of other bundles - depending on the time when the static Throwable was created.

Here are static throwables found in jetty-9.2.17.v20160517:

11:40 $ grep -r --include='*.java' 'static.*new.*Exception'
jetty-util/src/test/java/org/eclipse/jetty/util/component/LifeCycleListenerTest.java:    static Exception cause = new Exception("expected test exception");

11:40 $ grep -r --include='*.java' 'static.*new.*Throwable'
jetty-continuation/src/main/java/org/eclipse/jetty/continuation/FauxContinuation.java:    private final static ContinuationThrowable __exception = new ContinuationThrowable();
jetty-continuation/src/main/java/org/eclipse/jetty/continuation/Servlet3Continuation.java:    private final static ContinuationThrowable __exception = new ContinuationThrowable();
jetty-util/src/main/java/org/eclipse/jetty/util/SharedBlockingCallback.java:    private static Throwable IDLE = new Throwable()
jetty-util/src/main/java/org/eclipse/jetty/util/SharedBlockingCallback.java:    private static Throwable SUCCEEDED = new Throwable()
jetty-util/src/main/java/org/eclipse/jetty/util/SharedBlockingCallback.java:    private static Throwable FAILED = new Throwable()
jetty-util/src/main/java/org/eclipse/jetty/util/FutureCallback.java:    private static Throwable COMPLETED=new Throwable();
jetty-util/src/main/java/org/eclipse/jetty/util/FuturePromise.java:    private static Throwable COMPLETED=new Throwable();
jetty-util/src/main/java/org/eclipse/jetty/util/BlockingCallback.java:    private static Throwable SUCCEEDED=new Throwable()

Actually, for JDK 7+ the fix is simple, PR in next comment.

Activity

  1. grgrzybek
    added a commit that references this issue on Aug 25, 2016

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    a7c0a86
  2. grgrzybek commented on Aug 25, 2016

    @grgrzybek
    grgrzybek
    on Aug 25, 2016
    ContributorAuthor
    More actions

    PR #869 uses subclasses of java.lang.Throwable that use 4 argument constructors, allowing to create exceptions without backtrace/stackTrace.

  3. sbordet commented on Aug 25, 2016

    @sbordet
    sbordet
    on Aug 25, 2016
    Contributor
    More actions

    @grgrzybek this is surprising. Actually, I am more inclined to think it's a JVM bug. Java application code should not bother about magic hidden fields, no matter what the application does.
    For reference, see also: http://bugs.java.com/view_bug.do?bug_id=8033735

    Even using the 4 arguments constructor, I don't see any guarantee that the backtrace field is not filled by some JVM code.

  4. grgrzybek commented on Aug 25, 2016

    @grgrzybek
    grgrzybek
    on Aug 25, 2016
    ContributorAuthor
    More actions

    @sbordet I checked before&after heap dumps and looks like it helps with ClassLoader actually being GCed - no references kept in Throwables created with writableStackTrace==false.

    There's similar issue with JGit: https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/errors/StopWalkException.java#L56 and same fix (4 argument Exception constructor) helps.

    Of course I can't be sure some native code actually writes to this field, I'm also not sure how BacktraceBuilder class... But my fix looks promising for now.

    Javadoc for 4 argument Throwable says:

    If the writable stack trace is false, this constructor will not call fillInStackTrace(), a null will be written to the stackTrace field, and subsequent calls to fillInStackTrace and setStackTrace(StackTraceElement[]) will not set the stack trace. If the writable stack trace is false, getStackTrace will return a zero length array.

    which is what we need - no Class instances in Throwable.

  5. sbordet commented on Aug 26, 2016

    @sbordet
    sbordet
    on Aug 26, 2016
    Contributor
    More actions

    @grgrzybek then rather than applying the 4 args constructor in all the places, I'd prefer to have a utility class, say ConstantThrowable that does the 4 args constructor, in module jetty-util, and then have all the other classes extend this one.

    Can you comply with the required checks and update your PR ?

    Thanks !

  6. grgrzybek
    added a commit that references this issue on Aug 26, 2016

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    e2d9d00
  7. grgrzybek commented on Aug 26, 2016

    @grgrzybek
    grgrzybek
    on Aug 26, 2016
    ContributorAuthor
    More actions

    @sbordet sure! I'll refactor private StaticThrowable classes from all required places and use single definition. Thanks for checking!

  8. grgrzybek
    added 4 commits that reference this issue on Aug 26, 2016

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    d37a315

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    bc9fb29

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    68f1793

    Issue jetty#868 - Use static Throwables without backtrace/stackTrace

    b70f0fd
  9. grgrzybek commented on Aug 26, 2016

    @grgrzybek
    grgrzybek
    on Aug 26, 2016
    ContributorAuthor
    More actions

    @sbordet done - I've updated PR #869 for jetty-9.2.x branch, but also pushed 3 other to my fork - for 9.3.x, 9.4.x and master. Should I create PRs for these branches as well?

  10. sbordet commented on Aug 26, 2016

    @sbordet
    sbordet
    on Aug 26, 2016
    Contributor
    More actions

    @grgrzybek just do 9.2.x, we'll do the merge with the other branches.

  11. grgrzybek commented on Aug 26, 2016

    @grgrzybek
    grgrzybek
    on Aug 26, 2016
    ContributorAuthor
    More actions

    @sbordet done, PR #869 is being checked by Jenkins now and ip-validation already passed OK

  12. janbartel
    added a commit that references this issue on Aug 31, 2016

    Merge pull request #869 from grgrzybek/jetty-9.2.x-868

    c07d842
  13. grgrzybek commented on Aug 31, 2016

    @grgrzybek
    grgrzybek
    on Aug 31, 2016
    ContributorAuthor
    More actions

    @janbartel thanks for merging!
    I'll close this issue when fix is merged to 9.3.x, 9.4.x and master, ok?

  14. janbartel commented on Aug 31, 2016

    @janbartel
    janbartel
    on Aug 31, 2016
    Contributor
    More actions

    @grgrzybek I'll close it when I'm done merging. Thanks.

  15. janbartel commented on Aug 31, 2016

    @janbartel
    janbartel
    on Aug 31, 2016
    Contributor
    More actions

    Committed to jetty-9.2.x and merged to jetty-9.3.x jetty-9.4.x and master.

  16. janbartel
    closed this as completedon Aug 31, 2016
  17. grgrzybek commented on Aug 31, 2016

    @grgrzybek
    grgrzybek
    on Aug 31, 2016
    ContributorAuthor
    More actions

    @janbartel many many thanks!

  18. janbartel commented on Aug 31, 2016

    @janbartel
    janbartel
    on Aug 31, 2016
    Contributor
    More actions

    @grgrzybek thank you for the pull request.

  19. joakime
    added a commit that references this issue on Sep 7, 2016

    Bumping up minimum to Java 7 everywhere

    3347c74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.