Skip to content

[iOS][Fabric] Crash "Attempt to unmount a view which has a different index": a recycled view keeps its container view (minimal repro inside) #60304

Backlog

Description

@andreialecu

Description

On iOS, a View with boxShadow and overflow: "hidden" is replaced by a Pressable with a boxShadow. Children of the Pressable that mount after that are inserted one index too low, and so appear behind their siblings. Unmounting one of them crashes debug builds with "Attempt to unmount a view which has a different index". Release builds don't crash, but the view stays hidden, and later mutations on that parent act on the wrong views.

The cause is a recycled RCTViewComponentView that keeps _useCustomContainerView and _containerView from its previous use, because prepareForRecycle doesn't reset them. #59166 described the same mechanism and was closed as stale without a reproducer. This issue has one.

How it goes wrong:

  1. The clipping view with a box shadow gets a container view (styleWouldClipOverflowInk). It's unmounted and recycled with that container still in place.
  2. The Pressable's view dequeues that instance. The Pressable is a stacking context (it's accessible), so its children are its native subviews. RCTPerformMountInstructions applies a view's props at that view's own Insert, and the children's Inserts come first, so the children are mounted into the stale container.
  3. The Pressable's props arrive. invalidateLayer adds the box shadow layer to self.layer. Then, at // clipping, self.currentContainerView sees _useCustomContainerView == NO and moves the children back with addSubview:, which appends them after the shadow layer.
  4. The layer order is now [shadow CALayer, child0]. UIKit's insertSubview:atIndex: counts sublayers, so mountChildComponentView:index:1 puts the new child at subview index 0, behind child0.
  5. unmountChildComponentView:index:1 then finds it at index 0 and asserts.

debaf42 ("Fix iOS Fabric unmount assertions for zero-sized recycled views") reconciles the container before the unmount check. It doesn't reset the container on recycle, which its own message says, and it doesn't change this order. Nightly 0.90.0-nightly-20261125-c4b72b416 still migrates with addSubview: and doesn't reset on recycle.

Steps to reproduce

  1. Clone https://github.com/andreialecu/rn-recycled-container-crash (the community reproducer template). Run npm install, cd ios && bundle exec pod install, then npm run ios. It must be a Debug build, because the assertion is debug-only.
  2. Tap 1. Swap the views. The green pill should cover the left half of the dark bar. It's missing, because it's behind the bar.
  3. Tap 2. Hide the green pill. The app crashes.

The whole reproducer is App.tsx:

import { useState } from 'react';
import { Pressable, Text, View } from 'react-native';

const SHADOW = '0px 2px 6px rgba(0,0,0,0.4)';
const FILL = { position: 'absolute', left: 0, right: 0, top: 0, bottom: 0 } as const;

export default function App() {
  const [step, setStep] = useState(1);
  const [width, setWidth] = useState(0);
  return (
    <View style={{ flex: 1, padding: 16, paddingTop: 80 }}>
      <Text onPress={() => setStep(step + 1)} style={{ fontSize: 18, padding: 12, backgroundColor: '#c88' }}>
        {['', '1. Swap the views', '2. Hide the green pill (crashes)', 'Done'][step]}
      </Text>
      {step === 1 ? (
        // A view that clips and has a box shadow: RCTViewComponentView gives it a container view.
        <View key="clipped" style={{ height: 40, boxShadow: SHADOW, overflow: 'hidden' }} />
      ) : (
        // A Pressable with a box shadow that doesn't clip reuses the recycled view.
        <Pressable key="pressable" onLayout={e => setWidth(e.nativeEvent.layout.width)} style={{ height: 40, boxShadow: SHADOW }}>
          <View style={[FILL, { backgroundColor: '#333' }]} />
          {width > 0 && step === 2 ? <View style={{ ...FILL, right: width / 2, backgroundColor: '#5a5' }} /> : null}
        </Pressable>
      )}
    </View>
  );
}

