Commit 21fae86
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 #17842 (2018) by switching to `ThreadLocal<SimplePool>`, giving each thread its own isolated pool. A reviewer on that PR [noted](#17842 (comment)) 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: #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: 6ba0be783dc44c913bf57040d57fd98e184636ba1 parent c3acbc4 commit 21fae86
1 file changed
Lines changed: 9 additions & 4 deletions
File tree
- packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge
Lines changed: 9 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
52 | 57 | | |
53 | 58 | | |
54 | 59 | | |
55 | | - | |
| 60 | + | |
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
| |||
0 commit comments