clee704 opened a new issue, #11531:
URL: https://github.com/apache/incubator-gluten/issues/11531

   ## Description
   
   When running tests with LeakSanitizer enabled, a memory leak is detected 
because protobuf's static internal data structures are not properly cleaned up 
when the JNI library is unloaded.
   
   ## Root Cause
   
   Gluten uses protobuf for parsing `ConfigMap` and Substrait plans. When the 
protobuf library initializes, it creates static internal data structures. 
According to the [protobuf 
documentation](https://protobuf.dev/reference/cpp/api-docs/google.protobuf.message_lite/),
 dynamically-loaded libraries that use protobuf must call 
`google::protobuf::ShutdownProtobufLibrary()` during cleanup to free these 
static-duration objects when the library is unloaded. Without this call, the 
static objects remain allocated after library unload, causing LeakSanitizer to 
report a memory leak.
   
   ## LeakSanitizer Output
   
   ```
   ==4790==ERROR: LeakSanitizer: detected memory leaks
   Direct leak of 88 byte(s) in 1 object(s) allocated from:
       #0 operator new(unsigned long) asan_new_delete.cpp
       #1 gluten::ConfigMap::_InternalParse (libgluten.so+0x9ebd49)
   ```
   
   ## Proposed Fix
   
   Add `google::protobuf::ShutdownProtobufLibrary()` call at the end of 
`JNI_OnUnload()` in `cpp/core/jni/JniWrapper.cc`:
   
   ```cpp
   #include <google/protobuf/stubs/common.h>
   
   void JNI_OnUnload(JavaVM* vm, void* reserved) {
     // ... existing cleanup code ...
     
     getJniErrorState()->close();
     getJniCommonState()->close();
     
     google::protobuf::ShutdownProtobufLibrary();
   }
   ```
   
   ## Impact
   
   - **Scope**: Minimal change with only 3 lines added (1 include + 1 blank 
line + 1 function call)
   - **Risk**: Low - only adds proper cleanup code that executes during library 
unload
   - **Behavior**: No functional changes to runtime behavior; only prevents 
memory leaks during cleanup


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to