For the second view to reach its stale container it has to be a stacking context, and a plain View with only boxShadow isn't one: its children are mounted into the nearest ancestor that is. Pressable (accessible), collapsable={false}, nativeID, opacity or a transform all make it one; we saw it crash with Pressable, accessible and collapsable={false}. Our app first hit it with a glass chip (boxShadow + overflow: hidden) in a menu that closes, followed by a segmented control in the sheet that opens. The control has a boxShadow, sits in a GestureDetector, and mounts its pill after onLayout.

React Native Version

0.90.0-nightly-20261125-c4b72b416 (the reproducer). It also crashes on 0.88.1 (Expo SDK 58, prebuilt core).

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info

System:
  OS: macOS 27.2.1
  CPU: (18) arm64 Apple M5 Max
Binaries:
  Node: 24.16.0
  npm: 11.13.0
Managers:
  CocoaPods: 1.17.0
SDKs:
  iOS SDK: iOS 27.2
IDEs:
  Xcode: 27.2/27B52
npmPackages:
  "@react-native-community/cli": 22.0.0-alpha.0
  react: 19.3.0
  react-native: 0.90.0-nightly-20261125-c4b72b416
iOS:
  hermesEnabled: true
  newArchEnabled: true

Simulator: iPhone 17 Pro, iOS 26.5. The crash happened in 3 of 3 runs on nightly and in 3 of 3 on 0.88.1.

Stacktrace or Logs

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to unmount a view which has a different index. (parent: <RCTViewComponentView: 0x103a88600; frame = (16 125.667; 370 40); tag = 22; layer = <CALayer: 0x103967750>>, child: <RCTViewComponentView: 0x103a88f00; frame = (0 0; 185 40); tag = 24; backgroundColor = UIExtendedSRGBColorSpace 0.333333 0.666667 0.333333 1; layer = <CALayer: 0x10faba2e0>>, index: 1, actual index: 0, tag at index: 20)'

Logging added to RCTViewComponentView.mm in a larger variant of the reproducer (ten such views, so the tags differ) shows the order. The parent is a reused instance, and the shadow CALayer ends up first:

mount parent=664 child=658 idx=0 flag=1 container=1          <- children go into the stale container
mount parent=664 child=662 idx=1 flag=1 container=1
finalize tag=664 flag 1->0 container=1
migrate-back tag=664 cont=[658,662] sublayers=[UIView,CALayer]
migrated tag=664 self=[658,662] layers=[CALayer(z0),v658,v662]
mount parent=664 child=738 idx=1 → self=[738,658,662]        <- index 1 lands at 0

Reproducer

https://github.com/andreialecu/rn-recycled-container-crash

Screenshots and Videos

After step 1 (the bug: the green pill is behind the bar) After step 1 with either fix

Suggested fix

Each fix on its own makes the reproducer work on 0.88.1 built from source: no crash in 3 of 3 runs, and the pill shows. Without them it crashed in 3 of 3 runs. (2) was tested with this Pressable version; (1) with the same reproducer using <View collapsable={false}> in place of the Pressable, and in our app.

  1. In currentContainerView, move the children back below the layers the view draws itself, where children mounted without a container sit:
NSUInteger index = 0;
for (UIView *subview in _containerView.subviews) {
  [effectiveContentView insertSubview:subview atIndex:index++];
}
  1. In prepareForRecycle, drop the container so a reused view starts clean:
if (_containerView) {
  for (UIView *subview in _containerView.subviews) {
    [self.effectiveContentView addSubview:subview];
  }
  self.effectiveContentView.clipsToBounds = _containerView.clipsToBounds;
  self.effectiveContentView.layer.mask = _containerView.layer.mask;
  [_containerView removeFromSuperview];
  _containerView = nil;
}
_useCustomContainerView = NO;

(2) removes this cause. (1) also keeps the order right for any other path that moves children back out of a container. I can open a PR with either or both.

Activity

  1. andreialecu commented on Nov 26, 2026

    @andreialecu
    Author

    Fix in #60306. It drops the stale container in prepareForRecycle and moves children back below the view's own layers, which also fixes the case without recycling (a view with boxShadow switching overflow from hidden to visible). It adds native regression tests and was verified against this reproducer and the one in #60141.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions