forked from apache/activemq-artemis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadLeakCheckRule.java
317 lines (269 loc) · 10.9 KB
/
ThreadLeakCheckRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.utils;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jboss.logging.Logger;
import org.junit.Assert;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
/**
* This is useful to make sure you won't have leaking threads between tests
*/
public class ThreadLeakCheckRule extends TestWatcher {
private static Logger log = Logger.getLogger(ThreadLeakCheckRule.class);
private static Set<String> knownThreads = new HashSet<>();
protected boolean enabled = true;
protected boolean testFailed = false;
protected Description testDescription = null;
protected Throwable failure = null;
protected Map<Thread, StackTraceElement[]> previousThreads;
/**
* Override to set up your specific external resource.
*
* @throws if setup fails (which will disable {@code after}
*/
@Override
protected void starting(Description description) {
// do nothing
previousThreads = Thread.getAllStackTraces();
}
public void disable() {
enabled = false;
}
@Override
protected void failed(Throwable e, Description description) {
this.failure = e;
this.testFailed = true;
this.testDescription = description;
}
@Override
protected void succeeded(Description description) {
this.testFailed = false;
}
/**
* Override to tear down your specific external resource.
*/
@Override
protected void finished(Description description) {
log.debug("checking thread enabled? " + enabled + " testFailed? " + testFailed);
try {
if (enabled) {
boolean failed = true;
boolean failedOnce = false;
// if the test failed.. there's no point on waiting a full minute.. we will report it once and go
long timeout = System.currentTimeMillis() + (testFailed ? 30000 : 60000);
while (failed && timeout > System.currentTimeMillis()) {
failed = checkThread();
if (failed) {
failedOnce = true;
forceGC();
try {
Thread.sleep(500);
} catch (Throwable e) {
}
}
}
if (failed) {
if (!testFailed) {
//we only fail on thread leak if test passes.
Assert.fail("Thread leaked");
} else {
System.out.println("***********************************************************************");
System.out.println(" The test failed and there is a leak");
System.out.println("***********************************************************************");
failure.printStackTrace();
Assert.fail("Test " + testDescription + " Failed with a leak - " + failure.getMessage());
}
} else if (failedOnce) {
System.out.println("******************** Threads cleared after retries ********************");
System.out.println();
}
} else {
enabled = true;
}
} finally {
// clearing just to help GC
previousThreads = null;
}
}
private static int failedGCCalls = 0;
public static void forceGC() {
if (failedGCCalls >= 10) {
log.info("ignoring forceGC call since it seems System.gc is not working anyways");
return;
}
log.info("#test forceGC");
CountDownLatch finalized = new CountDownLatch(1);
WeakReference<DumbReference> dumbReference = new WeakReference<>(new DumbReference(finalized));
long timeout = System.currentTimeMillis() + 1000;
// A loop that will wait GC, using the minimal time as possible
while (!(dumbReference.get() == null && finalized.getCount() == 0) && System.currentTimeMillis() < timeout) {
System.gc();
System.runFinalization();
try {
finalized.await(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
}
if (dumbReference.get() != null) {
failedGCCalls++;
log.info("It seems that GC is disabled at your VM");
} else {
// a success would reset the count
failedGCCalls = 0;
}
log.info("#test forceGC Done ");
}
public static void forceGC(final Reference<?> ref, final long timeout) {
long waitUntil = System.currentTimeMillis() + timeout;
// A loop that will wait GC, using the minimal time as possible
while (ref.get() != null && System.currentTimeMillis() < waitUntil) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add("Some string with garbage with concatenation " + i);
}
list.clear();
list = null;
System.gc();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
public static void removeKownThread(String name) {
knownThreads.remove(name);
}
public static void addKownThread(String name) {
knownThreads.add(name);
}
private boolean checkThread() {
boolean failedThread = false;
Map<Thread, StackTraceElement[]> postThreads = Thread.getAllStackTraces();
if (postThreads != null && previousThreads != null && postThreads.size() > previousThreads.size()) {
for (Thread aliveThread : postThreads.keySet()) {
if (aliveThread.isAlive() && !isExpectedThread(aliveThread) && !previousThreads.containsKey(aliveThread)) {
if (!failedThread) {
System.out.println("*********************************************************************************");
System.out.println("LEAKING THREADS");
}
failedThread = true;
System.out.println("=============================================================================");
System.out.println("Thread " + aliveThread + " is still alive with the following stackTrace:");
StackTraceElement[] elements = postThreads.get(aliveThread);
for (StackTraceElement el : elements) {
System.out.println(el);
}
aliveThread.interrupt();
}
}
if (failedThread) {
System.out.println("*********************************************************************************");
}
}
return failedThread;
}
/**
* if it's an expected thread... we will just move along ignoring it
*
* @param thread
* @return
*/
private boolean isExpectedThread(Thread thread) {
final String threadName = thread.getName();
final ThreadGroup group = thread.getThreadGroup();
final boolean isSystemThread = group != null && "system".equals(group.getName());
final String javaVendor = System.getProperty("java.vendor");
if (threadName.contains("SunPKCS11")) {
return true;
} else if (threadName.contains("Keep-Alive-Timer")) {
return true;
} else if (threadName.contains("Attach Listener")) {
return true;
} else if ((javaVendor.contains("IBM") || isSystemThread) && threadName.equals("process reaper")) {
return true;
} else if ((javaVendor.contains("IBM") || isSystemThread) && threadName.equals("ClassCache Reaper")) {
return true;
} else if (javaVendor.contains("IBM") && threadName.equals("MemoryPoolMXBean notification dispatcher")) {
return true;
} else if (threadName.contains("MemoryMXBean")) {
return true;
} else if (threadName.contains("globalEventExecutor")) {
return true;
} else if (threadName.contains("threadDeathWatcher")) {
return true;
} else if (threadName.contains("netty-threads")) {
// This is ok as we use EventLoopGroup.shutdownGracefully() which will shutdown things with a bit of delay
// if the EventLoop's are still busy.
return true;
} else if (threadName.contains("threadDeathWatcher")) {
//another netty thread
return true;
} else if (threadName.contains("Abandoned connection cleanup thread")) {
// MySQL Engine checks for abandoned connections
return true;
} else if (threadName.contains("hawtdispatch") || (group != null && group.getName().contains("hawtdispatch"))) {
// Static workers used by MQTT client.
return true;
} else if (threadName.contains("ObjectCleanerThread")) {
// Required since upgrade to Netty 4.1.22 maybe because https://github.com/netty/netty/commit/739e70398ccb6b11ffa97c6b5f8d55e455a2165e
return true;
} else if (threadName.contains("RMI TCP")) {
return true;
} else if (threadName.contains("RMI Scheduler")) {
return true;
} else if (threadName.contains("RMI RenewClean")) {
return true;
} else if (threadName.contains("Signal Dispatcher")) {
return true;
} else if (threadName.contains("ForkJoinPool.commonPool")) {
return true;
} else if (threadName.contains("GC Daemon")) {
return true;
} else {
for (StackTraceElement element : thread.getStackTrace()) {
if (element.getClassName().contains("org.jboss.byteman.agent.TransformListener")) {
return true;
}
}
for (String known : knownThreads) {
if (threadName.contains(known)) {
return true;
}
}
return false;
}
}
protected static class DumbReference {
private CountDownLatch finalized;
public DumbReference(CountDownLatch finalized) {
this.finalized = finalized;
}
@Override
public void finalize() throws Throwable {
finalized.countDown();
super.finalize();
}
}
}