Repository navigation
Make the view manager prop setter caches thread-safe - #59245
Open
brownrice777 wants to merge 1 commit into
Open
brownrice777 wants to merge 1 commit into
brownrice777 wants to merge 1 commit into
Conversation
ViewManagersPropertyCache.CLASS_PROPS_CACHE and the two setter maps in ViewManagerPropertyUpdater are static HashMaps. Two threads write them: - the UI thread, when it updates the props of a view (ViewManager.updateProperties -> ViewManagerPropertyUpdater.updateProps); - the JS thread, when it builds a view config (getConstantsForViewManager -> ViewManager.nativeProps). In bridgeless mode, the JS thread gets the view config of a lazy view manager on first use, during render. At that time the UI thread can mount other views. Concurrent writes to a HashMap can lose entries or break the map. This change uses ConcurrentHashMap for the three maps. The get-then-put code does not change. If two threads compute the same entry, they make equal values, so the second write is harmless. computeIfAbsent is not used, because getNativePropSettersForViewManagerClass calls itself for the superclass.
brownrice777
marked this pull request as ready for review
October 20, 2026 20:22
Contributor
|
I'm trying to figure out why we haven't seen this internally. We use the generated viewmanager delegates exclusively, so do those take a different path here? |
Contributor
|
Ah, when using ViewManagerDelegates, we won't be using the prop setters at all, so there's no race on the UI thread. we've also disabled We also don't consume the |
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D126631569. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
ViewManagersPropertyCache.CLASS_PROPS_CACHEand the two setter maps inViewManagerPropertyUpdaterare staticHashMaps. Two threads write them:ViewManagerPropertyUpdater.updateProps→findManagerSetter);getConstantsForViewManager→ViewManager.nativeProps).In bridgeless mode, JS gets the view config of a lazy view manager on first use, during render. At that time, the UI thread can mount other views. Concurrent writes to a
HashMapcan lose entries or break the map.This change uses
ConcurrentHashMapfor the three maps. The get-then-put code does not change. If two threads compute the same entry, the values are equal, so the second write is harmless. I did not usecomputeIfAbsent, becausegetNativePropSettersForViewManagerClasscalls itself for the superclass.This came up in the review of #59173, which sends the eager view managers through the same on-demand path.
Changelog:
[ANDROID] [FIXED] - Make the view manager prop setter caches thread-safe
Test Plan:
Unit tests:
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.uimanager.*"passes (230 tests, 0 failures).Device: Xiaomi Mi Note 2 (Snapdragon 821, Android 8.0). App: a production Expo app, release build, React Native 0.88.0, Hermes. I compiled this change and #59173 against 0.88.0 and replaced the same classes in
react-android-0.88.0-release.aar. I measured from process start to the first frame with rows in the first list, 12 cold starts for each build.getConstants()To see the cost of this change alone, I also used a build whose JS does not call
getConstants()at startup, so the first view configs come fromgetConstantsForViewManager()during render. Without this change: 2189 ms median (2140–2236 ms). With it: 2192 ms median (2156–2244 ms). I see no difference.Same chat app with threads list and messages. With this change, I opened the chat list, a thread, settings, the theme list, the profile and the contacts, and scrolled. Logcat shows no crash and no view config errors.