Bug #92508 mysql-connector in bootclasspath causing memory leak
Submitted: 19 Sep 2018 16:05 Modified: 5 Dec 2018 18:57
Reporter: Bernhard Lenz Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:8.0.12 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any
Tags: AbandonedConnectionCleanupThread, connectionPhantomRefs, memory leak

[19 Sep 2018 16:05] Bernhard Lenz
Description:
We found that when adding the mysql-connector into the java bootclasspath using -Xbootclasspath/a:./mysql-connector-java-8.0.12.jar the connector is causing a memory leak. 

The reason the driver is in the bootclasspath is, because we are using a 3rd party java agent (-javaagent:./3rdpartyjavaagent.jar) which requires a database connection and the only way to pass a driver jar to an 3rd party agent without modifying the agent jar is to use the bootclasspath. 

When the mysql-connector is in the bootclasspath, the AbandonedConnectionCleanupThread is failing with a NullPointerException
in line "threadRef.getContextClassLoader().getResource("");". It appears that if mysql-connector is in the bootclasspath, getContextClassLoader returns null. 

Subsequently the AbandonedConnectionCleanupThread shuts down and the NonRegisteringDriver.connectionPhantomRefs ConcurrentHashMap keeps growing, causing a memory leak, as the refs never get removed due to AbandonedConnectionCleanupThread not running anymore. 

How to repeat:
Add -Xbootclasspath/a:./mysql-connector-java-8.0.12.jar and debug AbandonedConnectionCleanupThread to observe the NullPointerException in threadRef.getContextClassLoader().getResource("");

Suggested fix:
check threadRef.getContextClassLoader() to see if it is null before calling getResource(""). Allow thread to run if getContextClassLoader is null. 

Additionally I suggest to add more logging to AbandonedConnectionCleanupThread as exceptions just get swallowed, without any possibility to determine what is wrong.
[4 Oct 2018 16:28] Filipe Silva
Hi Bernhard,

Thank you for this bug report. It was verified as described.
[5 Dec 2018 18:57] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 8.0.14 and 5.1.48  changelogs:

"A memory leak occurred if Connector/J was loaded via the bootstrap class path instead of the main application classpath. It was because AbandonedConnectionCleanupThread failed to initialize its internal thread in that case, so that references for closed connections were not cleaned up, and their number kept growing. This fix repairs the clean up process for closed connections and also makes the process thread safe."