From 515264f88aea2c1f0030c20330f6dccdbbd9064c Mon Sep 17 00:00:00 2001 From: Michal Zimonczyk Date: Tue, 14 Jul 2026 12:13:32 +0200 Subject: [PATCH] Keep thread name after attaching it to JVM When native thread is attached to JVM, then it's name is taken from JavaVMAttachArgs. When no JavaVMAttachArgs, or no JavaVMAttachArgs::name passed, then JVM on its own decides on naming thread. Those names are not descriptive. To preserve thread name, pass the currently set thread name in JavaVMAttachArgs::name. Relates-To: HNAV-15387 Signed-off-by: Michal Zimonczyk --- .../src/http/android/utils/JNIThreadBinder.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/olp-cpp-sdk-core/src/http/android/utils/JNIThreadBinder.h b/olp-cpp-sdk-core/src/http/android/utils/JNIThreadBinder.h index a6a0e58a4..9883ab9e6 100644 --- a/olp-cpp-sdk-core/src/http/android/utils/JNIThreadBinder.h +++ b/olp-cpp-sdk-core/src/http/android/utils/JNIThreadBinder.h @@ -22,6 +22,7 @@ #ifdef __ANDROID__ #include +#include namespace olp { namespace http { @@ -35,7 +36,9 @@ class JNIThreadBinder final { : attached_(false), jni_env_(nullptr), jni_vm_(vm) { if (jni_vm_->GetEnv(reinterpret_cast(&jni_env_), JNI_VERSION_1_6) != JNI_OK) { - if (jni_vm_->AttachCurrentThread(&jni_env_, nullptr) == JNI_OK) { + captureThreadName(); + auto attachArgs = getJvmAttachArgs(); + if (jni_vm_->AttachCurrentThread(&jni_env_, &attachArgs) == JNI_OK) { attached_ = true; } } @@ -56,9 +59,23 @@ class JNIThreadBinder final { JNIEnv* GetEnv() const { return jni_env_; } private: + void captureThreadName() { + if (prctl(PR_GET_NAME, threadName_) != 0) { + threadName_[0] = '\0'; + } + } + + JavaVMAttachArgs getJvmAttachArgs() const { + return JavaVMAttachArgs{.version = JNI_VERSION_1_6, + .name = threadName_[0] ? threadName_ : nullptr, + .group = nullptr}; + } + bool attached_; JNIEnv* jni_env_; JavaVM* jni_vm_; + constexpr static size_t kThreadNameMaxLength = 16; + char threadName_[kThreadNameMaxLength] = {0}; }; } // namespace utils