Skip to content

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

Open
A-Kimpo wants to merge 2 commits into
react:mainfrom
A-Kimpo:A-Kimpo-fix-securetextentry-leak
Open

A-Kimpo wants to merge 2 commits 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.

Unit test: ReactEditTextMeasurementCacheTest. cachedCopyOfSecureTextHasNoSpanReferencingTheEditable fails without the change (the PasswordTransformationMethod.Visible span reaches the cache) and passes with it. cachedCopyKeepsStyleSpans passes both ways. All 138 views.text and views.textinput unit tests pass.

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.views.textinput." --tests "com.facebook.react.views.text."

…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
@A-Kimpo

A-Kimpo commented Nov 16, 2026

Copy link
Copy Markdown
Author

Pushed a Robolectric test, ReactEditTextMeasurementCacheTest:

  • cachedCopyOfSecureTextHasNoSpanReferencingTheEditable types into a password field with "show password" on and checks that the PasswordTransformationMethod.Visible span does not reach the measurement cache, and that only CharacterStyle, ParagraphStyle and ReactSpan spans do. It fails on main and passes with this change.
  • cachedCopyKeepsStyleSpans checks that style spans still reach the cache, so measurement is unchanged.

This is my first PR here, so the CI workflows (Test All, Format, Validate C++ API Snapshots) are waiting for maintainer approval. Could someone approve them?

For triage: #59472 contains the same change to ReactEditText.kt, based on the fix suggested in #59469. Timeline: #59469 opened Oct 25 21:29 UTC, this PR Oct 25 21:56 UTC, #59472 Oct 26 01:17 UTC. The heap-dump reproducer is in the description and at https://github.com/A-Kimpo/rn-secure-textinput-leak.

@A-Kimpo

A-Kimpo commented Nov 30, 2026

Copy link
Copy Markdown
Author

Hey @cortinico, this is my first React Native PR. I saw your comment about LineBreakConfigSpan in #59472. Do you think it’s worth making the same change here? I’m happy to update this PR if so.

@cortinico

Copy link
Copy Markdown
Contributor

Hey @cortinico, this is my first React Native PR. I saw your comment about LineBreakConfigSpan in #59472. Do you think it’s worth making the same change here? I’m happy to update this PR if so.

Thanks for flagging, it seems this PR and #59470 are duplicate so I'd rather go with that one and close this as that one is a bit forward in the review process.

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

2 participants