fix(java): resolve JNI classloader bug on dispatcher thread in Spark#6549
Open
summaryzb wants to merge 3 commits intolance-format:mainfrom
Open
fix(java): resolve JNI classloader bug on dispatcher thread in Spark#6549summaryzb wants to merge 3 commits intolance-format:mainfrom
summaryzb wants to merge 3 commits intolance-format:mainfrom
Conversation
Move AsyncScanner class resolution from the JNI dispatcher thread to JNI_OnLoad, which runs on the thread with the correct application classloader. Pass a GlobalRef to the dispatcher so it never calls FindClass on a native thread that only has the system classloader. In Spark executors, user JARs are loaded by an isolated MutableURLClassLoader/ChildFirstURLClassLoader chain installed per-thread inside TaskRunner.run(). JNI native threads attached via attach_current_thread_permanently() never receive this classloader, causing ClassNotFoundException for org.lance.ipc.AsyncScanner. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Change-Id: I166bec22f7373e6140cdc20c573723bcaeff48e7
Change-Id: Ic2a8e094a5fd600d8148853bdf930fe70261897b
Author
|
Fix #6577 |
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a JNI classloader bug where the
AsyncScannerclass could not be found on the lance-jni-dispatcher thread in Spark executor environments. Spark loads user JARs through an isolatedMutableURLClassLoader/ChildFirstURLClassLoaderchain that is only installed on Spark's managed task threads — JNI native threads attached viaattach_current_thread_permanently()never receive this classloader and fall back to the JVM system classloader, which cannot see user classes.Problem
Error log
In Spark executor environments, user JARs (including lance) are not on the JVM system classpath. Instead, Spark constructs a custom classloader chain during
Executorinitialization (Executor.scala:newSessionState()):This classloader is installed per-thread inside
TaskRunner.run()(Executor.scala:580):Thread.currentThread.setContextClassLoader(isolatedSession.replClassLoader)Only Spark's managed task-pool threads receive this call. When lance's Rust dispatcher thread calls
jvm.attach_current_thread_permanently(), the JVM creates a new Java thread that:TaskRunner.run()setContextClassLoader()called with Spark's custom classloaderWhen the dispatcher thread then called
env.find_class("org/lance/ipc/AsyncScanner"), the JVM used this system classloader to resolve the class and failed:This is a well-known JNI pitfall: native threads attached to the JVM via
AttachCurrentThread/attach_current_thread_permanentlyalways get the system classloader, not the application classloader that originally loaded the library.Approach
The fix leverages the fact that
JNI_OnLoadis called on the thread that triggeredSystem.loadLibrary(), which has access to the correct application classloader. The class resolution is moved toJNI_OnLoad, where aGlobalRefto theAsyncScannerclass is created. ThisGlobalRefis then passed intoDispatcher::initialize, so the dispatcher thread never needs to callfind_classat all -- it uses the pre-resolved class reference to look up method IDs.This approach is the standard JNI pattern for solving classloader issues: resolve classes eagerly on a thread with the right classloader and cache
GlobalRefs for later use on native threads.