From 1b63d8be654dc0bd60c6e745fad5453b98ca4027 Mon Sep 17 00:00:00 2001 From: msaf Date: Sun, 11 Oct 2026 20:26:22 +0100 Subject: [PATCH] fix: load the native library once per JVM when NativeBase is reloaded NativeBase.load() after setLoaded(false), which CometSparkSessionExtensionsSuite does to test the Arrow properties, unpacked libcomet to a new temporary file and loaded it again. The JVM then held two copies of the library with separate statics: native methods linked before the reload stayed bound to the first copy, methods first called after it bound to the second. Native.cancelPlan, first called only when a task is killed, looked up the plan in the second copy's empty registry, so CometNativeTaskKillSuite failed whenever it ran after that suite in the same JVM. load() now reapplies the logging and Arrow settings without loading the library a second time. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../main/java/org/apache/comet/NativeBase.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spark/src/main/java/org/apache/comet/NativeBase.java b/spark/src/main/java/org/apache/comet/NativeBase.java index 15ddf8bd8b3..6b76d990cce 100644 --- a/spark/src/main/java/org/apache/comet/NativeBase.java +++ b/spark/src/main/java/org/apache/comet/NativeBase.java @@ -48,6 +48,7 @@ public abstract class NativeBase { private static final String libraryToLoad = System.mapLibraryName(NATIVE_LIB_NAME); private static boolean loaded = false; + private static boolean libraryLoaded = false; private static volatile Throwable loadErr = null; private static final String searchPattern = "libcomet-"; private static final AtomicBoolean released = new AtomicBoolean(false); @@ -91,13 +92,18 @@ static synchronized void load() { return; } - // Try to load Comet library from the java.library.path. - try { - System.loadLibrary(NATIVE_LIB_NAME); + if (libraryLoaded) { loaded = true; - } catch (UnsatisfiedLinkError ex) { - // Doesn't exist, so proceed to loading bundled library. - bundleLoadLibrary(); + } else { + // Try to load Comet library from the java.library.path. + try { + System.loadLibrary(NATIVE_LIB_NAME); + loaded = true; + } catch (UnsatisfiedLinkError ex) { + // Doesn't exist, so proceed to loading bundled library. + bundleLoadLibrary(); + } + libraryLoaded = loaded; } initWithLogConf();