Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49596

User session memory leak

    XMLWordPrintable

Details

    Description

      The latest version of Jenkins core uses Jetty 9.4, this version seems changes the way to manage the session cache and now it is infinite by default, this makes that the session cache is not cleaned with the time and cause a memory leak.

      This script gets the EvictionPolicy value and returns -1

      import net.bull.javamelody.* 
      def sessionMapField = SessionListener.class.getDeclaredField('SESSION_MAP_BY_ID') 
      sessionMapField.setAccessible(true) 
      def sessions = sessionMapField.get(null) 
      for (def sessionKV : sessions) { 
      def session = sessionKV.value 
        println session.getSessionHandler().getSessionCache().getEvictionPolicy() 
      }
      

      Jetty was upgraded from 9.2 to 9.4 in Jenkins 2.61, The old Jetty uses a different caching mechanism, which seems to have a non-infinite default eviction policy

      The workaround is to place this script at JENKINS_HOME/init.groovy.d to set the eviction policy to 30 min.

      import net.bull.javamelody.* 
      def sessionMapField = SessionListener.class.getDeclaredField('SESSION_MAP_BY_ID') 
      sessionMapField.setAccessible(true) 
      def sessions = sessionMapField.get(null) 
      for (def sessionKV : sessions) { 
      def session = sessionKV.value 
         session.getSessionHandler().getSessionCache().setEvictionPolicy(1800) 
      }
      

      These are other utility scripts to check the leak.

      This script list the sessions

      import net.bull.javamelody.* 
      println SessionListener.getSessionCount() + " sessions:" 
      def sessioninfos = SessionListener.getAllSessionsInformations() 
      for (sessioninfo in sessioninfos) { 
        println sessioninfo 
      }
      

      This script returns the number of sessions in cache

      import net.bull.javamelody.*
      
      def sessionMapField = SessionListener.class.getDeclaredField('SESSION_MAP_BY_ID')
      sessionMapField.setAccessible(true)
      def sessions = sessionMapField.get(null)
      for (def sessionKV: sessions) {
        def session = sessionKV.value
        def sessionCache = session.getSessionHandler().getSessionCache()
        def sessionsField = sessionCache.class.getDeclaredField('_sessions')
        sessionsField.setAccessible(true)
        println sessionsField.get(sessionCache).size()
      }
      

      This script force to clear the session cache

      import net.bull.javamelody.*
        
      def sessionMapField = SessionListener.class.getDeclaredField('SESSION_MAP_BY_ID')
      sessionMapField.setAccessible(true)
      def sessions = sessionMapField.get(null)
      for (def sessionKV: sessions) {
        def session = sessionKV.value
        def sessionCache = session.getSessionHandler().getSessionCache()
        def sessionsField = sessionCache.class.getDeclaredField('_sessions')
        sessionsField.setAccessible(true)
        def sessionKeys = sessionsField.get(sessionCache).keys()
        for (def sessionKey: sessionKeys) {
          sessionCache.delete(sessionKey)
        }
      }
      

      Attachments

        Activity

          Code changed in jenkins
          User: Oleg Nenashev
          Path:
          CHANGELOG.md
          http://jenkins-ci.org/commit/winstone/9137572c6fe1bdb3ef510be5ee0ee9fbc9f56d43
          Log:
          JENKINS-49596 - Noting 4.1.2

          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Oleg Nenashev Path: CHANGELOG.md http://jenkins-ci.org/commit/winstone/9137572c6fe1bdb3ef510be5ee0ee9fbc9f56d43 Log: JENKINS-49596 - Noting 4.1.2
          oleg_nenashev Oleg Nenashev added a comment -

          The fix has been integrated towards 2.110.
          It may be considered as LTS candidate since we decoupled the meaty Jetty update part

          oleg_nenashev Oleg Nenashev added a comment - The fix has been integrated towards 2.110. It may be considered as LTS candidate since we decoupled the meaty Jetty update part

          Code changed in jenkins
          User: Oleg Nenashev
          Path:
          war/pom.xml
          http://jenkins-ci.org/commit/jenkins/805a7ab57d4d25f8c5565c5af0b16b36a780a8c9
          Log:
          JENKINS-49596 - Update Winstone to 4.1.2 (#3307)

          • JENKINS-49596 - Pick 4.1.2 with extended session eviction defaults

          (cherry picked from commit 852ecdc5f2a7802fa74f425bd17ba028f787b2c1)

          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Oleg Nenashev Path: war/pom.xml http://jenkins-ci.org/commit/jenkins/805a7ab57d4d25f8c5565c5af0b16b36a780a8c9 Log: JENKINS-49596 - Update Winstone to 4.1.2 (#3307) JENKINS-49596 - Update Winstone to 4.1.1 JENKINS-49596 - Pick 4.1.2 with extended session eviction defaults (cherry picked from commit 852ecdc5f2a7802fa74f425bd17ba028f787b2c1)

          Prior to this fix, I'd been setting --sessionTimeout=NNNNN in my java options to extend the session.  Is there an equivalent way to set the eviction policy to longer than 30 minutes, or is my only option to add a groovy script to JENKINS_HOME/init.groovy.d?

          marcmac marc macintyre added a comment - Prior to this fix, I'd been setting --sessionTimeout=NNNNN in my java options to extend the session.  Is there an equivalent way to set the eviction policy to longer than 30 minutes, or is my only option to add a groovy script to JENKINS_HOME/init.groovy.d?
          dnusbaum Devin Nusbaum added a comment - - edited

          marcmac Based on the README I think you want to pass --sessionEviction=foo in addition to --sessionTimeout=foo.

          dnusbaum Devin Nusbaum added a comment - - edited marcmac Based on the  README I think you want to pass --sessionEviction=foo in addition to --sessionTimeout=foo .

          People

            olamy Olivier Lamy
            ifernandezcalvo Ivan Fernandez Calvo
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: