From e681736e1838904155409e236c3c2fa630b71c9a Mon Sep 17 00:00:00 2001 From: Eric Kreutzer Date: Thu, 2 Apr 2026 11:59:13 -0700 Subject: [PATCH] Fix Android accessibility descendant race in ReactViewGroup (#56182) Summary: Guard `ReactViewGroup.addChildrenForAccessibility` against transient non-descendant races during accessibility traversal on Android. This replaces direct `super.addChildrenForAccessibility(...)` calls with a small wrapper that: - catches `IllegalArgumentException` - only swallows the specific "descendant of this view" case - rethrows all other `IllegalArgumentException`s ## Why There are recurring crashes with stack traces ending in: `ViewGroup.offsetRectBetweenParentAndChild` -> `offsetDescendantRectToMyCoords` -> `addChildrenForAccessibility` -> `IllegalArgumentException: parameter must be a descendant of this view`. This can happen when accessibility is traversing children while views are being re-parented/removed. Related reports: - https://github.com/facebook/react-native/issues/32649 - https://github.com/facebook/react-native/issues/38925 - https://github.com/facebook/react-native/issues/7377 - https://github.com/kirillzyusko/react-native-keyboard-controller/issues/961 - https://github.com/kirillzyusko/react-native-keyboard-controller/pull/962 - https://github.com/dotnet/maui/issues/32927 There is also precedent in RN for handling this class of non-descendant race defensively: - https://github.com/facebook/react-native/pull/55273 ## Scope This is intentionally minimal and localized to accessibility child collection in `ReactViewGroup`. Behavior is unchanged in the non-racy path. ## Changelog: [Android] [Fixed] - Guard `ReactViewGroup.addChildrenForAccessibility` against transient non-descendant accessibility traversal crashes. Pull Request resolved: https://github.com/facebook/react-native/pull/56182 Test Plan: Validated in a downstream RN Android app on Android 16 with repeated sheet/modal open-close stress and active accessibility hierarchy traversal; crash reproduces before patch and no longer reproduces with this guard. Reviewed By: jorge-cab Differential Revision: D97861147 Pulled By: Abbondanzo fbshipit-source-id: 15e624a94d5acd99777a7426787b52d8e85cdf8a --- .../react/bridge/ReactSoftExceptionLogger.kt | 4 ++++ .../react/views/view/ReactViewGroup.kt | 20 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.kt index 51ff4d0224b..877f2fc2843 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactSoftExceptionLogger.kt @@ -11,6 +11,7 @@ import androidx.annotation.StringDef import com.facebook.common.logging.FLog import com.facebook.proguard.annotations.DoNotStrip import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.CLIPPING_PROHIBITED_VIEW +import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_ADD_CHILDREN_FOR_ACCESSIBILITY import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_IS_VIEW_CLIPPED import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.RVG_ON_VIEW_REMOVED import com.facebook.react.bridge.ReactSoftExceptionLogger.Categories.SOFT_ASSERTIONS @@ -21,6 +22,7 @@ import java.util.concurrent.CopyOnWriteArrayList internal object ReactSoftExceptionLogger { @Retention(AnnotationRetention.SOURCE) @StringDef( + RVG_ADD_CHILDREN_FOR_ACCESSIBILITY, RVG_IS_VIEW_CLIPPED, RVG_ON_VIEW_REMOVED, CLIPPING_PROHIBITED_VIEW, @@ -31,6 +33,8 @@ internal object ReactSoftExceptionLogger { /** Constants that listeners can utilize for custom category-based behavior. */ object Categories { + const val RVG_ADD_CHILDREN_FOR_ACCESSIBILITY: String = + "ReactViewGroup.addChildrenForAccessibility" const val RVG_IS_VIEW_CLIPPED: String = "ReactViewGroup.isViewClipped" const val RVG_ON_VIEW_REMOVED: String = "ReactViewGroup.onViewRemoved" const val CLIPPING_PROHIBITED_VIEW: String = "ReactClippingProhibitedView" diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt index 44aeba9e204..880c15362a4 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt @@ -997,12 +997,12 @@ public open class ReactViewGroup public constructor(context: Context?) : } else if (axOrderParentOrderList != null) { // view is a container so add its children normally if (!isFocusable) { - super.addChildrenForAccessibility(outChildren) + safeAddChildrenForAccessibility(outChildren) return // If this view can coopt, turn the focusability off its children but add them to the tree } else if (isFocusable && (contentDescription == null || contentDescription == "")) { - super.addChildrenForAccessibility(outChildren) + safeAddChildrenForAccessibility(outChildren) for (i in 0..) { + try { super.addChildrenForAccessibility(outChildren) + } catch (error: IllegalArgumentException) { + // Android 16 can race while building accessibility child lists during fast re-parenting. + if (error.message?.contains("descendant of this view") == true) { + logSoftException( + ReactSoftExceptionLogger.Categories.RVG_ADD_CHILDREN_FOR_ACCESSIBILITY, + error, + ) + } else { + throw error + } } }