Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][ci] Improve thread leak detection by ignoring more Testcontainers threads #21499

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
Expand Down Expand Up @@ -56,6 +57,18 @@ public class ThreadLeakDetectorListener extends BetweenTestClassesListenerAdapte

private Set<ThreadKey> capturedThreadKeys;

private static final Field THREAD_TARGET_FIELD;
static {
Field targetField = null;
try {
targetField = Thread.class.getDeclaredField("target");
targetField.setAccessible(true);
} catch (NoSuchFieldException e) {
// ignore this error. on Java 21, the field is not present
// TODO: add support for extracting the Runnable target on Java 21
}
THREAD_TARGET_FIELD = targetField;
}

@Override
protected void onBetweenTestClasses(Class<?> endedTestClass, Class<?> startedTestClass) {
Expand Down Expand Up @@ -201,10 +214,37 @@ private static boolean shouldSkipThread(Thread thread) {
if (threadName.equals("Grizzly-HttpSession-Expirer")) {
return true;
}
// Testcontainers AbstractWaitStrategy.EXECUTOR
if (threadName.startsWith("testcontainers-wait-")) {
return true;
}
}
Runnable target = extractRunnableTarget(thread);
if (target != null) {
String targetClassName = target.getClass().getName();
// ignore threads that contain a Runnable class under org.testcontainers package
if (targetClassName.startsWith("org.testcontainers.")) {
return true;
}
}
return false;
}

// use reflection to extract the Runnable target from a thread so that we can detect threads created by
// Testcontainers based on the Runnable's class name.
private static Runnable extractRunnableTarget(Thread thread) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: maybe add a comment on why we have to use reflection here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (THREAD_TARGET_FIELD == null) {
return null;
}
Runnable target = null;
try {
target = (Runnable) THREAD_TARGET_FIELD.get(thread);
} catch (IllegalAccessException e) {
LOG.warn("Cannot access target field in Thread.class", e);
}
return target;
}

/**
* Unique key for a thread
* Based on thread id and it's identity hash code
Expand Down