Skip to content

Fix: unmounted secureTextEntry TextInput retained (with its Activity) by the measurement cache - #59470

Open
A-Kimpo wants to merge 1 commit into
react:mainfrom
A-Kimpo:A-Kimpo-fix-securetextentry-leak
Open

A-Kimpo wants to merge 1 commit into
react:mainfrom
A-Kimpo:A-Kimpo-fix-securetextentry-leak

Conversation

@A-Kimpo

@A-Kimpo A-Kimpo commented Oct 25, 2026

Copy link
Copy Markdown

Summary:

Fixes #59469.

ReactEditText.updateCachedSpannable() copies the current text, with all its spans, into TextLayoutManager's spannable cache. The entry is evicted only from ReactEditText.finalize().

For a password field the Editable carries PasswordTransformationMethod.Visible while the last typed character is shown. That span is not a NoCopySpan, so it ends up in the copy. It references the Editable. The Editable's TextView.ChangeWatcher references the ReactEditText, and the view's ThemedReactContext has the Activity as its base context. The cache therefore keeps the view reachable, finalize() never runs, and the entry is never evicted.

As a result, every unmounted <TextInput secureTextEntry /> that received input stays in memory for the life of the process, together with its ancestors and its typed text. After the Activity is recreated, the old Activity stays too.

The copy only feeds measurement, so this change keeps only the spans that can affect it: CharacterStyle and ParagraphStyle (Android's own style spans) and React Native's ReactSpans. Editor bookkeeping spans are dropped. Eviction stays in finalize(), as #58869 requires, because a background layout can still measure a TextInput after its view was deleted. The view simply becomes collectable again.

Changelog:

[ANDROID] [FIXED] - An unmounted TextInput with secureTextEntry is no longer retained, together with its Activity, by the TextInput measurement cache

Test Plan:

Reproducer: a fresh 0.88.0 app with no other dependencies (https://github.com/A-Kimpo/rn-secure-textinput-leak). One TextInput gets a new key on every mount, plus mount/unmount and secureTextEntry toggles. It is a release build with debuggable true and React Native built from source. It runs on an emulator: API 34, arm64. The test types abc123, unmounts and mounts 5 times, then takes am dumpheap -g and counts strongly reachable instances.

Build ReactEditText reachable MainActivity reachable after Activity recreation
Before: official 0.88.0 artifacts 6 (1 mounted + 5 leaked) 2
Before: built from source, unpatched 6 2
After: built from source, this change 1 1
After: built from source, this change, plain TextInput 1 —
Control: official artifacts, plain TextInput 1 1

The same runs on 0.87.1 gave the same numbers.

Measurement is unchanged. An auto-growing multiline TextInput, which is measured from the cached spannable, reports the same height via onLayout before and after the change: 48 dp empty and 104 dp with four lines, two runs each, on 0.88.0 and 0.87.1.

…cache

Added logic to strip specific spans from the text buffer to prevent memory leaks.
@meta-cla

meta-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

Hi @A-Kimpo!

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 commented Oct 25, 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!

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

meta-cla Bot commented Oct 25, 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 Oct 25, 2026

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] Unmounted TextInput with secureTextEntry is never released: TextLayoutManager's spannable cache keeps it (and its Activity) alive

1 participant