World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#13075] fix(catalog): inspect thread targets without resolving subclass fields (#13076)
Qi Yu132202914e94
diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.javaindex b4c33ffa8a..cff678a05c 100644--- a/catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java+++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/utils/ClassLoaderResourceCleanerUtils.java@@ -197,17 +197,22 @@ public class ClassLoaderResourceCleanerUtils {if (thread == null) {return false;}- if (thread.getContextClassLoader() == targetClassLoader- || thread.getClass().getClassLoader() == targetClassLoader) {- return true;- }try {- Object runnable = FieldUtils.readField(thread, "target", true);+ if (thread.getContextClassLoader() == targetClassLoader+ || thread.getClass().getClassLoader() == targetClassLoader) {+ return true;+ }+ // Inspect Thread's own target, not a subclass's fields. Reflecting on a driver thread+ // subclass can resolve unavailable field types, or find a shadowed target field.+ Field targetField = Thread.class.getDeclaredField("target");+ targetField.setAccessible(true);+ Object runnable = targetField.get(thread);if (runnable != null && runnable.getClass().getClassLoader() == targetClassLoader) {return true;}- } catch (Exception e) {- LOG.debug("Cannot read the runnable of thread {}", thread.getName(), e);+ } catch (Exception | LinkageError e) {+ // A stale thread's dependencies must not abort cleanup of the remaining threads.+ LOG.debug("Cannot inspect the classloader ownership of thread {}", thread.getName(), e);}return false;diff --git a/catalogs/catalog-common/src/test/java/org/apache/gravitino/utils/TestClassLoaderResourceCleanerUtils.java b/catalogs/catalog-common/src/test/java/org/apache/gravitino/utils/TestClassLoaderResourceCleanerUtils.javaindex 52e4d10b6a..953a75fdcf 100644--- a/catalogs/catalog-common/src/test/java/org/apache/gravitino/utils/TestClassLoaderResourceCleanerUtils.java+++ b/catalogs/catalog-common/src/test/java/org/apache/gravitino/utils/TestClassLoaderResourceCleanerUtils.java@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNull;import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;import java.lang.ref.SoftReference;@@ -53,6 +54,108 @@ class TestClassLoaderResourceCleanerUtils {}}+ /** A thread whose unrelated field type can become unavailable during classloader cleanup. */+ public static class ThreadWithMissingDependency extends Thread {+ /** A field whose type the isolated test loader deliberately cannot resolve. */+ public Leaky dependency;++ /**+ * Creates a thread with the supplied runnable.+ *+ * @param runnable the actual thread target+ */+ public ThreadWithMissingDependency(Runnable runnable) {+ super(runnable);+ }+ }++ private static class ThreadWithShadowedTarget extends Thread {+ @SuppressWarnings("unused")+ private final Runnable target;++ private ThreadWithShadowedTarget(Runnable actualTarget, Runnable shadowedTarget) {+ super(actualTarget);+ target = shadowedTarget;+ setContextClassLoader(null);+ }+ }++ @Test+ void testThreadInspectionDoesNotResolveSubclassFieldTypes() throws Exception {+ URL location = getClass().getProtectionDomain().getCodeSource().getLocation();+ try (URLClassLoader child =+ new URLClassLoader(new URL[] {location}, null) {+ @Override+ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {+ if (name.equals(Leaky.class.getName())) {+ throw new ClassNotFoundException(name);+ }+ return super.loadClass(name, resolve);+ }+ }) {+ Thread thread =+ (Thread)+ child+ .loadClass(ThreadWithMissingDependency.class.getName())+ .getConstructor(Runnable.class)+ .newInstance((Runnable) () -> {});+ thread.setContextClassLoader(null);+ assertThrows(NoClassDefFoundError.class, () -> thread.getClass().getDeclaredFields());+ assertTrue(+ ClassLoaderResourceCleanerUtils.runningWithClassLoader(+ thread, getClass().getClassLoader()));+ }+ }++ @Test+ void testThreadInspectionIgnoresShadowedTarget() throws Exception {+ try (URLClassLoader child = childLoaderOwning(LeakyTask.class)) {+ Runnable owned =+ (Runnable)+ child.loadClass(LeakyTask.class.getName()).getDeclaredConstructor().newInstance();+ Runnable unrelated = () -> {};+ assertTrue(+ ClassLoaderResourceCleanerUtils.runningWithClassLoader(+ new ThreadWithShadowedTarget(owned, unrelated), child));+ assertFalse(+ ClassLoaderResourceCleanerUtils.runningWithClassLoader(+ new ThreadWithShadowedTarget(unrelated, owned), child));+ }+ }++ @Test+ void testThreadInspectionToleratesLinkageErrors() {+ Thread thread =+ new Thread() {+ @Override+ public ClassLoader getContextClassLoader() {+ throw new NoClassDefFoundError("unavailable driver dependency");+ }+ };+ assertFalse(+ ClassLoaderResourceCleanerUtils.runningWithClassLoader(+ thread, getClass().getClassLoader()));+ }++ @Test+ void testThreadInspectionDoesNotSwallowFatalErrors() {+ OutOfMemoryError failure = new OutOfMemoryError("test error");+ Thread thread =+ new Thread() {+ @Override+ public ClassLoader getContextClassLoader() {+ throw failure;+ }+ };+ assertSame(+ failure,+ assertThrows(+ OutOfMemoryError.class,+ () ->+ ClassLoaderResourceCleanerUtils.runningWithClassLoader(+ thread, getClass().getClassLoader())));+ }+private static URLClassLoader childLoaderOwning(Class<?> clazz) throws Exception {URL location = clazz.getProtectionDomain().getCodeSource().getLocation();// A null parent keeps delegation off the app loader, so the child defines the class itself.