This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new 721a828e657 [bugfix](jnileak) should use jni util to get jni env 
(#66990)
721a828e657 is described below

commit 721a828e657e75ac3c7149fa71c5162f367f1834
Author: yiguolei <[email protected]>
AuthorDate: Fri Aug 21 11:38:15 2026 +0800

    [bugfix](jnileak) should use jni util to get jni env (#66990)
    
    ### What problem does this PR solve?
    
    Issue Number: close #xxx
    
    Related PR: #xxx
    
    Problem Summary:
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Test <!-- At least one of them must be included. -->
        - [ ] Regression test
        - [ ] Unit Test
        - [ ] Manual test (add detailed scripts or steps below)
        - [ ] No need to test or manual test. Explain why:
    - [ ] This is a refactor/code format and no logic has been changed.
            - [ ] Previous test can cover this change.
            - [ ] No code files have been changed.
            - [ ] Other reason <!-- Add your reason?  -->
    
    - Behavior changed:
        - [ ] No.
        - [ ] Yes. <!-- Explain the behavior change -->
    
    - Does this need documentation?
        - [ ] No.
    - [ ] Yes. <!-- Add document PR link here. eg:
    https://github.com/apache/doris-website/pull/1214 -->
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label <!-- Add branch pick label that this PR
    should merge into -->
---
 .../writer/paimon/jni_paimon_write_backend.cpp     | 33 ++++------------------
 1 file changed, 6 insertions(+), 27 deletions(-)

diff --git a/be/src/exec/sink/writer/paimon/jni_paimon_write_backend.cpp 
b/be/src/exec/sink/writer/paimon/jni_paimon_write_backend.cpp
index 576efa7d21a..456e3bdfa29 100644
--- a/be/src/exec/sink/writer/paimon/jni_paimon_write_backend.cpp
+++ b/be/src/exec/sink/writer/paimon/jni_paimon_write_backend.cpp
@@ -139,7 +139,7 @@ Status get_paimon_arrow_schema_from_block(const Block& 
block,
 } // namespace
 
 // ────────────────────────────────────────────────────────────
-// JNI helpers — JVM attachment and class loading
+// JNI helpers — class loading
 // ────────────────────────────────────────────────────────────
 
 static constexpr const char* PAIMON_JNI_WRITER_CLASS = 
"org/apache/doris/paimon/PaimonJniWriter";
@@ -156,27 +156,6 @@ PaimonJniWriterOpenMode 
PaimonJniWriterOpenMode::from_write_mode(
             static_cast<jboolean>(write_mode == TPaimonWriteMode::CHANGELOG)};
 }
 
-/// Attach the current native thread to the JVM if not already attached,
-/// and return a valid JNIEnv pointer.
-static Status _get_jni_env(JNIEnv** env) {
-    JavaVM* jvm = nullptr;
-    jsize n_vms = 0;
-    jint result = JNI_GetCreatedJavaVMs(&jvm, 1, &n_vms);
-    if (result != JNI_OK || n_vms == 0) {
-        return Status::InternalError("Failed to get created JavaVM");
-    }
-    result = jvm->GetEnv(reinterpret_cast<void**>(env), JNI_VERSION_1_8);
-    if (result == JNI_EDETACHED) {
-        result = jvm->AttachCurrentThread(reinterpret_cast<void**>(env), 
nullptr);
-        if (result != JNI_OK) {
-            return Status::InternalError("Failed to attach current thread to 
JVM");
-        }
-    } else if (result != JNI_OK) {
-        return Status::InternalError("Failed to get JNIEnv");
-    }
-    return Status::OK();
-}
-
 JniPaimonWriteBackend::~JniPaimonWriteBackend() {
     Status st = close();
     if (!st.ok()) {
@@ -192,7 +171,7 @@ Status JniPaimonWriteBackend::close() {
     }
 
     JNIEnv* env = nullptr;
-    Status env_status = _get_jni_env(&env);
+    Status env_status = Jni::Env::Get(&env);
     if (!env_status.ok()) {
         bool java_users_may_exist = _jni_writer_obj != nullptr;
         // JNI global references cannot be released without an environment.
@@ -318,7 +297,7 @@ Status JniPaimonWriteBackend::open(const TPaimonTableSink& 
sink, RuntimeState* s
     _native_page_memory_peak = ADD_COUNTER(jni_profile, 
"NativePageMemoryPeak", TUnit::BYTES);
 
     JNIEnv* env = nullptr;
-    RETURN_IF_ERROR(_get_jni_env(&env));
+    RETURN_IF_ERROR(Jni::Env::Get(&env));
 
     // Step 1: Load PaimonJniWriter class through ScannerLoader (Paimon jars 
are
     // not on the default application classpath, so FindClass won't work).
@@ -474,7 +453,7 @@ Status 
JniPaimonWriter::_write_projected_block(RuntimeState* state, Block& block
     // call PaimonJniWriter.write(ByteBuffer). Java side reads the Arrow IPC
     // stream via ArrowStreamReader.
     JNIEnv* env = nullptr;
-    RETURN_IF_ERROR(_get_jni_env(&env));
+    RETURN_IF_ERROR(Jni::Env::Get(&env));
 
     jobject direct_buffer =
             env->NewDirectByteBuffer(buffer->mutable_data(), 
static_cast<jlong>(buffer->size()));
@@ -494,7 +473,7 @@ Status JniPaimonWriter::write(RuntimeState* state, Block& 
block) {
 
 Status JniPaimonWriter::prepare_commit(std::vector<TPaimonCommitMessage>& 
messages) {
     JNIEnv* env = nullptr;
-    RETURN_IF_ERROR(_get_jni_env(&env));
+    RETURN_IF_ERROR(Jni::Env::Get(&env));
 
     // Call PaimonJniWriter.prepareCommit() which returns byte[][] —
     // each element is a DPCM-framed serialized CommitMessage chunk produced
@@ -546,7 +525,7 @@ Status 
JniPaimonWriter::prepare_commit(std::vector<TPaimonCommitMessage>& messag
 
 Status JniPaimonWriter::abort() {
     JNIEnv* env = nullptr;
-    RETURN_IF_ERROR(_get_jni_env(&env));
+    RETURN_IF_ERROR(Jni::Env::Get(&env));
     env->CallVoidMethod(_jni_writer_obj, _abort_id);
     return Jni::Env::GetJniExceptionMsg(env, true, "JNI exception in abort: ");
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to