Skip to content

Commit b0d68c0

Browse files
haitaolifacebook-github-bot
authored andcommitted
Fix DynamicFromMap object pool synchronization (#17842)
Summary: DynamicFromMap internally uses SimplePool object to recycle dynamic prop objects. But the pool is not multi-thread safe. Currently the most used dynamic props are size props such as left, paddingVertical, marginTop and so on. These props are only accessed from the layout thread so the pool works fine. If a dynamic prop is needed in UI thread, then the two threads can access the same pool object and cause random errors. This PR make the pool object thread local to avoid synchronization. After this change there are two pool objects created in the process. Tested in official Airbnb app after updating accessibilityComponentType to be dynamic. Once this is merged, I'll send another PR to support "disabled" state in `accessibilityComponentType`. [ANDROID] [BUGFIX] [DynamicFromMap] - Fix a crash caused by dynamic props. Pull Request resolved: #17842 Differential Revision: D10374238 Pulled By: hramos fbshipit-source-id: 7ebf89c5abf06bd5fb43b205348ba4dc7e19517d
1 parent 7b15069 commit b0d68c0

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
import javax.annotation.Nullable;
1111

12-
import android.support.v4.util.Pools;
12+
import android.support.v4.util.Pools.SimplePool;
1313

1414
/**
1515
* Implementation of Dynamic wrapping a ReadableMap.
1616
*/
1717
public class DynamicFromMap implements Dynamic {
18-
private static final Pools.SimplePool<DynamicFromMap> sPool = new Pools.SimplePool<>(10);
18+
private static final ThreadLocal<SimplePool<DynamicFromMap>> sPool = new ThreadLocal<SimplePool<DynamicFromMap>>() {
19+
@Override
20+
protected SimplePool<DynamicFromMap> initialValue() {
21+
return new SimplePool<>(10);
22+
}
23+
};
1924

2025
private @Nullable ReadableMap mMap;
2126
private @Nullable String mName;
@@ -24,7 +29,7 @@ public class DynamicFromMap implements Dynamic {
2429
private DynamicFromMap() {}
2530

2631
public static DynamicFromMap create(ReadableMap map, String name) {
27-
DynamicFromMap dynamic = sPool.acquire();
32+
DynamicFromMap dynamic = sPool.get().acquire();
2833
if (dynamic == null) {
2934
dynamic = new DynamicFromMap();
3035
}
@@ -37,7 +42,7 @@ public static DynamicFromMap create(ReadableMap map, String name) {
3742
public void recycle() {
3843
mMap = null;
3944
mName = null;
40-
sPool.release(this);
45+
sPool.get().release(this);
4146
}
4247

4348
@Override

0 commit comments

Comments
 (0)