From 21fae865468fa0ec178078ca834e77964abf7d0a Mon Sep 17 00:00:00 2001 From: edwii-zhu Date: Fri, 27 Feb 2026 09:30:11 -0800 Subject: [PATCH] Fix DynamicFromArray thread-safety by using ThreadLocal pool (#55793) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `DynamicFromArray` uses a plain `Pools.SimplePool` shared across all threads. When two threads concurrently access the same pooled `DynamicFromArray` instance — one calling `recycle()` while the other reads from it — the reader sees `array == null` and throws `IllegalStateException("This dynamic value has been recycled")`. The sibling class `DynamicFromMap` was fixed for this exact issue in https://github.com/facebook/react-native/issues/17842 (2018) by switching to `ThreadLocal`, giving each thread its own isolated pool. A reviewer on that PR [noted](https://github.com/facebook/react-native/pull/17842#issuecomment-426784854) the same fix should be applied to `DynamicFromArray`, but it never was. This PR applies the identical `ThreadLocal` pattern from `DynamicFromMap` to `DynamicFromArray`. ## Changelog: [ANDROID] [FIXED] - Fix thread-safety crash in `DynamicFromArray` by using `ThreadLocal` instead of a shared `SimplePool`, matching the existing fix in `DynamicFromMap` Pull Request resolved: https://github.com/facebook/react-native/pull/55793 Test Plan: The change mirrors the existing pattern in `DynamicFromMap.kt` in the same directory — the only difference is the type parameter. The crash is a race condition that is not reliably reproducible in a unit test, but can be confirmed by: 1. Verify `DynamicFromArray.kt` now uses `ThreadLocal>` for its pool, `pool.get()?.acquire()` in `create()`, and `pool.get()?.release(this)` in `recycle()` 2. Verify this matches the pattern in `DynamicFromMap.kt` line-for-line 3. Build and run the Android app — no behavioral change expected since the pooling logic is identical, just thread-isolated Reviewed By: cortinico Differential Revision: D94676330 Pulled By: javache fbshipit-source-id: 6ba0be783dc44c913bf57040d57fd98e184636ba --- .../com/facebook/react/bridge/DynamicFromArray.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.kt index 458c42c3afc..eece940dd6c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.kt @@ -7,7 +7,7 @@ package com.facebook.react.bridge -import androidx.core.util.Pools +import androidx.core.util.Pools.SimplePool /** Implementation of Dynamic wrapping a ReadableArray. */ internal class DynamicFromArray private constructor() : Dynamic { @@ -17,7 +17,7 @@ internal class DynamicFromArray private constructor() : Dynamic { override fun recycle() { array = null index = -1 - pool.release(this) + pool.get()?.release(this) } override val type: ReadableType @@ -48,11 +48,16 @@ internal class DynamicFromArray private constructor() : Dynamic { array?.getString(index) ?: throw IllegalStateException("This dynamic value has been recycled") companion object { - private val pool = Pools.SimplePool(10) + private val pool: ThreadLocal> = + object : ThreadLocal>() { + override fun initialValue(): SimplePool { + return SimplePool(10) + } + } @JvmStatic fun create(array: ReadableArray, index: Int): DynamicFromArray { - val dynamic = pool.acquire() ?: DynamicFromArray() + val dynamic = pool.get()?.acquire() ?: DynamicFromArray() dynamic.array = array dynamic.index = index return dynamic