Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix the race condition when calling readAsDataURL after new Blob…
…(blobs) (facebook#34096) Summary: ```js async () => { let blobs = []; for (let i = 0; i < 4; i++) { const res = await fetch(); blobs = [...blobs, await res.blob()] } const blob = new Blob(blobs); // <<<<<<<<<<<<<<< a return await new Promise((resolve, reject) => { const fileReader = new FileReader(); fileReader.onload = async () => { await RNFS.writeFile(destPath, (fileReader.result as string).split(',')[1], 'base64'); resolve(destPath); }; fileReader.onabort = () => { reject(''); }; fileReader.onerror = (event) => { reject(''); }; fileReader.readAsDataURL(blob); // <<<<<<<<<<<<<<< b }); } ``` Sometime `fileReader.readAsDataURL` is unable to get blob from the dictionary after `new Blob(blobs)` and then reject with `Unable to resolve data for blob: blobId` in iOS or `The specified blob is invalid` in android. Because line `a` and `b` can be run in different thread. `new Blob([])` is in progress and `fileReader.readAsDataURL` accesses the blob dictionary ahead of the blob creation. The expected behaviour is it should finish new Blob([]) first and then readAsDataURL(blob) To fix that, there should be a lock inside the method `createFromParts`. For iOS, It needs to be a recursive_mutex to allow same thread to acquire lock ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [iOS] [Fixed] - fix the race condition when calling readAsDataURL after new Blob(blobs) Pull Request resolved: facebook#34096 Reviewed By: cipolleschi Differential Revision: D37514981 Pulled By: javache fbshipit-source-id: 4bf84ece99871276ecaa5aa1849b9145ff44dbf4
- Loading branch information