Repository navigation
[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
Open
andreialecu wants to merge 2 commits into
andreialecu wants to merge 2 commits into
Conversation
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.
cipolleschi
approved these changes
Nov 27, 2026
|
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this in D134806020. |
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:
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 (
boxShadowtogether withoverflow: '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,
RCTViewComponentViewkeeps its children in a separate container view. That happens withoverflow: 'hidden'plus aboxShadow, an outline, or (withenableIOSViewClipToPaddingBox) a border. When the view leaves that state,currentContainerViewmoves the children back withaddSubview:, which appends them after the layers the view draws itself.invalidateLayerhas just re-added those layers (box shadow, outline). BecauseinsertSubview: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:
prepareForRecyclekeeps_useCustomContainerViewand_containerView. If the recycled view is reused for a stacking context (aPressable,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 itsboxShadowand switches fromoverflow: 'hidden'to'visible'.This PR changes two things:
currentContainerViewputs 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.prepareForRecycledrops the container, so a reused view starts clean. It does this throughcurrentContainerView, which also moves a subclass'scontentViewback and returnsclipsToBoundsand the mask to the view. The view must already haveclipsToBoundsset when the next owner arrives, because that owner'supdatePropscomparesoverflowagainst 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
mountChildComponentViewandunmountChildComponentViewin this file. It doesn't touchcurrentContainerVieworprepareForRecycle, 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
boxShadowstops clipping or is recycledTest Plan:
Unit tests
New tests in
RCTViewComponentViewContainerTests.mm:testChildrenMovedOutOfContainerStayBelowSelfDrawnLayerscovers the case without recycling: clip and shadow, two children, dropoverflow, mount a third child at index 2, then check the order and unmount it.testRecycledViewDropsContainerBeforeNextOwnerMountsChildrenruns the reproducer's sequence in native code.testRecycledViewKeepsClippingForNextOwnerThatClipscovers theoverflowcomparison against the previous owner's props.testRecycledViewMovesContentViewOutOfContainerchecks that a subclass'scontentViewcomes back.testUnmountReconcilesRecycledContainerAtZeroSizeis nowtestUnmountReconcilesStaleContainerAtZeroSizeand uses a prop change instead of recycling.OSS CI doesn't run these tests. I ran them locally by enabling the
React-RCTFabricTeststest spec in RNTester (:testspecs => ['Tests']inscripts/cocoapods/fabric.rb, not part of this PR):The two files in
React/Tests/Textdon't compile in OSS becausereact/renderer/element/ComponentBuilder.hisn't available there, so I limited the test spec toTests/Mountingfor this run.RCTViewComponentView.mmreverted, 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.testRecycledViewKeepsClippingForNextOwnerThatClipspasses 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.mmrevertedThe 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:0.90.0-nightly-20261125-c4b72b416from sourceboxShadowview,overflowhidden→visible, then a child mounted at index 1 and removed (0.88.1)A view with an inset and an outset
boxShadowlooks 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.