Skip to content

fix(android): don't preallocate a view that a commit already created - #60019

Open
erennyuksell wants to merge 2 commits into
react:mainfrom
erennyuksell:fix-preallocation-after-commit
Open

erennyuksell wants to merge 2 commits into
react:mainfrom
erennyuksell:fix-preallocation-after-commit

Conversation

@erennyuksell

Copy link
Copy Markdown

Summary:

Fixes #60018, and I believe #58526 too.

The preallocation queue is drained on the UI thread, so a commit can get there first. If the commit creates a view and the next commit deletes it again (a View that is flattened right after it mounts, e.g. opacity set and cleared in a layout effect), executeMount adds the tag to allocatedViewTags and erases it before the drain runs. The drain then finds the tag missing, registers it as preallocated and creates the Java view, which the queued DELETE removes. The tag stays allocated in C++ with no view behind it. The next CREATE for that family is skipped, the inserts land in a view that doesn't exist, and the next REMOVE crashes with Unable to find parentView for tag.

This skips the drain for a family that is already part of a committed tree (ShadowNodeFamily::hasBeenMounted()). Its view belongs to executeMount from then on. The check runs under allocatedViewsMutex_, which executeMount takes after the commit has marked the family, so the two can't interleave in a way that brings the tag back. Views that haven't been committed yet are still preallocated as before.

Changelog:

[ANDROID] [FIXED] - Fix a crash in removeViewAt when a view is flattened in the commit right after it mounts while its preallocation is still queued

Test Plan:

Added two cases to FabricMountingManagerInstrumentationTest (with commitQueuedView and executeDeleteMutation on the test helper): a queued view that a commit created and deleted is not registered by the drain, and a view no commit has taken yet still is. These run only in the internal build (the OSS Gradle build excludes this test), so I couldn't run them myself.

What I did run, on an API 36 emulator with a release build:

  • Repro app (https://github.com/erennyuksell/rn-preallocation-crash, from the reproducer template) on stock 0.88.1: crashes 5 out of 5 launches, within 4 to 18 seconds.
  • The same component mounted in our app on stock 0.88.1: 5 out of 5, within 4 to 20 seconds.
  • Same, with 0.88.1 built from source with this change: 0 out of 5, 40 seconds each (about 26 mount, flatten and unflatten rounds per launch).
  • Our production app: about 1 in 4 tries crashed before, 0 in 24 with the change. Preallocation still ran (2928 preallocated views in one run).

0.88 doesn't have QueuedView or ShadowNodeFamily::hasBeenMounted() yet, so for that build I carried the family in the queue and added the getter, same as main.

The preallocation queue is drained on the UI thread, so a commit can get
there first. When a commit creates a view and the next one deletes it again
(a View flattened right after it mounts), executeMount adds the tag to
allocatedViewTags and erases it before the drain runs. The drain then
registers the tag as preallocated and creates the Java view, which the
queued DELETE removes, so the tag stays allocated with no view behind it.
The next CREATE for that family is skipped and the next REMOVE throws
"Unable to find parentView for tag".

Skip the drain for a family that is already part of a committed tree: its
view is executeMount's to create from then on.
@meta-cla

meta-cla Bot commented Nov 16, 2026

Copy link
Copy Markdown

Hi @erennyuksell!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 16, 2026
@meta-cla

meta-cla Bot commented Nov 16, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 16, 2026
@meta-codesync

meta-codesync Bot commented Nov 16, 2026

Copy link
Copy Markdown

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

@javache

javache commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Please run formatter

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at e089b9336b72.

android: no capture

ios: no capture

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

Development

Successfully merging this pull request may close these issues.

[Android] Crash in removeViewAt ("Unable to find parentView for tag") when a view is flattened in the commit right after it mounts

2 participants