Uploaded image for project: 'Quarkus'
  1. Quarkus
  2. QUARKUS-2854

Fix memory leak in Agroal local connection cache for Netty's FastThreadLocalThread

XMLWordPrintable

      https://github.com/quarkusio/quarkus/blob/2.15/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java#L234

      https://github.com/agroal/agroal/blob/2.0/agroal-pool/src/main/java/io/agroal/pool/ConnectionPool.java#L745

      https://github.com/agroal/agroal/blob/2.0/agroal-pool/src/main/java/io/agroal/pool/ConnectionHandler.java#L173

      1. Quarkus uses `QuarkusNettyConnectionCache` for Agroal local connection cache if it finds class `FastThreadLocal`.
      2. Agroal `ConnectionPool` calls `localCache.reset()` in its `ReapTask` periodically (5min in Quarkus), the `FastThreadLocal` instance is created again in `reset()` but actually the old thread local `ConnectionHandler` is still in array `FastThreadLocalThread.threadLocalMap.indexedVariables`.
      3. `ConnectionHandler.closeConnection()` doesn't set its fields to null, so it still holds reference to JDBC connection such as `ConnectionImpl` in mysql-connector-j, the JDBC connection still holds some heavy Java objects, thus `localCache.reset()` leads to heavy memory leak after running for about a week.

      The solution is to not create `FastThreadLocal` again, so the memory leak is limited by total count of Java threads that use JDBC to access database.

      This bug is introduced since Quarkus-2.2.0 at https://github.com/quarkusio/quarkus/issues/17304 .

              Unassigned Unassigned
              probinso_jira Quarkus JIRA Bot
              Fedor Dudinskii Fedor Dudinskii
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: