From d9f5319cf0d9828b29d0e350284b22ce29985042 Mon Sep 17 00:00:00 2001 From: Haitao Li Date: Sat, 13 Oct 2018 17:40:45 -0700 Subject: [PATCH] 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: https://github.com/facebook/react-native/pull/17842 Differential Revision: D10374238 Pulled By: hramos fbshipit-source-id: 7ebf89c5abf06bd5fb43b205348ba4dc7e19517d --- .../com/facebook/react/bridge/DynamicFromMap.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java index 4d268ea8614ccc..c7273d5ccd3428 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java @@ -9,13 +9,18 @@ import javax.annotation.Nullable; -import android.support.v4.util.Pools; +import android.support.v4.util.Pools.SimplePool; /** * Implementation of Dynamic wrapping a ReadableMap. */ public class DynamicFromMap implements Dynamic { - private static final Pools.SimplePool sPool = new Pools.SimplePool<>(10); + private static final ThreadLocal> sPool = new ThreadLocal>() { + @Override + protected SimplePool initialValue() { + return new SimplePool<>(10); + } + }; private @Nullable ReadableMap mMap; private @Nullable String mName; @@ -24,7 +29,7 @@ public class DynamicFromMap implements Dynamic { private DynamicFromMap() {} public static DynamicFromMap create(ReadableMap map, String name) { - DynamicFromMap dynamic = sPool.acquire(); + DynamicFromMap dynamic = sPool.get().acquire(); if (dynamic == null) { dynamic = new DynamicFromMap(); } @@ -37,7 +42,7 @@ public static DynamicFromMap create(ReadableMap map, String name) { public void recycle() { mMap = null; mName = null; - sPool.release(this); + sPool.get().release(this); } @Override