mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix DynamicFromArray thread-safety by using ThreadLocal pool (#55793)
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<SimplePool>`, 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<SimplePool>` 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<Pools.SimplePool<DynamicFromArray>>` 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
This commit is contained in:
1 parent
c3acbc424d
commit
21fae86546
1 file changed
+9
-4
+9
-4
@@ -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<DynamicFromArray>(10)
|
||||
private val pool: ThreadLocal<SimplePool<DynamicFromArray>> =
|
||||
object : ThreadLocal<SimplePool<DynamicFromArray>>() {
|
||||
override fun initialValue(): SimplePool<DynamicFromArray> {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user