Skip to content

[iOS] Fix "Attempt to unmount a view which has a different index" crash when a view drops its clipping container - #60306

Open
andreialecu wants to merge 2 commits into
react:mainfrom
andreialecu:fix/ios-recycled-container-view
Open

andreialecu wants to merge 2 commits into
react:mainfrom
andreialecu:fix/ios-recycled-container-view

Conversation

@andreialecu

Copy link
Copy Markdown

Summary:

Fixes #60304. Fixes #60140.

Debug builds crash with "Attempt to unmount a view which has a different index". Release builds don't throw, but the new child is drawn behind its siblings, and later mount and unmount instructions on that parent hit the wrong views. Ordinary styles trigger it (boxShadow together with overflow: 'hidden'). It reproduces on 0.88.1 and on today's nightly, and it has been reported three times: #59166, #60140 and #60304. #59628 fixed a related assertion caused by the same stale container.

While a view's style clips and also draws outside its bounds, RCTViewComponentView keeps its children in a separate container view. That happens with overflow: 'hidden' plus a boxShadow, an outline, or (with enableIOSViewClipToPaddingBox) a border. When the view leaves that state, currentContainerView moves the children back with addSubview:, which appends them after the layers the view draws itself. invalidateLayer has just re-added those layers (box shadow, outline). Because insertSubview:atIndex: counts sublayers, every child mounted after that lands one index too low.

A view can get into this state in two ways. The first is recycling: prepareForRecycle keeps _useCustomContainerView and _containerView. If the recycled view is reused for a stacking context (a Pressable, collapsable={false}, …), Fabric mounts its children before applying its props, so the children go into the stale container, and the new props then move them back after the new owner's box shadow layer. The second needs no recycling: a view keeps its boxShadow and switches from overflow: 'hidden' to 'visible'.

This PR changes two things:

  • currentContainerView puts the children back at the front, below the view's own layers. Children mounted without a container already sit there, because those layers are added after them.
  • prepareForRecycle drops the container, so a reused view starts clean. It does this through currentContainerView, which also moves a subclass's contentView back and returns clipsToBounds and the mask to the view. The view must already have clipsToBounds set when the next owner arrives, because that owner's updateProps compares overflow against the previous owner's props and won't set it again if both clip.

debaf42 (#59628) reconciles the container before the unmount check. It doesn't change where the children end up, and its commit message says it doesn't reset the container on recycle. Its test created the stale container through recycling, which no longer leaves one behind, so I changed that test to reach the same state with a prop change at zero size. It still covers the reconcile-before-check path.

#59857 also edits mountChildComponentView and unmountChildComponentView in this file. It doesn't touch currentContainerView or prepareForRecycle, so at most one of the two needs a rebase.

Changelog:

[IOS] [FIXED] - Fix "Attempt to unmount a view which has a different index" crash and out-of-order children after a view with boxShadow stops clipping or is recycled

Test Plan:

Unit tests

New tests in RCTViewComponentViewContainerTests.mm:

  • testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers covers the case without recycling: clip and shadow, two children, drop overflow, mount a third child at index 2, then check the order and unmount it.
  • testRecycledViewDropsContainerBeforeNextOwnerMountsChildren runs the reproducer's sequence in native code.
  • testRecycledViewKeepsClippingForNextOwnerThatClips covers the overflow comparison against the previous owner's props.
  • testRecycledViewMovesContentViewOutOfContainer checks that a subclass's contentView comes back.

testUnmountReconcilesRecycledContainerAtZeroSize is now testUnmountReconcilesStaleContainerAtZeroSize and uses a prop change instead of recycling.

OSS CI doesn't run these tests. I ran them locally by enabling the React-RCTFabric Tests test spec in RNTester (:testspecs => ['Tests'] in scripts/cocoapods/fabric.rb, not part of this PR):

xcodebuild test -workspace RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests -destination 'platform=iOS Simulator,name=iPhone 17 Pro'

The two files in React/Tests/Text don't compile in OSS because react/renderer/element/ComponentBuilder.h isn't available there, so I limited the test spec to Tests/Mounting for this run.

  • With the change, all 53 tests pass.
  • With only RCTViewComponentView.mm reverted, three of the new tests fail, two of them on the same "Attempt to unmount a view which has a different index" assertion the app hits. testRecycledViewKeepsClippingForNextOwnerThatClips passes either way. It is there to catch the recycle reset dropping the clipping.

End to end on this commit

I built RNTester from source at this commit (Debug, iPhone 17 Pro simulator, iOS 27.2) and pasted each reproducer into RNTesterPlayground.js, which isn't part of this PR:

RCTViewComponentView.mm reverted With the change
#60304 reproducer (App.tsx) Green pill hidden after step 1, step 2 crashes with "Attempt to unmount a view which has a different index" Pill shows, no crash
#60141 playground (border instead of box shadow) Green square hidden at step 3, step 4 crashes with the same assertion Square shows, no crash

The Box Shadow examples in RNTester render normally with the change.

Earlier revision

An earlier revision had the same two changes, with the recycle reset a few lines higher in prepareForRecycle. I ran it in Debug on iOS 26.5, three times per case:

Without With
#60304 reproducer on nightly 0.90.0-nightly-20261125-c4b72b416 from source Crash 3 of 3 No crash 3 of 3
Same reproducer on 0.88.1 from source Crash 3 of 3 No crash 3 of 3
No recycling: boxShadow view, overflow hidden → visible, then a child mounted at index 1 and removed (0.88.1) Crash 3 of 3 No crash 3 of 3

A view with an inset and an outset boxShadow looks the same after its children move back out of the container as a view that never clipped.

cc @javache, who imported #59628; @cipolleschi, who imported earlier recycling fixes in this component; and @joevilches, who wrote the container view in #46189. A review from any of you would help.

A recycled RCTViewComponentView kept the clipping container of its previous
owner, and moving children back out of a container appended them after the
view's own box shadow layers. insertSubview:atIndex: counts sublayers, so later
mounts landed one index too low, which asserts on unmount in debug builds.

Drop the container in prepareForRecycle and move children back below the
view's own layers.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 26, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 26, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this in D134806020.

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

2 participants