From 1fe94ee16f7bbc8b9664eb8d6d5897751e8c0d3d Mon Sep 17 00:00:00 2001 From: Priyanshu Dixit Date: Tue, 20 Oct 2026 09:44:34 +0530 Subject: [PATCH] [0.86] Fix premature blob deallocation during FileReader reads Backport of #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 #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. --- .../react-native/Libraries/Blob/FileReader.js | 25 +++++ .../Blob/__tests__/FileReader-test.js | 105 ++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/packages/react-native/Libraries/Blob/FileReader.js b/packages/react-native/Libraries/Blob/FileReader.js index 9cb37e5ca487..c87507d92993 100644 --- a/packages/react-native/Libraries/Blob/FileReader.js +++ b/packages/react-native/Libraries/Blob/FileReader.js @@ -44,6 +44,11 @@ class FileReader extends EventTarget { _error: ?Error; _result: ?ReaderResult; _aborted: boolean = false; + // Keep the Blob strongly referenced until the native read settles. If the + // caller drops its own reference the Blob can be GC'd mid-read. Its + // BlobCollector finalizer then frees the native buffer and the read fails + // with "The specified blob is invalid". + _blob: ?Blob; constructor() { super(); @@ -54,6 +59,14 @@ class FileReader extends EventTarget { this._readyState = EMPTY; this._error = null; this._result = null; + this._blob = null; + } + + _releaseBlob(blob: Blob): void { + // A stale read settling after abort() must not drop a newer read's blob. + if (this._blob === blob) { + this._blob = null; + } } _setReadyState(newState: ReadyState) { @@ -80,8 +93,11 @@ class FileReader extends EventTarget { ); } + this._blob = blob; + NativeFileReaderModule.readAsDataURL(blob.data).then( (text: string) => { + this._releaseBlob(blob); if (this._aborted) { return; } @@ -93,6 +109,7 @@ class FileReader extends EventTarget { this._setReadyState(DONE); }, error => { + this._releaseBlob(blob); if (this._aborted) { return; } @@ -111,8 +128,11 @@ class FileReader extends EventTarget { ); } + this._blob = blob; + NativeFileReaderModule.readAsDataURL(blob.data).then( (text: string) => { + this._releaseBlob(blob); if (this._aborted) { return; } @@ -120,6 +140,7 @@ class FileReader extends EventTarget { this._setReadyState(DONE); }, error => { + this._releaseBlob(blob); if (this._aborted) { return; } @@ -138,8 +159,11 @@ class FileReader extends EventTarget { ); } + this._blob = blob; + NativeFileReaderModule.readAsText(blob.data, encoding).then( (text: string) => { + this._releaseBlob(blob); if (this._aborted) { return; } @@ -147,6 +171,7 @@ class FileReader extends EventTarget { this._setReadyState(DONE); }, error => { + this._releaseBlob(blob); if (this._aborted) { return; } diff --git a/packages/react-native/Libraries/Blob/__tests__/FileReader-test.js b/packages/react-native/Libraries/Blob/__tests__/FileReader-test.js index 549d027bc3a0..6b1aa715d513 100644 --- a/packages/react-native/Libraries/Blob/__tests__/FileReader-test.js +++ b/packages/react-native/Libraries/Blob/__tests__/FileReader-test.js @@ -12,6 +12,7 @@ import type Event from '../../../src/private/webapis/dom/events/Event'; +const FileReaderModuleMock = require('../__mocks__/FileReaderModule').default; const Blob = require('../Blob').default; const FileReader = require('../FileReader').default; @@ -59,4 +60,108 @@ describe('FileReader', function () { // $FlowFixMe[cannot-resolve-name] expect(new TextDecoder().decode(ab)).toBe('42'); }); + + it('should retain the blob until the read resolves', async () => { + let resolveRead: string => void = () => {}; + const spy = jest + .spyOn(FileReaderModuleMock, 'readAsText') + .mockImplementation( + () => + new Promise(resolve => { + resolveRead = resolve; + }), + ); + + const reader = new FileReader(); + const blob = new Blob(); + const loadend = new Promise(resolve => { + reader.onloadend = resolve; + }); + reader.readAsText(blob); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(blob); + + resolveRead(''); + await loadend; + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(null); + + spy.mockRestore(); + }); + + it('should release the blob when the read rejects', async () => { + let rejectRead: Error => void = () => {}; + const spy = jest + .spyOn(FileReaderModuleMock, 'readAsText') + .mockImplementation( + () => + new Promise((resolve, reject) => { + rejectRead = reject; + }), + ); + + const reader = new FileReader(); + const blob = new Blob(); + const loadend = new Promise(resolve => { + reader.onloadend = resolve; + }); + reader.readAsText(blob); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(blob); + + rejectRead(new Error('nope')); + await loadend; + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(null); + + spy.mockRestore(); + }); + + it('should release the blob when a pending read is aborted', () => { + const spy = jest + .spyOn(FileReaderModuleMock, 'readAsText') + .mockImplementation(() => new Promise(() => {})); + + const reader = new FileReader(); + const blob = new Blob(); + reader.readAsText(blob); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(blob); + + reader.abort(); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(null); + + spy.mockRestore(); + }); + + it('should keep retaining the new blob when a stale read settles after abort', async () => { + const resolvers: Array<(string) => void> = []; + const spy = jest + .spyOn(FileReaderModuleMock, 'readAsText') + .mockImplementation( + () => + new Promise(resolve => { + resolvers.push(resolve); + }), + ); + + const reader = new FileReader(); + const staleBlob = new Blob(); + reader.readAsText(staleBlob); + reader.abort(); + + const newBlob = new Blob(); + reader.readAsText(newBlob); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(newBlob); + + // Settle the first (aborted) read; it must not drop the new blob. + resolvers[0](''); + await Promise.resolve(); + // $FlowFixMe[prop-missing] - accessing private state for the test + expect(reader._blob).toBe(newBlob); + + spy.mockRestore(); + }); });