Fix stack-use-after-return in TaskDispatchThread Android ctor (#57629)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57629

Under `dev_clang_asan`, `bloks_runtimeTestsAndroid` crashes at startup with `__asan_report_load4` inside `ThreadScope::WithClassLoader`. The Android branch of the `TaskDispatchThread` constructor captures the `priorityOffset` parameter by reference into the `std::thread` lambda; the constructor returns immediately, so by the time the thread runs and reads `priorityOffset` inside `setpriority(...)` the stack slot is gone. Non-asan builds tolerate the racy read; asan catches it deterministically.

Capture `priorityOffset` (and `this`) by value on both the outer thread lambda and the inner `WithClassLoader` lambda.

Changelog:
[Internal]

Reviewed By: christophpurrer

Differential Revision: D113046624

fbshipit-source-id: 7536661d52a1af1a63e0596379f9e41f85fb62f0
This commit is contained in:
Pieter De Baets authored and meta-codesync[bot] committed 2026-07-22 02:28:46 -07:00
1 parent eb4d3892ab
commit 677f571059
1 file changed
+6 -3
@@ -27,9 +27,12 @@ TaskDispatchThread::TaskDispatchThread(
int priorityOffset) noexcept
: threadName_(threadName) {
#ifdef ANDROID
// Attaches the thread to JVM just in case anything calls out to Java
thread_ = std::thread([&]() {
facebook::jni::ThreadScope::WithClassLoader([&]() {
// Attaches the thread to JVM just in case anything calls out to Java.
// Capture priorityOffset by value: it is a constructor parameter whose
// storage dies when the constructor returns, and the thread body reads it
// asynchronously.
thread_ = std::thread([this, priorityOffset]() {
facebook::jni::ThreadScope::WithClassLoader([this, priorityOffset]() {
int result = setpriority(
PRIO_PROCESS,
static_cast<pid_t>(::syscall(SYS_gettid)),