| 1 |
/* |
| 2 |
* Copyright (C) 2017 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.util; |
| 18 |
|
| 19 |
import com.zaxxer.hikari.pool.TestElf; |
| 20 |
import com.zaxxer.hikari.pool.TestElf.FauxWebClassLoader; |
| 21 |
import com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry; |
| 22 |
import org.junit.FixMethodOrder; |
| 23 |
import org.junit.Test; |
| 24 |
import org.junit.runners.MethodSorters; |
| 25 |
import org.slf4j.Logger; |
| 26 |
import org.slf4j.LoggerFactory; |
| 27 |
|
| 28 |
import java.lang.ref.Reference; |
| 29 |
import java.lang.reflect.Field; |
| 30 |
import java.lang.reflect.Method; |
| 31 |
import java.util.Collection; |
| 32 |
import java.util.ConcurrentModificationException; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.concurrent.CompletableFuture; |
| 35 |
|
| 36 |
import static com.zaxxer.hikari.pool.TestElf.isJava9; |
| 37 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 38 |
import static org.junit.Assert.assertNotNull; |
| 39 |
import static org.junit.Assert.assertNull; |
| 40 |
import static org.junit.Assume.assumeTrue; |
| 41 |
|
| 42 |
/** |
| 43 |
* @author Brett Wooldridge |
| 44 |
*/ |
| 45 |
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 46 |
public class TomcatConcurrentBagLeakTest |
| 47 |
{ |
| 48 |
@Test |
| 49 |
public void testConcurrentBagForLeaks() throws Exception |
| 50 |
{ |
| 51 |
assumeTrue(!isJava9()); |
| 52 |
|
| 53 |
ClassLoader cl = new FauxWebClassLoader(); |
| 54 |
Class<?> clazz = cl.loadClass(this.getClass().getName() + "$FauxWebContext"); |
| 55 |
Object fauxWebContext = clazz.newInstance(); |
| 56 |
|
| 57 |
Method createConcurrentBag = clazz.getDeclaredMethod("createConcurrentBag"); |
| 58 |
createConcurrentBag.invoke(fauxWebContext); |
| 59 |
|
| 60 |
Field failureException = clazz.getDeclaredField("failureException"); |
| 61 |
Exception ex = (Exception) failureException.get(fauxWebContext); |
| 62 |
assertNull(ex); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void testConcurrentBagForLeaks2() throws Exception |
| 67 |
{ |
| 68 |
assumeTrue(!isJava9()); |
| 69 |
|
| 70 |
ClassLoader cl = this.getClass().getClassLoader(); |
| 71 |
Class<?> clazz = cl.loadClass(this.getClass().getName() + "$FauxWebContext"); |
| 72 |
Object fauxWebContext = clazz.newInstance(); |
| 73 |
|
| 74 |
Method createConcurrentBag = clazz.getDeclaredMethod("createConcurrentBag"); |
| 75 |
createConcurrentBag.invoke(fauxWebContext); |
| 76 |
|
| 77 |
Field failureException = clazz.getDeclaredField("failureException"); |
| 78 |
Exception ex = (Exception) failureException.get(fauxWebContext); |
| 79 |
assertNotNull(ex); |
| 80 |
} |
| 81 |
|
| 82 |
public static class PoolEntry implements IConcurrentBagEntry |
| 83 |
{ |
| 84 |
private int state; |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean compareAndSet(int expectState, int newState) |
| 88 |
{ |
| 89 |
this.state = newState; |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void setState(int newState) |
| 95 |
{ |
| 96 |
this.state = newState; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public int getState() |
| 101 |
{ |
| 102 |
return state; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@SuppressWarnings("unused") |
| 107 |
public static class FauxWebContext |
| 108 |
{ |
| 109 |
private static final Logger log = LoggerFactory.getLogger(FauxWebContext.class); |
| 110 |
|
| 111 |
@SuppressWarnings("WeakerAccess") |
| 112 |
public Exception failureException; |
| 113 |
|
| 114 |
@SuppressWarnings({"unused", "ResultOfMethodCallIgnored"}) |
| 115 |
public void createConcurrentBag() throws InterruptedException |
| 116 |
{ |
| 117 |
try (ConcurrentBag<PoolEntry> bag = new ConcurrentBag<>((x) -> CompletableFuture.completedFuture(Boolean.TRUE))) { |
| 118 |
|
| 119 |
PoolEntry entry = new PoolEntry(); |
| 120 |
bag.add(entry); |
| 121 |
|
| 122 |
PoolEntry borrowed = bag.borrow(100, MILLISECONDS); |
| 123 |
bag.requite(borrowed); |
| 124 |
|
| 125 |
PoolEntry removed = bag.borrow(100, MILLISECONDS); |
| 126 |
bag.remove(removed); |
| 127 |
} |
| 128 |
|
| 129 |
checkThreadLocalsForLeaks(); |
| 130 |
} |
| 131 |
|
| 132 |
private void checkThreadLocalsForLeaks() |
| 133 |
{ |
| 134 |
Thread[] threads = getThreads(); |
| 135 |
|
| 136 |
try { |
| 137 |
// Make the fields in the Thread class that store ThreadLocals |
| 138 |
// accessible |
| 139 |
Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); |
| 140 |
threadLocalsField.setAccessible(true); |
| 141 |
Field inheritableThreadLocalsField = Thread.class.getDeclaredField("inheritableThreadLocals"); |
| 142 |
inheritableThreadLocalsField.setAccessible(true); |
| 143 |
// Make the underlying array of ThreadLoad.ThreadLocalMap.Entry objects |
| 144 |
// accessible |
| 145 |
Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); |
| 146 |
Field tableField = tlmClass.getDeclaredField("table"); |
| 147 |
tableField.setAccessible(true); |
| 148 |
Method expungeStaleEntriesMethod = tlmClass.getDeclaredMethod("expungeStaleEntries"); |
| 149 |
expungeStaleEntriesMethod.setAccessible(true); |
| 150 |
|
| 151 |
for (Thread thread : threads) { |
| 152 |
Object threadLocalMap; |
| 153 |
if (thread != null) { |
| 154 |
|
| 155 |
// Clear the first map |
| 156 |
threadLocalMap = threadLocalsField.get(thread); |
| 157 |
if (null != threadLocalMap) { |
| 158 |
expungeStaleEntriesMethod.invoke(threadLocalMap); |
| 159 |
checkThreadLocalMapForLeaks(threadLocalMap, tableField); |
| 160 |
} |
| 161 |
|
| 162 |
// Clear the second map |
| 163 |
threadLocalMap = inheritableThreadLocalsField.get(thread); |
| 164 |
if (null != threadLocalMap) { |
| 165 |
expungeStaleEntriesMethod.invoke(threadLocalMap); |
| 166 |
checkThreadLocalMapForLeaks(threadLocalMap, tableField); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
catch (Throwable t) { |
| 172 |
log.warn("Failed to check for ThreadLocal references for web application [{}]", getContextName(), t); |
| 173 |
failureException = new Exception(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private Object getContextName() |
| 178 |
{ |
| 179 |
return this.getClass().getName(); |
| 180 |
} |
| 181 |
|
| 182 |
// THE FOLLOWING CODE COPIED FROM APACHE TOMCAT (2017/01/08) |
| 183 |
|
| 184 |
/** |
| 185 |
* Analyzes the given thread local map object. Also pass in the field that |
| 186 |
* points to the internal table to save re-calculating it on every |
| 187 |
* call to this method. |
| 188 |
*/ |
| 189 |
private void checkThreadLocalMapForLeaks(Object map, Field internalTableField) throws IllegalAccessException, NoSuchFieldException |
| 190 |
{ |
| 191 |
if (map != null) { |
| 192 |
Object[] table = (Object[]) internalTableField.get(map); |
| 193 |
if (table != null) { |
| 194 |
for (Object obj : table) { |
| 195 |
if (obj != null) { |
| 196 |
boolean keyLoadedByWebapp = false; |
| 197 |
boolean valueLoadedByWebapp = false; |
| 198 |
// Check the key |
| 199 |
Object key = ((Reference<?>) obj).get(); |
| 200 |
if (this.equals(key) || loadedByThisOrChild(key)) { |
| 201 |
keyLoadedByWebapp = true; |
| 202 |
} |
| 203 |
// Check the value |
| 204 |
Field valueField = obj.getClass().getDeclaredField("value"); |
| 205 |
valueField.setAccessible(true); |
| 206 |
Object value = valueField.get(obj); |
| 207 |
if (this.equals(value) || loadedByThisOrChild(value)) { |
| 208 |
valueLoadedByWebapp = true; |
| 209 |
} |
| 210 |
if (keyLoadedByWebapp || valueLoadedByWebapp) { |
| 211 |
Object[] args = new Object[5]; |
| 212 |
args[0] = getContextName(); |
| 213 |
if (key != null) { |
| 214 |
args[1] = getPrettyClassName(key.getClass()); |
| 215 |
try { |
| 216 |
args[2] = key.toString(); |
| 217 |
} catch (Exception e) { |
| 218 |
log.warn("Unable to determine string representation of key of type [{}]", args[1], e); |
| 219 |
args[2] = "Unknown"; |
| 220 |
} |
| 221 |
} |
| 222 |
if (value != null) { |
| 223 |
args[3] = getPrettyClassName(value.getClass()); |
| 224 |
try { |
| 225 |
args[4] = value.toString(); |
| 226 |
} catch (Exception e) { |
| 227 |
log.warn("webappClassLoader.checkThreadLocalsForLeaks.badValue {}", args[3], e); |
| 228 |
args[4] = "Unknown"; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if (valueLoadedByWebapp) { |
| 233 |
log.error("The web application [{}] created a ThreadLocal with key " + |
| 234 |
"(value [{}]) and a value of type [{}] (value [{}]) but failed to remove " + |
| 235 |
"it when the web application was stopped. Threads are going to be renewed " + |
| 236 |
"over time to try and avoid a probable memory leak.", args); |
| 237 |
failureException = new Exception(); |
| 238 |
} else if (value == null) { |
| 239 |
log.debug("The web application [{}] created a ThreadLocal with key of type [{}] " + |
| 240 |
"(value [{}]). The ThreadLocal has been correctly set to null and the " + |
| 241 |
"key will be removed by GC.", args); |
| 242 |
failureException = new Exception(); |
| 243 |
} else { |
| 244 |
log.debug("The web application [{}] created a ThreadLocal with key of type [{}] " + |
| 245 |
"(value [{}]) and a value of type [{}] (value [{}]). Since keys are only " + |
| 246 |
"weakly held by the ThreadLocal Map this is not a memory leak.", args); |
| 247 |
failureException = new Exception(); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* @param o object to test, may be null |
| 258 |
* @return <code>true</code> if o has been loaded by the current classloader |
| 259 |
* or one of its descendants. |
| 260 |
*/ |
| 261 |
private boolean loadedByThisOrChild(Object o) { |
| 262 |
if (o == null) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
Class<?> clazz; |
| 267 |
if (o instanceof Class) { |
| 268 |
clazz = (Class<?>) o; |
| 269 |
} else { |
| 270 |
clazz = o.getClass(); |
| 271 |
} |
| 272 |
|
| 273 |
ClassLoader cl = clazz.getClassLoader(); |
| 274 |
while (cl != null) { |
| 275 |
if (cl == this.getClass().getClassLoader()) { |
| 276 |
return true; |
| 277 |
} |
| 278 |
cl = cl.getParent(); |
| 279 |
} |
| 280 |
|
| 281 |
if (o instanceof Collection<?>) { |
| 282 |
Iterator<?> iter = ((Collection<?>) o).iterator(); |
| 283 |
try { |
| 284 |
while (iter.hasNext()) { |
| 285 |
Object entry = iter.next(); |
| 286 |
if (loadedByThisOrChild(entry)) { |
| 287 |
return true; |
| 288 |
} |
| 289 |
} |
| 290 |
} catch (ConcurrentModificationException e) { |
| 291 |
log.warn("Failed to check for ThreadLocal references for web application [{}]", getContextName(), e); |
| 292 |
} |
| 293 |
} |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
* Get the set of current threads as an array. |
| 299 |
*/ |
| 300 |
private Thread[] getThreads() |
| 301 |
{ |
| 302 |
// Get the current thread group |
| 303 |
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
| 304 |
// Find the root thread group |
| 305 |
try { |
| 306 |
while (tg.getParent() != null) { |
| 307 |
tg = tg.getParent(); |
| 308 |
} |
| 309 |
} |
| 310 |
catch (SecurityException se) { |
| 311 |
log.warn("Unable to obtain the parent for ThreadGroup [{}]. It will not be possible to check all threads for potential memory leaks", tg.getName(), se); |
| 312 |
} |
| 313 |
|
| 314 |
int threadCountGuess = tg.activeCount() + 50; |
| 315 |
Thread[] threads = new Thread[threadCountGuess]; |
| 316 |
int threadCountActual = tg.enumerate(threads); |
| 317 |
// Make sure we don't miss any threads |
| 318 |
while (threadCountActual == threadCountGuess) { |
| 319 |
threadCountGuess *= 2; |
| 320 |
threads = new Thread[threadCountGuess]; |
| 321 |
// Note tg.enumerate(Thread[]) silently ignores any threads that |
| 322 |
// can't fit into the array |
| 323 |
threadCountActual = tg.enumerate(threads); |
| 324 |
} |
| 325 |
|
| 326 |
return threads; |
| 327 |
} |
| 328 |
|
| 329 |
private String getPrettyClassName(Class<?> clazz) |
| 330 |
{ |
| 331 |
String name = clazz.getCanonicalName(); |
| 332 |
if (name == null) { |
| 333 |
name = clazz.getName(); |
| 334 |
} |
| 335 |
return name; |
| 336 |
} |
| 337 |
} |
| 338 |
} |