Skip to content

[0.86] Fix premature blob deallocation during FileReader reads - #59187

Open
dixitpriyanshu wants to merge 1 commit into
react:0.86-stablefrom
dixitpriyanshu:fix/0.86-filereader-retain-blob
Open

dixitpriyanshu wants to merge 1 commit into
react:0.86-stablefrom
dixitpriyanshu:fix/0.86-filereader-retain-blob

Conversation

@dixitpriyanshu

@dixitpriyanshu dixitpriyanshu commented Oct 19, 2026 •

Copy link
Copy Markdown

Summary:

Backport of #57796 (568eaa6) to 0.86-stable.

FileReader.readAsText / readAsDataURL / readAsArrayBuffer pass only blob.data to the native module and keep no reference to the Blob. whatwg-fetch reads response bodies exactly this way (readBlobAsText keeps only the reader), so for a plain fetch(url).then(r => r.json()) the Blob and its BlobCollector can be GC'd while the native read is in flight. The collector's finalizer frees the native buffer and the read rejects with Unable to resolve data for blob (iOS) / The specified blob is invalid (Android). See #56884.

The commit on main doesn't cherry-pick cleanly: it builds on the read-id bookkeeping (_readId / _startRead) from #57745, which isn't on 0.86. This PR applies the same fix to the 0.86 FileReader:

  • Retain the Blob on the reader (_blob) for the duration of each read.
  • Release it when the read settles, by identity, so a stale read settling after abort() can't drop a newer read's reference.
  • Release it in _reset(), which covers abort().

The four tests from #57796 are ported unchanged.

For context, a production app on 0.86.0 sees the iOS error from ordinary fetch + response.json() calls (~1.5k events / 150 users over the last 90 days), and the Android message at a much larger volume.

Changelog:

[GENERAL] [FIXED] - Retain Blob reference in FileReader during pending native reads to prevent premature deallocation by BlobCollector

Test Plan:

  • yarn jest packages/react-native/Libraries/Blob: 26 passed. The 4 new FileReader tests fail without the FileReader.js change and pass with it.
  • yarn flow focus-check on both changed files: 0 errors.
  • eslint and prettier --check on both changed files: clean.

@meta-cla

meta-cla Bot commented Oct 19, 2026

Copy link
Copy Markdown

Hi @dixitpriyanshu!

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!

Backport of react#57796 to 0.86-stable.

FileReader passes only `blob.data` to the native module and keeps no
reference to the Blob. When the caller also drops its reference (as
whatwg-fetch's readBlobAsText does for `response.json()`/`.text()`), the
Blob and its BlobCollector can be GC'd while the native read is in
flight. The collector's finalizer then frees the native buffer and the
read rejects with "Unable to resolve data for blob" (iOS) / "The
specified blob is invalid" (Android).

The fix on main builds on read ids from react#57745, which 0.86 doesn't have,
so it doesn't cherry-pick cleanly. This version retains the Blob on the
reader for the duration of the read, releases it in `_reset()` (covering
`abort()`), and settles release by identity so a stale read finishing
after abort can't drop a newer read's reference.
@dixitpriyanshu
dixitpriyanshu force-pushed the fix/0.86-filereader-retain-blob branch from 804d71c to 1fe94ee Compare October 20, 2026 04:14
@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 20, 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. Pick Request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant