Files
react-native/Libraries
wood1986 bd12e41188 fix: fix the race condition when calling readAsDataURL after new Blob(blobs) (#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: https://github.com/facebook/react-native/pull/34096

Reviewed By: cipolleschi

Differential Revision: D37514981

Pulled By: javache

fbshipit-source-id: 4bf84ece99871276ecaa5aa1849b9145ff44dbf4
2022-06-30 05:13:17 -07:00
..
2022-04-05 12:15:05 -07:00
2022-04-05 12:15:05 -07:00
2022-05-19 18:48:24 -07:00
2022-06-18 08:23:32 -07:00